@eang/core
Version:
eang - model driven enterprise event processing
97 lines • 3.04 kB
JavaScript
export class EntityUpdateContext {
diff;
constructor(data) {
if (Array.isArray(data)) {
this.diff = data;
}
else if (data && typeof data === 'object' && 'diff' in data) {
this.diff = data.diff;
}
else {
this.diff = [];
}
}
static create(oldEntity, newData) {
const diff = EntityUpdateContext.createDiff(oldEntity, newData);
return new EntityUpdateContext(diff);
}
static createDiff(oldEntity, newData) {
const diffs = [];
// Only iterate over keys in newData (the values that are being changed)
for (const key in newData) {
if (Object.prototype.hasOwnProperty.call(newData, key)) {
const oldValue = oldEntity[key];
const newValue = newData[key];
// Only include fields that have actually changed
if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
diffs.push({
field: key,
oldValue: oldValue,
newValue: newValue
});
}
}
}
return diffs;
}
/**
* Apply the update context to an entity by updating its fields with the new values
* @param entity - The entity to apply the updates to
*/
applyDiffTo(entity) {
for (const d of this.diff) {
;
entity[d.field] = d.newValue;
}
}
}
export class FunctionStartContext {
rootObjectId;
entities;
data;
inIds;
outIds;
functionInputMapping;
inEventInstances;
constructor(contextOpts) {
Object.assign(this, contextOpts);
if (contextOpts.rootObjectId !== undefined) {
this.rootObjectId = contextOpts.rootObjectId;
}
this.data = contextOpts.data || {};
this.entities = contextOpts.entities || [];
this.inEventInstances = this.entities.filter((obj) => this.inIds?.includes(obj.id) && obj.typeOf === 'Event_Instance');
}
/**
* Gets the root object that owns this context
* @returns The root object or undefined if not found
*/
getRootObject() {
return this.entities?.find((obj) => obj.id === this.rootObjectId);
}
}
export class FunctionStopContext {
data;
err;
entityEvents;
events;
errEvents;
constructor(contextOpts) {
if (contextOpts?.data !== undefined) {
this.data = contextOpts.data;
}
if (contextOpts?.err !== undefined) {
this.err = contextOpts.err;
}
if (contextOpts?.entityEvents !== undefined) {
this.entityEvents = contextOpts.entityEvents;
}
if (contextOpts?.events !== undefined) {
this.events = contextOpts.events;
}
if (contextOpts?.errEvents !== undefined) {
this.errEvents = contextOpts.errEvents;
}
}
}
//# sourceMappingURL=context.js.map