@nent/core
Version:
49 lines (45 loc) • 1.35 kB
JavaScript
/*!
* NENT 2022
*/
;
const index$1 = require('./index-1829aebc.js');
const index = require('./index-637e8c28.js');
const state = require('./state-f97ff0e6.js');
/* istanbul ignore file */
/* It subscribes to an event on the event bus and calls forceUpdate on the component when the event is
emitted */
class CommonStateSubscriber {
constructor(component, commonSetting, eventName, events = index.eventBus) {
this.component = component;
this.eventName = eventName;
this.events = events;
if (state.state[commonSetting]) {
this.subscribeToEvents();
}
else {
this.stateSubscription = state.onChange(commonSetting, enabled => {
var _a;
if (enabled) {
this.subscribeToEvents();
}
else {
(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.call(this);
}
});
}
}
subscribeToEvents() {
this.subscription = this.events.on(this.eventName, () => {
index$1.forceUpdate(this.component);
});
}
/**
* It unsubscribes from the observable.
*/
destroy() {
var _a, _b;
(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.call(this);
(_b = this.stateSubscription) === null || _b === void 0 ? void 0 : _b.call(this);
}
}
exports.CommonStateSubscriber = CommonStateSubscriber;