Commit 0b307c54 authored by Romain Courteaud's avatar Romain Courteaud

declareMethod: add a third options parameter to ease mutex usage.

Example: rJS(window).declareMethod('getFoo', function () {}, {mutex: 'foo'})
It will prevent 2 concurrent execution of the getFoo calls.
The same mutex can be used by other methods if the same name 'foo' is used.
parent 428d97dd
...@@ -643,15 +643,26 @@ ...@@ -643,15 +643,26 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.declareMethod // RenderJSGadget.declareMethod
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
RenderJSGadget.declareMethod = function declareMethod(name, callback) { RenderJSGadget.declareMethod = function declareMethod(name, callback,
options) {
this.prototype[name] = function triggerMethod() { this.prototype[name] = function triggerMethod() {
var context = this, var context = this,
argument_list = arguments; argument_list = arguments,
mutex_name;
function waitForMethodCallback() {
return callback.apply(context, argument_list);
}
if ((options !== undefined) && (options.hasOwnProperty('mutex'))) {
mutex_name = '__mutex_' + options.mutex;
if (!context.hasOwnProperty(mutex_name)) {
context[mutex_name] = new Mutex();
}
return context[mutex_name].lockAndRun(waitForMethodCallback);
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function waitForMethodCallback() { .push(waitForMethodCallback);
return callback.apply(context, argument_list);
});
}; };
// Allow chain // Allow chain
return this; return this;
......
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