entangle.ts
Version:
A declarative, event-driven framework for orchestrating business logic in TypeScript & Node.js applications.
64 lines (63 loc) • 2.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InteractionBuilder = void 0;
class InteractionBuilder {
constructor(parent, event,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initialInteraction = {}) {
this.parent = parent;
this.event = event;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.interaction = {};
this.interaction = Object.assign(Object.assign({}, initialInteraction), { upon: this.event });
}
use(target) {
this.interaction.use = target;
return this;
}
/**
* Specifies the method to be called on the target particle.
* @param method The name of a method that exists on the target particle.
* @returns A NEW, more specific, InteractionBuilder instance.
*/
call(method) {
return new InteractionBuilder(this.parent, this.event, Object.assign(Object.assign({}, this.interaction), { call: method }));
}
/**
* Provides the arguments for the method specified in `.call()`.
* The arguments are strongly typed based on the chosen method.
*/
with(...args) {
this.interaction.with = args;
return this;
}
emit(event) {
this.interaction.emit = event;
return this;
}
requirements(events) {
this.interaction.requirements = events;
return this;
}
once() {
this.interaction.once = true;
return this;
}
entanglement(entanglement) {
this.interaction.entanglement = entanglement;
return this;
}
catch(errorHandler) {
this.interaction.errorHandler = errorHandler;
return this;
}
then(callback) {
this.interaction.then = callback;
if (!this.interaction.use || !this.interaction.call || !this.interaction.entanglement) {
throw new Error('Invalid interaction provided. You must call "use", "call" and "entanglement" methods before calling "then"');
}
this.parent.addInteraction(this.interaction);
return this.parent;
}
}
exports.InteractionBuilder = InteractionBuilder;