forked from mateodelnorte/sourced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.ts
389 lines (360 loc) · 10.8 KB
/
entity.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* jslint node: true */
import debug from "debug";
import EventEmitter from "eventemitter3";
import cloneDeep from "lodash.clonedeep";
const log = debug("sourced");
/**
* Extending native Error.
*
* @class {Function} EntityError
* @param {String} msg The error message.
*/
class EntityError extends Error {
constructor(...args) {
super(...args);
log("Entity Error: ", this);
Error.captureStackTrace(this, EntityError);
}
}
/**
* mergeProperties holds a map of entity types to properties.
*
* @see Entity.mergeProperty
* @see Entity.prototype.mergeProperty
* @static
*/
const mergeProperties = new Map();
/**
* Creates an event-sourced Entity.
*
* @class {Function} Entity
* @param {Object} [snapshot] A previously saved snapshot of an Entity.
* @param {Array} [events] An array of events to apply on instantiation.
* @requires eventemitter3
* @requires debug
* @requires lodash.clonedeep
* @license MIT
*/
export class Entity extends EventEmitter {
eventsToEmit: any[];
newEvents: any[];
replaying: boolean;
snapshotVersion: number;
timestamp: number;
version: number;
constructor() {
super();
/**
* [Description]
* @member {Array} eventsToEmit
* @todo discuss the use of this so it can be documented better.
*/
this.eventsToEmit = [];
/**
* [Description]
* @member {Array} newEvents
* @todo discuss the use of this so it can be documented better.
*/
this.newEvents = [];
/**
* Boolean to prevent emit, enqueue and digest from running during replay.
* @member {Boolean} replaying
*/
this.replaying = false;
/**
* Holds the version of the latest snapshot for the Entity.
* @member {Number} snapshotVersion
*/
this.snapshotVersion = 0;
/**
* Holds the event's timestamp in the Entity.
* @member {Number} timestamp
*/
this.timestamp = Date.now();
/**
* Holds the current version of the Entity.
* @member {Number} version
*/
this.version = 0;
}
/**
* Rehydrates by merging a snapshot, and replaying events on top.
*/
rehydrate(snapshot: any, events: any[]) {
log("rehydrating", this);
/**
* If a snapshot is provided, merge it.
*/
if (snapshot) {
this.merge(snapshot);
}
/**
* If events are also provided, replay them
*/
if (events) {
this.replay(events);
}
}
/**
* Wrapper around the EventEmitter.emit method that adds a condition so events
* are not fired during replay.
*/
emit(event: string | symbol, ...args: any[]): boolean {
if (!this.replaying) {
super.emit(event, ...args);
}
return true;
}
/**
* Add events to the queue of events to emit. If called during replay, this
* method does nothing.
*/
enqueue(...args: any[]) {
if (!this.replaying) {
this.eventsToEmit.push(args);
}
}
/**
* Digest a command with given data.This is called whenever you want to record
* a command into the events for the Entity. If called during replay, this
* method does nothing.
*
* @param {String} method the name of the method/command you want to digest.
* @param {Object} data the data that should be passed to the replay.
*/
digest(method: string, data: any) {
if (!this.replaying) {
this.timestamp = Date.now();
this.version = this.version + 1;
log(`digesting event ${method} w/ data`, { method, data });
this.newEvents.push({
method: method,
data: data,
timestamp: this.timestamp,
version: this.version,
});
}
}
/**
* Merge a snapshot onto the Entity.
*
* For every property passed in the snapshot, the value is deep-cloned and then
* merged into the instance through mergeProperty. See mergeProperty for details.
*
* @param {Object} snapshot snapshot object.
* @see Entity.mergeProperty
*/
merge(snapshot: any) {
log("merging snapshot", { snapshot });
for (const property in snapshot) {
const val = cloneDeep(snapshot[property]);
this.mergeProperty(property, val);
}
return this;
}
/**
* Merge a property onto the instance.
*
* Given a name and a value, mergeProperty checks first attempt to find the
* property in the mergeProperties map using the constructor name as key. If it
* is found and it is a function, the function is called. If it is NOT found
* we check if the property is an object. If so, we merge. If not, we simply
* assign the passed value to the instance.
*
* @param {String} name the name of the property being merged.
* @param {Object} value the value of the property being merged.
* @see mergeProperties
* @see Entity.mergeProperty
*/
mergeProperty(name: string, value: any) {
if (
mergeProperties.size &&
mergeProperties.has(Object.getPrototypeOf(this).constructor.name) &&
mergeProperties
.get(Object.getPrototypeOf(this).constructor.name)
.has(name) &&
typeof mergeProperties
.get(Object.getPrototypeOf(this).constructor.name)
.get(name) === "function"
) {
return mergeProperties
.get(Object.getPrototypeOf(this).constructor.name)
.get(name)
.call(this, value);
} else if (typeof value === "object" && typeof this[name] === "object") {
Object.assign(this, {
[name]: value,
});
} else {
this[name] = value;
}
}
/**
* Replay an array of events onto the instance.
*
* The goal here is to allow application of events without emitting, enqueueing
* nor digesting the replayed events. This is done by setting this.replaying to
* true which emit, enqueue and digest check for.
*
* If the method in the event being replayed exists in the instance, we call
* the mathod with the data in the event and set the version of the instance to
* the version of the event. If the method is not found, we attempt to parse the
* constructor to give a more descriptive error.
*
* @param {Array} events an array of events to be replayed.
*/
replay(events) {
this.replaying = true;
log("replaying events", { events });
events.forEach((event) => {
if (this[event.method]) {
this[event.method](event.data);
this.version = event.version;
} else {
const classNameRegexes = [
/function\s+(\w+)\s?\(/,
/class\s+(\w+)\s+extends?/,
];
const match = classNameRegexes.find((regex) =>
regex.test(this.constructor.toString())
);
const className = match.exec(this.constructor!.toString())[1];
const errorMessage = `method '${
event.method
}' does not exist on model '${className.trim()}'`;
throw new EntityError(errorMessage);
}
});
this.replaying = false;
}
/**
* Returns a clone of the entity's current state without the event sourcing properties
*
* Here the instance's snapshotVersion property is set to the current version,
* then the instance is deep-cloned and the clone is trimmed of the internal
* sourced attributes using trimSnapshot and returned.
*
* @returns {Object}
*/
state(): Omit<
this,
"eventsToEmit" | "newEvents" | "replaying" | "_events" | "_maxListeners"
> {
const state = cloneDeep(this);
return this.trimSnapshot(state);
}
/**
* Create a snapshot of the current state of the entity instance.
*
* Here the instance's snapshotVersion property is set to the current version,
* then the instance is deep-cloned and the clone is trimmed of the internal
* sourced attributes using trimSnapshot and returned.
*
* @returns {Object}
*/
snapshot(): Omit<
this,
"eventsToEmit" | "newEvents" | "replaying" | "_events" | "_maxListeners"
> {
this.snapshotVersion = this.version;
const snap = cloneDeep(this);
return this.trimSnapshot(snap);
}
/**
* Remove the internal sourced properties from the passed snapshot.
*
* Snapshots are to contain only entity data properties. This trims all other
* properties from the snapshot.
*
* @param {Object} snapshot the snapshot to be trimmed.
* @see Entity.prototype.snapshot
*/
trimSnapshot(
snapshot
): Omit<
this,
"eventsToEmit" | "newEvents" | "replaying" | "_events" | "_maxListeners"
> {
delete snapshot.eventsToEmit;
delete snapshot.newEvents;
delete snapshot.replaying;
delete snapshot._events;
delete snapshot._maxListeners;
return snapshot;
}
/**
* Helper function to automatically create a method that calls digest on the
* param provided. Use it to add methods that automatically call digest.
*
* @param {Object} type the entity class to which the method will be added.
* @param {Function} fn the actual function to be added.
* @example
*
* Entity.digestMethod(Car, function clearSettings (param) {
*
* const self = this;
*
* this.settings.get(param.name).forEach((name, config) => {
*
* config.sources.forEach((source) => {
*
* source.remove();
*
* });
*
* });
*
* return this.settings;
*
* });
*
*/
static digestMethod(type: any, fn: any) {
if (!fn.name)
throw new EntityError(
"Anonmyous functions are not allowed in digest method definitions. Please provide a function name"
);
type.prototype[fn.name] = function (...args: any[]) {
const digestArgs = Array.prototype.slice.call(args);
digestArgs.unshift(fn.name);
Entity.prototype.digest.apply(this, digestArgs);
const methodArgs = Array.prototype.slice.call(args);
return fn.apply(this, methodArgs);
};
}
/**
* Convenience function to store references to functions that should be run
* when merging a particular property.
*
* @param {Object} type the entity class to which the property->fn belongs to.
* @param {String} name the name of the property that holds the fn.
* @param {Function} fn the function to execute when merging the property.
* @see mergeProperties
* @example
* function Wheel (status) {
* this.status = status;
* }
*
* Wheel.prototype.go = function () {
* this.status = 'going';
* }
*
* function Car () {
* this.id = null;
* this.wheel = new Wheel(); // for instantiating our default wheel, when we first 'new' up a Car
*
* Entity.apply(this, arguments);
* }
*
* util.inherits(Car, Entity);
*
* Entity.mergeProperty(Car, 'wheels', function (obj) {
* this.wheel = new Wheel(); // for instantiating our wheel from saved values in a database
* });
*/
static mergeProperty(type, name, fn) {
if (!mergeProperties.has(type.name))
mergeProperties.set(type.name, new Map());
mergeProperties.get(type.name).set(name, fn);
}
}