@fraktalio/fmodel-ts
Version:
Functional domain modeling with TypeScript. Optimized for event sourcing and CQRS
65 lines • 3.66 kB
JavaScript
/*
* Copyright 2023 Fraktalio D.O.O. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "
* AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
/**
* `Saga` is a datatype that represents the central point of control deciding what to execute next `A`.
* It is responsible for mapping different events from aggregates into action results `AR` that the `Saga` then can use to calculate the next actions `A` to be mapped to command of other aggregates.
*
* @typeParam AR - Action Result type
* @typeParam A - Action type
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
export class Saga {
/**
* @constructor Creates `Saga`
* @param react - A function/lambda that takes input state of type `AR`, and returns the list of actions `A[]`>.
*/
constructor(react) {
this.react = react;
}
/**
* Left map on `AR`/ActionResult parameter - Contravariant
*
* @typeParam ARn - New Action Result
*/
mapContraOnActionResult(f) {
return new Saga((arn) => this.react(f(arn)));
}
/**
* Right map on `A`/Action parameter - Covariant
*
* @typeParam An - New Action
*/
mapOnAction(f) {
return new Saga((ar) => this.react(ar).map(f));
}
/**
* Combines two choreography sagas into one orchestrating Saga
*
* @param saga2 - second Saga
*/
combine(saga2) {
const sagaX = this.mapContraOnActionResult((en) => en).mapOnAction(identity);
const sagaY = saga2
.mapContraOnActionResult((en) => en)
.mapOnAction(identity);
return new Saga((ar) => sagaX.react(ar).concat(sagaY.react(ar)));
}
}
/**
* Identity function
*
* @param t - some type
*/
const identity = (t) => t;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2FnYS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9saWIvZG9tYW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