entangle.ts
Version:
A declarative, event-driven framework for orchestrating business logic in TypeScript & Node.js applications.
74 lines (73 loc) • 2.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParticleContractBuilder = void 0;
class ParticleContractBuilder {
constructor(parent, _event, _when, _is) {
this.parent = parent;
this._event = _event;
this._when = _when;
this._is = _is;
this.contract = {};
this.contract.upon = this._event;
this.contract.when = this._when;
this.contract.is = this._is;
}
build(particleClass) {
this.contract.build = particleClass;
return this;
}
inScope(scope) {
this.contract.scope = scope;
return this;
}
using(...args) {
this.contract.using = args;
return this;
}
emit(event) {
this.contract.emit = event;
return this;
}
requirements(events) {
this.contract.requirements = events;
return this;
}
lifecycle(lifecycle) {
this.contract.lifecycle = lifecycle;
return this;
}
destroyOnInteraction(shouldDestroy) {
this.contract.destroyOnInteraction = shouldDestroy;
return this;
}
when(notationString) {
this.contract.when = notationString;
return this;
}
is(value) {
this.contract.is = value;
return this;
}
once() {
this.contract.once = true;
return this;
}
entanglement(entanglement) {
this.contract.entanglement = entanglement;
return this;
}
catch(errorHandler) {
this.contract.errorHandler = errorHandler;
return this;
}
then(callback) {
// If both upon and build are missing, we cannot create a particle
if (!this.contract.upon || !this.contract.build || !this.contract.entanglement) {
throw new Error('Missing required properties');
}
this.contract.then = callback;
this.parent.addContract(this.contract);
return this.parent;
}
}
exports.ParticleContractBuilder = ParticleContractBuilder;