Observer Pattern:
var observer = new Observer();
var eventA = new EventA();
var eventB = new EventB();
eventA.register(observer, function() {
// set off alarm in N amount of time units
});
eventB.register(observer, function() {
// reset alarm
});
Then later on eventA
and eventB
will call notify()
for all registered observers, which will trigger the callbacks for starting alarm countdown / reseting alarm.
Code is in pesudo-javascript. If you are using full out javascript, simply use setTimeout
and clearTimeout
in the callbacks.
I guess I should mention that a Pub/Sub pattern (which Observer is a subset of) is perfectly good as well.