UNPKG

@mdf.js/core

Version:

MMS - API Core - Common types, classes and functions

74 lines 2.6 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RunningState = void 0; const crash_1 = require("@mdf.js/crash"); const Error_1 = require("./Error"); const Stopped_1 = require("./Stopped"); /** * Provider Running state * @category State * @public */ class RunningState { /** * Create a instance of the running instance * @param instance - Port instance * @param changeState - Provider state change function * @param manageError - Provider error management function function */ constructor(instance, changeState, manageError) { this.instance = instance; this.changeState = changeState; this.manageError = manageError; /** Actual provider state */ this.state = 'running'; /** Handler for disconnected or unhealthy events */ this.errorEventHandler = (error) => this.fail(error); this.instance.once('closed', this.errorEventHandler); this.instance.once('unhealthy', this.errorEventHandler); } /** Stop the process: internal jobs, external dependencies connections ... */ async stop() { this.cleanEventHandlers(); try { await this.instance.stop(); } catch (error) { await this.fail(crash_1.Crash.from(error)); throw error; } this.changeState(new Stopped_1.StoppedState(this.instance, this.changeState, this.manageError)); } /** Initialize the process: internal jobs, external dependencies connections ... */ async start() { try { await this.instance.start(); } catch (error) { await this.fail(crash_1.Crash.from(error)); throw error; } } /** * Go to error state: waiting for new state o auto-fix de the problems * @param error - incoming error from provider */ async fail(error) { this.cleanEventHandlers(); this.manageError(error); this.changeState(new Error_1.ErrorState(this.instance, this.changeState, this.manageError)); } /** Clean event handlers for error state */ cleanEventHandlers() { this.instance.off('closed', this.errorEventHandler); this.instance.off('unhealthy', this.errorEventHandler); } } exports.RunningState = RunningState; //# sourceMappingURL=Running.js.map