@fraktalio/fmodel-ts
Version:
Functional domain modeling with TypeScript. Optimized for event sourcing and CQRS
56 lines • 3.18 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SagaManager = void 0;
/**
* Saga manager - Stateless process orchestrator.
*
* It is reacting on Action Results of type `AR` and produces new actions `A` based on them.
*
* @typeParam AR - Action Result of type `AR`
* @typeParam A - Action of type `A` that are going to be published downstream
* @typeParam ARM - Action Result metadata
* @typeParam AM - Action metadata
*
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
class SagaManager {
/**
*
* @param saga - A saga component of type `ISaga`<`AR`, `A`>
* @param actionPublisher - Interface for Action publishing
*/
constructor(saga, actionPublisher) {
this.saga = saga;
this.actionPublisher = actionPublisher;
}
react(actionResult) {
return this.saga.react(actionResult);
}
async publish(actions) {
return this.actionPublisher.publish(actions);
}
/**
* Handles the action result with metadata of type `AR & ARM`
*
* @param actionResult - Action Result represent the outcome of some action you want to handle in some way
* @return list of Actions with Metadata of type `A & AM`
*/
async handle(actionResult) {
const actions = this.saga.react(actionResult);
return this.actionPublisher.publish(actions.map((a) => (Object.assign(Object.assign({}, a), actionResult))));
}
}
exports.SagaManager = SagaManager;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2FnYS1tYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9hcHBsaWNhdGlvbi9zYWdhLW1hbmFnZXIudHMiXSwibmFtZXMiOltdLC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