// eslint-disable-next-line @typescript-eslint/no-unused-vars namespace OSFramework.OSUI.Event.ProviderEvents { export class ProviderEventsManager implements IProviderEventManager { // Map with the events added to provider and saved private _eventsMap = new Map(); // Map with the pending events to be added when provider is available private _pendingEventsMap = new Map(); /** * Method to add a pending event * * @param eventName Name of the provider event * @param callback Platform callback to run when the provider event fires * @param eventUniqueId Unique id of the provider event */ public addPendingEvent(eventName: string, callback: GlobalCallbacks.Generic, eventUniqueId: string): void { // Check if we've everything needed to store the event if (eventName === '' || callback === undefined || eventUniqueId === '') { throw new Error( `${ErrorCodes.ProviderEventsManager.FailSavingPendingEvent}: The event can not be saved.` ); } const newEvent = new ProviderEvent(callback, eventName, eventUniqueId); this._pendingEventsMap.set(eventUniqueId, newEvent); } /** * Method to remove a pending event * * @param eventUniqueId Unique id of the provider event */ public removePendingEvent(eventUniqueId: string): void { const event = this._pendingEventsMap.has(eventUniqueId); if (event) { this._pendingEventsMap.delete(eventUniqueId); } else { throw new Error( `${ErrorCodes.ProviderEventsManager.FailPendingEventRemoval}: The event with eventId:'${eventUniqueId}' does not exist` ); } } /** * Method to remove a saved event * * @param eventUniqueId Unique id of the provider event */ public removeSavedEvent(eventUniqueId: string): void { const event = this._eventsMap.has(eventUniqueId); if (event) { this._eventsMap.delete(eventUniqueId); } else { throw new Error( `${ErrorCodes.ProviderEventsManager.FailSavedEventRemoval}: The event with eventId:'${eventUniqueId}' does not exist` ); } } /** * Method to save an event * * @param eventName Name of the provider event * @param callback Platform callback to run when the provider event fires * @param eventUniqueId Unique id of the provider event */ public saveEvent(eventName: string, callback: GlobalCallbacks.Generic, eventUniqueId: string): void { // Check if we've everything needed to store the event if (eventName === '' || callback === undefined || eventUniqueId === '') { throw new Error(`${ErrorCodes.ProviderEventsManager.FailSavingEvent}: The event can not be saved.`); } let _newEvent: IProviderEvent; // If the event we are adding was already on the pending ones, get that one if (this._pendingEventsMap.has(eventUniqueId)) { _newEvent = this._pendingEventsMap.get(eventUniqueId); this._pendingEventsMap.delete(eventUniqueId); } else { // Otherwise create a new one _newEvent = new ProviderEvent(callback, eventName, eventUniqueId); } this._eventsMap.set(eventUniqueId, _newEvent); } /** * Get all the existing events */ public get events(): Map { return this._eventsMap; } /** * Get all the existing pending events * * @readonly */ public get pendingEvents(): Map { return this._pendingEventsMap; } /** * Check if there're saved events */ public get hasEvents(): boolean { return this._eventsMap.size > 0; } /** * Check if there're pending events */ public get hasPendingEvents(): boolean { return this._pendingEventsMap.size > 0; } } }