Merge pull request #77 from nmaier/race

Fix subscriptions race.
This commit is contained in:
hamza zia 2014-01-09 04:08:43 -08:00
commit 447f60c875

View File

@ -38,7 +38,11 @@ function(syscall, time, alerts, utils, rootScope, uri) {
clearTimeout(timeout); clearTimeout(timeout);
timeout = null; timeout = null;
if (!subscriptions.length) { subscriptions = _.filter(subscriptions, function(e) {
return !!e && e.once !== 2;
});
var subs = subscriptions.slice();
if (!subs.length) {
timeout = setTimeout(update, time); timeout = setTimeout(update, time);
return; return;
} }
@ -48,15 +52,13 @@ function(syscall, time, alerts, utils, rootScope, uri) {
syscall.init(currentConf); syscall.init(currentConf);
} }
subscriptions = _.filter(subscriptions, function(e) { return !!e }); var params = _.map(subs, function(s) {
var params = _.map(subscriptions, function(s) {
return { return {
methodName: s.name, methodName: s.name,
params: s.params && s.params.length ? s.params : undefined params: s.params && s.params.length ? s.params : undefined
}; };
}); });
syscall.invoke({ syscall.invoke({
name: 'system.multicall', name: 'system.multicall',
params: [params], params: [params],
@ -73,15 +75,16 @@ function(syscall, time, alerts, utils, rootScope, uri) {
var cbs = []; var cbs = [];
_.each(data.result, function(d, i) { _.each(data.result, function(d, i) {
var handle = subscriptions[i]; var handle = subs[i];
if (handle) { if (handle) {
if (d.code) { if (d.code) {
console.error(handle, d);
alerts.addAlert(d.message, 'error'); alerts.addAlert(d.message, 'error');
} }
// run them later as the cb itself can mutate the subscriptions // run them later as the cb itself can mutate the subscriptions
cbs.push({cb: handle.cb, data: d}); cbs.push({cb: handle.cb, data: d});
if (handle.once) { if (handle.once) {
subscriptions[i] = null; handle.once = 2;
} }
} }
}); });