-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEvent.js
42 lines (37 loc) · 2.09 KB
/
Event.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
/**
* @module esdf/core/Event
*/
var uuid = require('uuid');
/**
* Construct a new Event instance. Events are the basic (and only!) carrier of the application's state changes, and thus, of system state.
* @constructor
* @param {String} eventType The event type. This is a textual representation of the kind of event that has occured.
* @param {Object} eventPayload The data about the event that took place. Most events need some attributes to specify exactly what happened. This is usually a derivative of some action's input parameters.
* @param {Object} [metadata] Additional key-value pairs, separated from the payload in application processing logic. Usually contains, among others, the actual command parameters that were used to generate this event, for tracing purposes.
* @param {String} [aggregateID] ID of the originating Aggregate. This is typically omitted when originally creating the event - by default, Aggregates will overwrite this ID in _stageEvent automatically.
* @param {String} [eventID] The event ID to initialize the event with. Useful when reconstructing events from serialized storage (e.g. a DB).
*/
function Event(eventType, eventPayload, eventID){
this.eventType = eventType;
this.eventPayload = eventPayload;
this.eventID = (typeof(eventID) !== 'undefined') ? eventID : uuid.v4();
//TODO: methods
}
/**
* Encode the event into a string.
* This uses JSON, similarly to Commit.
* @returns {string} The encoded form of the Event, suitable for recovery via static method Event.fromString.
*/
Event.prototype.toString = function toString(){
return JSON.stringify(this);
};
/**
* Rebuild an event from its flattened object form (i.e. from an object that has been serialized and/or had its methods stripped).
* @param {Object} flattenedEvent The object to reconstruct from. Will take its properties and construct a new Event based on them.
* @returns {Event}
*/
//TODO: test coverage of reconstruct()
Event.reconstruct = function reconstruct(flattenedEvent){
return new Event(flattenedEvent.eventType, flattenedEvent.eventPayload, flattenedEvent.eventID);
};
module.exports.Event = Event;