@nent/core
Version:
44 lines (43 loc) • 1.28 kB
JavaScript
/*!
* NENT 2022
*/
/* istanbul ignore file */
import { forceUpdate } from '@stencil/core';
import { eventBus } from '../actions';
import { commonState, onCommonStateChange, } from './state';
/* It subscribes to an event on the event bus and calls forceUpdate on the component when the event is
emitted */
export class CommonStateSubscriber {
constructor(component, commonSetting, eventName, events = eventBus) {
this.component = component;
this.eventName = eventName;
this.events = events;
if (commonState[commonSetting]) {
this.subscribeToEvents();
}
else {
this.stateSubscription = onCommonStateChange(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, () => {
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);
}
}