@fraktalio/fmodel-ts
Version:
Functional domain modeling with TypeScript. Optimized for event sourcing and CQRS
52 lines • 3.04 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 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
*/
export 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) => ({ ...a, ...actionResult })));
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2FnYS1tYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9hcHBsaWNhdGlvbi9zYWdhLW1hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7Ozs7O0dBV0c7QUErQ0g7Ozs7Ozs7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