UNPKG

@node-ts/bus-workflow

Version:

A workflow engine for orchestrating logic flows in distributed applications.

62 lines 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryPersistence = void 0; const tslib_1 = require("tslib"); const workflow_data_1 = require("../workflow-data"); const inversify_1 = require("inversify"); const logger_core_1 = require("@node-ts/logger-core"); /** * A non-durable in-memory persistence for storage and retrieval of workflow data. Before using this, * be warned that all workflow data will not survive a process restart or application shut down. As * such this should only be used for testing, prototyping or handling unimportant workflows. */ let InMemoryPersistence = class InMemoryPersistence { constructor(logger) { this.logger = logger; this.workflowData = {}; } async initializeWorkflow(workflowDataConstructor, _) { this.workflowData[workflowDataConstructor.name] = []; } async getWorkflowData(workflowDataConstructor, messageMap, message, messageOptions, includeCompleted) { const filterValue = messageMap.lookupMessage(message, messageOptions); if (!filterValue) { return []; } const workflowDataName = workflowDataConstructor.name; const workflowData = this.workflowData[workflowDataName]; if (!workflowData) { this.logger.error('Workflow data not initialized', { workflowDataName }); } return workflowData .filter(data => (includeCompleted || data.$status === workflow_data_1.WorkflowStatus.Running) && data[messageMap.workflowDataProperty] === filterValue); } async saveWorkflowData(workflowData) { const workflowDataName = workflowData.constructor.name; const existingWorkflowData = this.workflowData[workflowDataName]; const existingItem = existingWorkflowData.find(d => d.$workflowId === workflowData.$workflowId); if (existingItem) { try { Object.assign(existingItem, workflowData); } catch (err) { this.logger.error('Unable to update data', { err }); throw err; } } else { existingWorkflowData.push(workflowData); } } length(workflowDataConstructor) { return this.workflowData[workflowDataConstructor.name].length; } }; InMemoryPersistence = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(logger_core_1.LOGGER_SYMBOLS.Logger)), tslib_1.__metadata("design:paramtypes", [Object]) ], InMemoryPersistence); exports.InMemoryPersistence = InMemoryPersistence; //# sourceMappingURL=in-memory-persistence.js.map