@zowe/imperative
Version:
framework for building configurable CLIs
179 lines • 8.75 kB
JavaScript
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventProcessor = void 0;
const Logger_1 = require("../../logger/src/Logger");
const EventConstants_1 = require("./EventConstants");
const ImperativeError_1 = require("../../error/src/ImperativeError");
const ConfigUtils_1 = require("../../config/src/ConfigUtils");
const LoggerManager_1 = require("../../logger/src/LoggerManager");
const utilities_1 = require("../../utilities");
const EventUtils_1 = require("./EventUtils");
const IEventInstanceTypes_1 = require("./doc/IEventInstanceTypes");
/**
* ## Overview
* Each EventProcessor manages its own subscriptions, handling the addition, emission, and removal of events.
* It uses a map where event names are keys, and values are Event objects that hold detailed event information and subscriptions.
*
* An `EventProcessor` handles three main functionalities:
* - **Subscribing to Events**:
* Registration of a callback function that will be executed when that event occurs.
* - **Emitting Events**:
* Notifying other applications or parts of the same application about certain actions or changes.
* - **Managing Event Subscriptions**:
* Mapping subscribed events and their corresponding callbacks, ensuring that events are properly handled and dispatched.
*
* ### Understanding Event Types
* - **Predefined Zowe Events**:
* Zowe provides a set of predefined shared and user events that can be watched.
* - **Custom Events**:
* Applications can define their own shared and user events, allowing for custom event-driven behavior.
*
* @export
* @class EventProcessor
*/
class EventProcessor {
/**
* Constructor initializes a new instance of EventProcessor.
*
* @param {string} appName - The application's name.
* @param {IProcessorTypes} type - The type of processor (Emitter, Watcher, or Both).
* @param {Logger} [logger] - Optional logger for recording events and errors.
* @throws {ImperativeError} If the application name is not recognized.
*/
constructor(appName, type, logger) {
this.subscribedEvents = new Map();
EventUtils_1.EventUtils.validateAppName(appName);
this.subscribedEvents = new Map();
this.eventTimes = new Map();
this.appName = appName;
this.processorType = type;
// Ensure correct environmental conditions to setup a custom logger,
// otherwise use default logger
if (utilities_1.ImperativeConfig.instance.loadedConfig == null || LoggerManager_1.LoggerManager.instance.isLoggerInit === false) {
ConfigUtils_1.ConfigUtils.initImpUtils("zowe");
}
this.logger = logger !== null && logger !== void 0 ? logger : Logger_1.Logger.getImperativeLogger();
}
_subscribe(type, eventName, callbacks) {
if (this.processorType === IEventInstanceTypes_1.IProcessorTypes.EMITTER) {
throw new ImperativeError_1.ImperativeError({ msg: `Processor does not have correct permissions: ${eventName}` });
}
let eventType;
if (type === "shared") {
eventType = EventUtils_1.EventUtils.isSharedEvent(eventName) ? EventConstants_1.EventTypes.ZoweSharedEvents : EventConstants_1.EventTypes.SharedEvents;
}
else if (type === "user") {
eventType = EventUtils_1.EventUtils.isUserEvent(eventName) ? EventConstants_1.EventTypes.ZoweUserEvents : EventConstants_1.EventTypes.UserEvents;
}
const disposable = EventUtils_1.EventUtils.createSubscription(this, eventName, eventType);
EventUtils_1.EventUtils.setupWatcher(this, eventName, callbacks);
return disposable;
}
/**
* Subscription to an event that will notify all subscribed users.
*
* @param {string} eventName - The name of the event to subscribe to.
* @param {EventCallback[] | EventCallback} callbacks - Callback functions to handle the event.
* @returns {IEventDisposable} - Object allowing management/cleanup of the subscription.
* @throws {ImperativeError} If the processor does not have the correct permissions to perform this action.
*/
subscribeShared(eventName, callbacks) {
return this._subscribe("shared", eventName, callbacks);
}
/**
* Subscription to an event that will notify a single user.
*
* @param {string} eventName - The name of the event to subscribe to.
* @param {EventCallback[] | EventCallback} callbacks - Callback functions to handle the event.
* @returns {IEventDisposable} - Object allowing management/cleanup of the subscription.
* @throws {ImperativeError} If the processor does not have the correct permissions to perform this action.
*/
subscribeUser(eventName, callbacks) {
return this._subscribe("user", eventName, callbacks);
}
/**
* Private method to emit the event
* @private
* @param eventName Event to be emitted
* @throws {ImperativeError} - If the event cannot be emitted.
*/
emit(eventName) {
var _a;
try {
const event = (_a = this.subscribedEvents.get(eventName)) !== null && _a !== void 0 ? _a : EventUtils_1.EventUtils.createEvent(eventName, this.appName);
event.appProcId = process.pid;
event.eventTime = new Date().toISOString();
EventUtils_1.EventUtils.writeEvent(event);
}
catch (err) {
throw new ImperativeError_1.ImperativeError({ msg: `Error writing event: ${eventName}`, causeErrors: err });
}
}
/**
* Emits an event by updating its timestamp and writing event data.
*
* @param {string} eventName - The name of the event to emit.
* @throws {ImperativeError} If the processor does not have the correct permissions to perform this action.
* @throws {ImperativeError} - If the event cannot be emitted.
*/
emitEvent(eventName) {
if (this.processorType === IEventInstanceTypes_1.IProcessorTypes.WATCHER) {
throw new ImperativeError_1.ImperativeError({ msg: `Processor does not have correct permissions: ${eventName}` });
}
if (EventUtils_1.EventUtils.isUserEvent(eventName) || EventUtils_1.EventUtils.isSharedEvent(eventName)) {
throw new ImperativeError_1.ImperativeError({ msg: `Processor not allowed to emit Zowe events: ${eventName}` });
}
this.emit(eventName);
}
/**
* Specifically emits Zowe-related events, updating timestamps and handling data.
*
* @internal Internal Zowe emitter method
* @param {string} eventName - The name of the Zowe event to emit.
* @throws {ImperativeError} If the processor does not have the correct permissions to perform this action.
* @throws {ImperativeError} - If the event cannot be emitted.
*/
emitZoweEvent(eventName) {
if (this.appName !== "Zowe") {
throw new ImperativeError_1.ImperativeError({ msg: `Processor does not have Zowe permissions: ${eventName}` });
}
if (!EventUtils_1.EventUtils.isUserEvent(eventName) && !EventUtils_1.EventUtils.isSharedEvent(eventName)) {
throw new ImperativeError_1.ImperativeError({ msg: `Invalid Zowe event: ${eventName}` });
}
this.emit(eventName);
}
/**
* Unsubscribes from an event, closing file watchers and cleaning up resources.
*
* @param {string} eventName - The name of the event to unsubscribe from.
* @throws {ImperativeError} If the processor does not have the correct permissions to perform this action.
* @throws {ImperativeError} - If unsubscribing fails.
*/
unsubscribe(eventName) {
if (this.processorType === IEventInstanceTypes_1.IProcessorTypes.EMITTER) {
throw new ImperativeError_1.ImperativeError({ msg: `Processor does not have correct permissions: ${eventName}` });
}
const event = this.subscribedEvents.get(eventName);
if (event) {
event.subscriptions.forEach(subscription => {
subscription.removeAllListeners(eventName);
if (typeof subscription.close === 'function') {
subscription.close();
}
});
this.subscribedEvents.delete(eventName);
}
}
}
exports.EventProcessor = EventProcessor;
//# sourceMappingURL=EventProcessor.js.map
;