node-sagas
Version:
Library for handling distributed transactions in the microservices architecture
48 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const exceptions_1 = require("./exceptions");
var SagaStates;
(function (SagaStates) {
SagaStates["New"] = "New";
SagaStates["InProgress"] = "In progress";
SagaStates["InCompensation"] = "In compensation";
SagaStates["Complete"] = "Complete";
SagaStates["CompensationComplete"] = "Compensation complete";
SagaStates["CompensationError"] = "Compensation error";
})(SagaStates = exports.SagaStates || (exports.SagaStates = {}));
class Saga {
constructor(sagaFlow) {
this.sagaFlow = sagaFlow;
this.state = SagaStates.New;
}
getState() {
return this.state;
}
async execute(params) {
this.state = SagaStates.InProgress;
try {
await this.sagaFlow.invoke(params);
this.state = SagaStates.Complete;
return params;
}
catch (e) {
this.state = SagaStates.InCompensation;
this.invokeError = e;
await this.runCompensationFlow(params);
throw new exceptions_1.SagaExecutionFailed(e);
}
}
async runCompensationFlow(params) {
try {
await this.sagaFlow.compensate(params);
this.state = SagaStates.CompensationComplete;
}
catch (e) {
this.state = SagaStates.CompensationError;
this.compensationError = e;
throw new exceptions_1.SagaCompensationFailed(e);
}
}
}
exports.Saga = Saga;
//# sourceMappingURL=saga.js.map