Commit 92a09327 authored by Romain Courteaud's avatar Romain Courteaud

Add declareJob function.

Jobs are executed on demand only when the gadget is in the DOM.

Calling the same job a second time cancel the previous execution.
parent f55764ba
...@@ -401,6 +401,9 @@ ...@@ -401,6 +401,9 @@
g.__monitor.cancel(); g.__monitor.cancel();
} }
g.__monitor = new Monitor(); g.__monitor = new Monitor();
g.__job_dict = {};
g.__job_list = [];
g.__job_triggered = false;
g.__monitor.fail(function (error) { g.__monitor.fail(function (error) {
if (!(error instanceof RSVP.CancellationError)) { if (!(error instanceof RSVP.CancellationError)) {
return g.aq_reportServiceError(error); return g.aq_reportServiceError(error);
...@@ -463,18 +466,64 @@ ...@@ -463,18 +466,64 @@
return this; return this;
}; };
function runJob(gadget, name, callback, argument_list) {
var job_promise = new RSVP.Queue()
.push(function () {
return callback.apply(gadget, argument_list);
});
if (gadget.__job_dict.hasOwnProperty(name)) {
gadget.__job_dict[name].cancel();
}
gadget.__job_dict[name] = job_promise;
gadget.__monitor.monitor(new RSVP.Queue()
.push(function () {
return job_promise;
})
.push(undefined, function (error) {
if (!(error instanceof RSVP.CancellationError)) {
throw error;
}
}));
}
function startService(gadget) { function startService(gadget) {
gadget.__monitor.monitor(new RSVP.Queue() gadget.__monitor.monitor(new RSVP.Queue()
.push(function () { .push(function () {
var i, var i,
service_list = gadget.constructor.__service_list; service_list = gadget.constructor.__service_list,
job_list = gadget.__job_list;
for (i = 0; i < service_list.length; i += 1) { for (i = 0; i < service_list.length; i += 1) {
gadget.__monitor.monitor(service_list[i].apply(gadget)); gadget.__monitor.monitor(service_list[i].apply(gadget));
} }
for (i = 0; i < job_list.length; i += 1) {
runJob(gadget, job_list[i][0], job_list[i][1], job_list[i][2]);
}
gadget.__job_list = [];
gadget.__job_triggered = true;
}) })
); );
} }
/////////////////////////////////////////////////////////////////
// RenderJSGadget.declareJob
// gadget internal method, which trigger execution
// of a function inside a service
/////////////////////////////////////////////////////////////////
RenderJSGadget.declareJob = function (name, callback) {
this.prototype[name] = function () {
var context = this,
argument_list = arguments;
if (context.__job_triggered) {
runJob(context, name, callback, argument_list);
} else {
context.__job_list.push([name, callback, argument_list]);
}
};
// Allow chain
return this;
};
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.declareMethod // RenderJSGadget.declareMethod
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -1072,6 +1121,8 @@ ...@@ -1072,6 +1121,8 @@
tmp_constructor.__service_list = RenderJSGadget.__service_list.slice(); tmp_constructor.__service_list = RenderJSGadget.__service_list.slice();
tmp_constructor.declareMethod = tmp_constructor.declareMethod =
RenderJSGadget.declareMethod; RenderJSGadget.declareMethod;
tmp_constructor.declareJob =
RenderJSGadget.declareJob;
tmp_constructor.declareAcquiredMethod = tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod; RenderJSGadget.declareAcquiredMethod;
tmp_constructor.allowPublicAcquisition = tmp_constructor.allowPublicAcquisition =
...@@ -1247,6 +1298,7 @@ ...@@ -1247,6 +1298,7 @@
RenderJSGadget.call(this); RenderJSGadget.call(this);
}; };
tmp_constructor.declareMethod = RenderJSGadget.declareMethod; tmp_constructor.declareMethod = RenderJSGadget.declareMethod;
tmp_constructor.declareJob = RenderJSGadget.declareJob;
tmp_constructor.declareAcquiredMethod = tmp_constructor.declareAcquiredMethod =
RenderJSGadget.declareAcquiredMethod; RenderJSGadget.declareAcquiredMethod;
tmp_constructor.allowPublicAcquisition = tmp_constructor.allowPublicAcquisition =
...@@ -1380,6 +1432,8 @@ ...@@ -1380,6 +1432,8 @@
tmp_constructor.declareService = tmp_constructor.declareService =
RenderJSGadget.declareService; RenderJSGadget.declareService;
tmp_constructor.declareJob =
RenderJSGadget.declareJob;
tmp_constructor.onEvent = tmp_constructor.onEvent =
RenderJSGadget.onEvent; RenderJSGadget.onEvent;
tmp_constructor.declareAcquiredMethod = tmp_constructor.declareAcquiredMethod =
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
var gk = rJS(window), var gk = rJS(window),
ready_called = false, ready_called = false,
service_started = false, service_started = false,
job_started = false,
event_started = false; event_started = false;
gk.ready(function (g) { gk.ready(function (g) {
...@@ -24,9 +25,18 @@ ...@@ -24,9 +25,18 @@
.declareMethod('wasServiceStarted', function () { .declareMethod('wasServiceStarted', function () {
return service_started; return service_started;
}) })
.declareMethod('triggerJob', function () {
return this.runJob();
})
.declareMethod('wasEventStarted', function () { .declareMethod('wasEventStarted', function () {
return event_started; return event_started;
}) })
.declareMethod('wasJobStarted', function () {
return job_started;
})
.declareJob('runJob', function () {
job_started = true;
})
.declareMethod('canReportServiceError', function () { .declareMethod('canReportServiceError', function () {
return (this.aq_reportServiceError !== undefined); return (this.aq_reportServiceError !== undefined);
}) })
......
This diff is collapsed.
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