forked from rkaw92/esdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.js
61 lines (53 loc) · 1.73 KB
/
Timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @module esdf/core/Timer
*/
var EventSourcedAggregate = require('./EventSourcedAggregate').EventSourcedAggregate;
var Event = require('./Event').Event;
var util = require('util');
/**
* An aggregate representing a timer. A timer is a simple object with only two behaviours - creation and firing.
* An external component is responsible for creating and firing timers at the appropriate time.
* @constructor
* @extends esdf/core/EventSourcedAggregate~EventSourcedAggregate
*/
function Timer(){
this._created = false;
this._fired = false;
this._fireAt = null;
this._applicationIdentifier = '';
this._timerMetadata = {};
}
util.inherits(Timer, EventSourcedAggregate);
Timer.prototype._aggregateType = 'Timer';
Timer.prototype.onTimerCreated = function onTimerCreated(event){
this._created = true;
this._fireAt = new Date(event.eventPayload.fireAt);
this._timerMetadata = event.eventPayload.timerMetadata;
this._applicationIdentifier = event.eventPayload.applicationIdentifier;
};
Timer.prototype.onTimerFired = function onTimerFired(event){
this._fired = true;
};
Timer.prototype.create = function create(applicationIdentifier, fireAt, timerMetadata){
if(this._created){
return;
}
this._stageEvent(new Event('TimerCreated', {
applicationIdentifier: applicationIdentifier,
fireAt: new Date(fireAt),
timerMetadata: timerMetadata || {}
}));
};
Timer.prototype.fire = function fire(){
if(this._fired){
return;
}
this._stageEvent(new Event('TimerFired', {}));
};
Timer.prototype._enrichEvent = function _enrichEvent(event){
if(event.eventType === 'TimerFired'){
event.eventPayload.timerMetadata = this._timerMetadata;
event.eventPayload.applicationIdentifier = this._applicationIdentifier;
}
};
module.exports.Timer = Timer;