@mdf.js/core
Version:
MMS - API Core - Common types, classes and functions
72 lines • 2.47 kB
JavaScript
"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.StoppedState = void 0;
const crash_1 = require("@mdf.js/crash");
const Error_1 = require("./Error");
const Running_1 = require("./Running");
/**
* Provider Stopped state
* @category State
* @public
*/
class StoppedState {
/**
* Create a instance of the stopped 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 = 'stopped';
/** Handler for disconnected or unhealthy events */
this.errorEventHandler = (error) => this.fail(error);
this.instance.once('unhealthy', this.errorEventHandler);
}
/** Stop the process: internal jobs, external dependencies connections ... */
async stop() {
try {
await this.instance.stop();
}
catch (error) {
await this.fail(crash_1.Crash.from(error));
throw error;
}
}
/** Initialize the process: internal jobs, external dependencies connections ... */
async start() {
this.cleanEventHandlers();
try {
await this.instance.start();
}
catch (error) {
await this.fail(crash_1.Crash.from(error));
throw error;
}
this.changeState(new Running_1.RunningState(this.instance, this.changeState, this.manageError));
}
/**
* 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('unhealthy', this.errorEventHandler);
}
}
exports.StoppedState = StoppedState;
//# sourceMappingURL=Stopped.js.map