Commit 164e473d authored by Gabriel Monnerat's avatar Gabriel Monnerat

propagate cancel message through all cancel methods

parent be266052
......@@ -113,18 +113,18 @@
prevent_default = true;
}
function cancelResolver() {
function cancelResolver(msg) {
if ((callback_promise !== undefined) &&
(typeof callback_promise.cancel === "function")) {
callback_promise.cancel();
callback_promise.cancel(msg);
}
}
function canceller() {
function canceller(msg) {
if (handle_event_callback !== undefined) {
target.removeEventListener(type, handle_event_callback, useCapture);
}
cancelResolver();
cancelResolver(msg);
}
function itsANonResolvableTrap(resolve, reject) {
var result;
......@@ -134,7 +134,9 @@
evt.preventDefault();
}
cancelResolver();
cancelResolver(
"Cancelling previous event (" + evt.type + ")"
);
try {
result = callback(evt);
......@@ -146,7 +148,7 @@
.push(undefined, function handleEventCallbackError(error) {
// Prevent rejecting the loop, if the result cancelled itself
if (!(error instanceof RSVP.CancellationError)) {
canceller();
canceller(error.toString());
reject(error);
}
});
......@@ -442,11 +444,11 @@
return new Monitor();
}
function canceller() {
function canceller(msg) {
var len = promise_list.length,
i;
for (i = 0; i < len; i += 1) {
promise_list[i].cancel();
promise_list[i].cancel(msg);
}
// Clean it to speed up other canceller run
promise_list = [];
......@@ -460,17 +462,17 @@
monitor.isRejected = true;
monitor.rejectedReason = rejectedReason;
resolved = true;
canceller();
canceller(rejectedReason.toString());
return fail(rejectedReason);
};
}, canceller);
monitor.cancel = function cancelMonitor() {
monitor.cancel = function cancelMonitor(msg) {
if (resolved) {
return;
}
resolved = true;
promise.cancel();
promise.cancel(msg);
promise.fail(function rejectMonitorPromise(rejectedReason) {
monitor.isRejected = true;
monitor.rejectedReason = rejectedReason;
......@@ -529,7 +531,11 @@
function deleteGadgetMonitor(g) {
if (g.hasOwnProperty('__monitor')) {
g.__monitor.cancel();
g.__monitor.cancel(
"Deleting Gadget Monitor " + "(" +
g.element.dataset.gadgetUrl +
")"
);
delete g.__monitor;
g.__job_list = [];
}
......@@ -654,7 +660,7 @@
function runJob(gadget, name, callback, argument_list) {
if (gadget.__job_dict.hasOwnProperty(name)) {
gadget.__job_dict[name].cancel();
gadget.__job_dict[name].cancel(name + " : Cancelling previous job");
}
var job_promise = ensurePushableQueue(callback, argument_list, gadget);
gadget.__job_dict[name] = job_promise;
......
......@@ -3267,6 +3267,108 @@
});
});
test('check cancellation message after trigger event twice', function () {
var called = false,
gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test600.html';
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
document.getElementById('qunit-fixture').innerHTML = "<div></div>";
stop();
expect(1);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
Klass.onEvent('bar', function () {
return new RSVP.Promise(function () {
return;
}, function (error) {
if (called) {
return;
}
called = true;
equal(error, "Cancelling previous event (bar)");
});
});
return gadget.declareGadget(
html_url,
{element: document.getElementById('qunit-fixture')
.querySelector("div")}
);
})
.then(function () {
var event = new Event("bar");
document.getElementById('qunit-fixture').querySelector("div")
.dispatchEvent(event);
document.getElementById('qunit-fixture').querySelector("div")
.dispatchEvent(event);
})
.always(function () {
start();
});
});
test('check message after delete gadget', function () {
var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test601.html';
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
document.getElementById('qunit-fixture').innerHTML = "<div></div>";
stop();
expect(2);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
Klass.declareJob("runJob1", function () {
return new RSVP.Promise(function () {
return RSVP.delay(20);
}, function (error) {
equal(
error,
"Deleting Gadget Monitor " +
"(https://example.org/files/qunittest/test601.html)"
);
});
});
Klass.onEvent('bar', function () {
return new RSVP.Promise(function () {
return;
}, function (error) {
equal(
error,
"Deleting Gadget Monitor " +
"(https://example.org/files/qunittest/test601.html)"
);
});
});
return gadget.declareGadget(
html_url,
{element: document.getElementById('qunit-fixture')
.querySelector("div")}
);
})
.then(function (g) {
var event = new Event("bar");
document.getElementById('qunit-fixture').querySelector("div")
.dispatchEvent(event);
g.runJob1();
})
.then(function () {
document
.getElementById('qunit-fixture')
.innerHTML = "";
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// RenderJSGadgetKlass.onLoop
/////////////////////////////////////////////////////////////////
......@@ -3578,6 +3680,91 @@
});
});
test('job called twice propage error message', function () {
var g,
gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test502.html';
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
document.getElementById('qunit-fixture').innerHTML = "<div></div>";
stop();
expect(1);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
Klass.declareJob("runJob1", function (parameter) {
return new RSVP.Promise(function () {
return RSVP.delay(20);
}, function (error) {
if (parameter === "first") {
equal(error, "runJob1 : Cancelling previous job");
}
});
});
return gadget.declareGadget(
html_url,
{element: document.getElementById('qunit-fixture')
.querySelector("div")}
);
})
.then(function (result) {
g = result;
g.runJob1("first");
g.runJob1("second");
})
.always(function () {
start();
});
});
test('cancel jobs with custom message', function () {
var gadget = new RenderJSGadget(),
html_url = 'https://example.org/files/qunittest/test502.html';
gadget.__sub_gadget_dict = {};
this.server.respondWith("GET", html_url, [200, {
"Content-Type": "text/html"
}, "<html><body></body></html>"]);
document.getElementById('qunit-fixture').innerHTML = "<div></div>";
stop();
expect(2);
renderJS.declareGadgetKlass(html_url)
.then(function (Klass) {
return gadget.declareGadget(
html_url,
{element: document.getElementById('qunit-fixture')
.querySelector("div")}
);
})
.then(function (result) {
var all,
msg = "Cancel RSVP.all";
all = RSVP.all([
RSVP.Promise(function () {
return;
}, function (error) {
equal(error, msg);
}),
RSVP.Promise(function () {
return;
}, function (error) {
equal(error, msg);
})
]);
all.cancel("Cancel RSVP.all");
})
.always(function () {
start();
});
});
test('job triggered when gadget element added in DOM', function () {
// Subclass RenderJSGadget to not pollute its namespace
var service1 = {},
......@@ -6934,4 +7121,3 @@
}(document, renderJS, QUnit, sinon, URI, URL, Event,
MutationObserver, RSVP));
\ No newline at end of file
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