service-utilities
Version:
Utility Package for FIORI UI5
88 lines (80 loc) • 2.46 kB
JavaScript
/**
* @module EventRadio
* @description Utility class for UI5 Event Bus
* @author jpanti
* @version 1.0.0
* @created 2025-08-01
* @lastModified 2025-08-01
* @license ISC
*/
sap.ui.define(["sap/ui/core/EventBus"], (EventBus) => {
"use strict";
const eventBus = EventBus.getInstance();
class EventRadio {
// Initialization ===================================
static defaultEventID = "default";
#sChannelID;
getChannelID = () => this.#sChannelID;
constructor(sChannelId) {
this.#sChannelID = sChannelId;
}
// ==================================================
// Core Methods =====================================
#transmit(sEventId = this.defaultEventID, oData = undefined) {
eventBus.publish(this.#sChannelID, sEventId, oData);
}
#listen(
sEventId = this.defaultEventID,
callback = (oData) => {},
oListener = undefined
) {
const callbackWrapper = (_sChannelId, _sEventId, oData) =>
callback(oData);
eventBus.subscribe(
this.#sChannelID,
sEventId,
callbackWrapper,
oListener
);
}
// doesn't support multiple watchers yet
listenOnce(
sEventId = this.defaultEventID,
callback = (oData) => {},
oListener = undefined
) {
const callbackWrapper = (_sChannelId, _sEventId, oData) =>
callback(oData);
eventBus.subscribeOnce(
this.#sChannelID,
sEventId,
callbackWrapper,
oListener
);
}
// ==================================================
// sEventIds can be either an array of string or just string
// Extension Methods ================================
transmit(sEventIds = this.defaultEventID, oData = undefined) {
if (Array.isArray(sEventIds))
sEventIds.forEach((sEventId) => this.#transmit(sEventId, oData));
else if (typeof sEventIds === "string") this.#transmit(sEventIds, oData);
return this;
}
listen(
sEventIds = this.defaultEventID,
callback = (oData) => {},
oListener = undefined
) {
if (Array.isArray(sEventIds))
sEventIds.forEach((sEventId) =>
this.#listen(sEventId, callback, oListener)
);
else if (typeof sEventIds === "string")
this.#listen(sEventIds, callback, oListener);
return this;
}
// ==================================================
}
return EventRadio;
});