Skip to content

Commit

Permalink
Added queueing of trigger calls until all event modules are initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronius committed Nov 5, 2018
1 parent 4adb292 commit 6438549
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
57 changes: 56 additions & 1 deletion src/__tests__/initRules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,61 @@ describe('initRules', function() {

expect(actionExport.calls.count()).toBe(0);
});

it('queues trigger calls that occur before all event ' +
'modules have been initialized', function() {
var rules = setupRules([
{
events: [
generateEvent(
'Event1',
function(module) {
module.exports = function(settings, trigger) {
debugger;
trigger();
};
}
)
],
actions: [
generateAction(
'Action1',
function(module) {
module.exports = jasmine.createSpy()
}
)
]
},
{
events: [
generateEvent(
'Event2',
function(module) {
module.exports = jasmine.createSpy();
}
)
]
}
]);

initRules(
_satellite,
rules,
moduleProvider,
replaceTokens,
getShouldExecuteActions
);

var action1Export = moduleProvider.getModuleExports(
moduleHelper.getPath('Action1')
);

var event2Export = moduleProvider.getModuleExports(
moduleHelper.getPath('Event2')
);

expect(event2Export).toHaveBeenCalledBefore(action1Export);
});
});

describe('error handling and logging', function() {
Expand Down Expand Up @@ -2489,7 +2544,7 @@ describe('initRules', function() {
rule: rules[0],
action: rules[0].actions[0]
});

done();
});
}
Expand Down
27 changes: 24 additions & 3 deletions src/initRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ module.exports = function(
}
};

var eventModulesInitialized = false;
var triggerCallQueue = [];

var initEventModule = function(ruleEventPair) {
var rule = ruleEventPair.rule;
var event = ruleEventPair.event;
Expand Down Expand Up @@ -268,19 +271,32 @@ module.exports = function(
* that occurred.
*/
var trigger = function(syntheticEvent) {
// DTM-11871
// If we're still in the process of initializing event modules,
// we need to queue up any calls to trigger, otherwise if the triggered
// rule does something that triggers a different rule whose event module
// has not been initialized, that secondary rule will never get executed.
// This can be removed if we decide to always use the rule queue, since
// conditions and actions will be processed asynchronously, which
// would give time for all event modules to be initialized.
if (!eventModulesInitialized) {
triggerCallQueue.push(trigger.bind(null, syntheticEvent));
return;
}

notifyMonitors('ruleTriggered', {
rule: rule
});

var normalizedSyntethicEvent = normalizeSyntheticEvent(
var normalizedSyntheticEvent = normalizeSyntheticEvent(
syntheticEventMeta,
syntheticEvent
);

if (isRuleQueueActive()) {
addRuleToQueue(rule, normalizedSyntethicEvent);
addRuleToQueue(rule, normalizedSyntheticEvent);
} else {
checkConditions(rule, normalizedSyntethicEvent);
checkConditions(rule, normalizedSyntheticEvent);
}
};

Expand All @@ -291,6 +307,11 @@ module.exports = function(
};

buildRuleExecutionOrder(rules).forEach(initEventModule);
eventModulesInitialized = true;
triggerCallQueue.forEach(function(triggerCall) {
triggerCall();
});
triggerCallQueue = null;

// We are returing the promise chain only for testing purposes.
return lastPromiseInQueue;
Expand Down

0 comments on commit 6438549

Please sign in to comment.