ddd-es-node
Advanced tools
Comparing version 0.0.18 to 0.0.19
{ | ||
"name": "ddd-es-node", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "ddd-es-node", | ||
@@ -5,0 +5,0 @@ "main": "./dist/src/index.js", |
@@ -19,7 +19,7 @@ # ddd-es-node | ||
constructor(id) { | ||
super(id, Entity.CONFIG((self, event) => { | ||
super(id, (self, event) => { | ||
if(event instanceof OrderCancelledEvent) { | ||
this.cancelled = true; | ||
} | ||
})); | ||
}); | ||
} | ||
@@ -26,0 +26,0 @@ cancel() { |
@@ -12,6 +12,7 @@ import { | ||
constructor(id) { | ||
super(id, Entity.CONFIG((self, event) => { | ||
super(id, (self, event) => { | ||
self.lastEventReceived = event; | ||
})); | ||
}); | ||
} | ||
doSomething() { | ||
@@ -23,6 +24,6 @@ this.dispatch(this.id, new TestEvent()); | ||
class TestEntitySuperclass extends Entity { | ||
constructor(id, eventProcessor) { | ||
super(id, Entity.CONFIG((self, event) => { | ||
constructor(id, eventHandler) { | ||
super(id, (self, event) => { | ||
self.lastEventReceivedFromSuperclass = event; | ||
}).apply(eventProcessor)); | ||
}, eventHandler); | ||
} | ||
@@ -65,3 +66,3 @@ } | ||
afterEach(() => { | ||
while(events.length > 0) { | ||
while (events.length > 0) { | ||
events.pop(); | ||
@@ -68,0 +69,0 @@ } |
@@ -8,38 +8,24 @@ import { | ||
export class EventProcessor { | ||
private handler : EventHandler; | ||
public apply(newHandler : EventHandler) : EventProcessor { | ||
const parentHandler : EventHandler = this.handler; | ||
let handlerToSet : EventHandler = newHandler; | ||
if (parentHandler) { | ||
handlerToSet = (entity : Entity, event : EntityEvent) : void => { | ||
newHandler(entity, event); | ||
parentHandler(entity, event); | ||
}; | ||
const composeHandlers = (...handlers : EventHandler[]) : EventHandler => { | ||
if (handlers.length === 1) { | ||
return handlers[0]; | ||
} | ||
return (entity : Entity, event : EntityEvent) => { | ||
for (let i = handlers.length - 1; i >= 0; --i) { | ||
handlers[i](entity, event); | ||
} | ||
this.handler = handlerToSet; | ||
return this; | ||
} | ||
}; | ||
}; | ||
public accept(entity : Entity, event : EntityEvent) : void { | ||
this.handler(entity, event); | ||
} | ||
} | ||
export class Entity { | ||
protected id : string; | ||
protected config : EventProcessor; | ||
protected applier : EventHandler; | ||
protected dispatch : VoidEventDispatcher; | ||
constructor(id : string, | ||
config : EventProcessor) { | ||
...appliers : EventHandler[]) { | ||
this.id = id; | ||
this.config = config; | ||
this.applier = composeHandlers(...appliers); | ||
} | ||
public static CONFIG(newHandler : EventHandler) : EventProcessor { | ||
return new EventProcessor().apply(newHandler); | ||
} | ||
public init(dispatch : VoidEventDispatcher) : void { | ||
@@ -50,5 +36,5 @@ this.dispatch = dispatch; | ||
public apply(event : EntityEvent) : void { | ||
this.config.accept(this, event); | ||
this.applier(this, event); | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
94361
1908