UNPKG

@othree.io/chisel

Version:

Event sourcing made easy

37 lines 2.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handle = void 0; const optional_1 = require("@othree.io/optional"); const cerillo_1 = require("@othree.io/cerillo"); const handle = (loadState) => (handleCommand) => (calculateNewState) => (publishEvent) => (command) => __awaiter(void 0, void 0, void 0, function* () { return (yield loadState((0, optional_1.Optional)('contextId' in command ? command.contextId : undefined))) .mapAsync((state) => __awaiter(void 0, void 0, void 0, function* () { return (yield handleCommand(state.state, command)) .mapAsync((events) => __awaiter(void 0, void 0, void 0, function* () { const theEvents = (0, cerillo_1.match)(events) .when(_ => Array.isArray(_)).then(_ => _) .default(_ => ([_])) .get(); return (yield calculateNewState(command, state, theEvents)) .mapAsync((newState) => __awaiter(void 0, void 0, void 0, function* () { const notifications = newState.newEvents.map((event) => __awaiter(void 0, void 0, void 0, function* () { return publishEvent(event, newState.state); })); yield Promise.allSettled(notifications); return { state: newState.state, events: newState.newEvents }; })); })); })); }); exports.handle = handle; //# sourceMappingURL=actor.js.map