yoonite-saga
Version:
> Orchestration de workflows transactionnels avec gestion de compensation (pattern Saga)
61 lines • 1.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SagaBuilder = void 0;
class SagaBuilder {
constructor(config = {}) {
this.index = null;
this.steps = [];
this.debug = config.debug || false;
}
step(name) {
this.index = this.index === null ? 0 : this.index + 1;
const step = {
name,
validate: null,
condition: null,
invokes: [],
withCompensation: null,
debug: this.debug,
};
this.steps = [...this.steps, step];
return this;
}
invoke(name, fn) {
const realFn = typeof name === "string" ? fn : name;
const realName = typeof name === "string" ? name : `Anonymous invoke`;
const step = this.steps[this.index];
if (typeof realFn === "function") {
step.invokes.push({
name: realName,
validate: null,
condition: null,
action: realFn,
withCompensation: null,
});
}
else {
step.invokes.push(realFn);
}
return this;
}
validate(dto) {
const step = this.steps[this.index];
step.validate = dto;
return this;
}
condition(fn) {
const step = this.steps[this.index];
step.condition = fn;
return this;
}
withCompensation(fn) {
const step = this.steps[this.index];
step.withCompensation = fn;
return this;
}
build() {
return this.steps;
}
}
exports.SagaBuilder = SagaBuilder;
//# sourceMappingURL=saga-builder.js.map