Commit 86970117 authored by Romain Courteaud's avatar Romain Courteaud

Reduce number of created Queue.

Reuse Queue created from callback directly.
parent 7f5c500e
...@@ -209,24 +209,42 @@ ...@@ -209,24 +209,42 @@
} }
} }
function ensurePushableQueue(callback, argument_list, context) {
var result;
try {
result = callback.apply(context, argument_list);
} catch (e) {
return new RSVP.Queue()
.push(function returnPushableError() {
return RSVP.reject(e);
});
}
if (result instanceof RSVP.Queue) {
return result;
}
return new RSVP.Queue()
.push(function returnPushableResult() {
return result;
});
}
function declareMethod(klass, name, precondition_function, post_function) { function declareMethod(klass, name, precondition_function, post_function) {
klass.prototype[name] = function () { klass.prototype[name] = function () {
var argument_list = arguments, var argument_list = arguments,
context = this, context = this,
precondition_result; precondition_result,
storage_method,
queue;
return new RSVP.Queue() // Precondition function are not asynchronous
.push(function () {
if (precondition_function !== undefined) { if (precondition_function !== undefined) {
return precondition_function.apply( precondition_result = precondition_function.apply(
context.__storage, context.__storage,
[argument_list, context, name] [argument_list, context, name]
); );
} }
})
.push(function (result) { storage_method = context.__storage[name];
var storage_method = context.__storage[name];
precondition_result = result;
if (storage_method === undefined) { if (storage_method === undefined) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Capacity '" + name + "' is not implemented on '" + "Capacity '" + name + "' is not implemented on '" +
...@@ -234,22 +252,21 @@ ...@@ -234,22 +252,21 @@
501 501
); );
} }
return storage_method.apply( queue = ensurePushableQueue(storage_method, argument_list,
context.__storage, context.__storage);
argument_list
);
})
.push(function (result) {
if (post_function !== undefined) { if (post_function !== undefined) {
queue
.push(function (result) {
return post_function.call( return post_function.call(
context, context,
argument_list, argument_list,
result, result,
precondition_result precondition_result
); );
}
return result;
}); });
}
return queue;
}; };
// Allow chain // Allow chain
return this; return this;
...@@ -281,8 +298,7 @@ ...@@ -281,8 +298,7 @@
JioProxyStorage.prototype.post = function () { JioProxyStorage.prototype.post = function () {
var context = this, var context = this,
argument_list = arguments; argument_list = arguments;
return new RSVP.Queue() return ensurePushableQueue(function () {
.push(function () {
var storage_method = context.__storage.post; var storage_method = context.__storage.post;
if (storage_method === undefined) { if (storage_method === undefined) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -409,13 +425,8 @@ ...@@ -409,13 +425,8 @@
501 501
); );
} }
return new RSVP.Queue() return ensurePushableQueue(storage_method, argument_list,
.push(function () { context.__storage);
return storage_method.apply(
context.__storage,
argument_list
);
});
}; };
JioProxyStorage.prototype.hasCapacity = function (name) { JioProxyStorage.prototype.hasCapacity = function (name) {
...@@ -439,8 +450,7 @@ ...@@ -439,8 +450,7 @@
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
} }
return new RSVP.Queue() return ensurePushableQueue(function () {
.push(function () {
if (context.hasCapacity("list") && if (context.hasCapacity("list") &&
((options.query === undefined) || context.hasCapacity("query")) && ((options.query === undefined) || context.hasCapacity("query")) &&
((options.sort_on === undefined) || context.hasCapacity("sort")) && ((options.sort_on === undefined) || context.hasCapacity("sort")) &&
...@@ -449,9 +459,7 @@ ...@@ -449,9 +459,7 @@
((options.include_docs === undefined) || ((options.include_docs === undefined) ||
context.hasCapacity("include")) && context.hasCapacity("include")) &&
((options.limit === undefined) || context.hasCapacity("limit"))) { ((options.limit === undefined) || context.hasCapacity("limit"))) {
return context.buildQuery(options); return context.buildQuery(options)
}
})
.push(function (result) { .push(function (result) {
return { return {
data: { data: {
...@@ -460,6 +468,8 @@ ...@@ -460,6 +468,8 @@
} }
}; };
}); });
}
});
}; };
declareMethod(JioProxyStorage, "allAttachments", checkId); declareMethod(JioProxyStorage, "allAttachments", checkId);
...@@ -468,8 +478,7 @@ ...@@ -468,8 +478,7 @@
JioProxyStorage.prototype.repair = function () { JioProxyStorage.prototype.repair = function () {
var context = this, var context = this,
argument_list = arguments; argument_list = arguments;
return new RSVP.Queue() return ensurePushableQueue(function () {
.push(function () {
var storage_method = context.__storage.repair; var storage_method = context.__storage.repair;
if (storage_method !== undefined) { if (storage_method !== undefined) {
return context.__storage.repair.apply(context.__storage, return context.__storage.repair.apply(context.__storage,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment