@devexperts/dxcharts-lite
Version:
84 lines (83 loc) • 3.09 kB
JavaScript
/*
* Copyright (C) 2019 - 2026 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (C) 2019 - 2024 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { Subject } from 'rxjs';
import { arrayRemove } from '../utils/array.utils';
import { EVENT_DRAW } from './events';
/**
* Event bus for chart.
* @doc-tags core-components
*/
export default class EventBus {
constructor() {
this.UNKNOWN_ARR = [];
/** Registered event handlers. */
this.handlers = {};
/** State which can enable / disable events processing. */
this.muted = false;
this.unsub = (type, fn) => {
const handler = this.handlers[type];
handler !== undefined && arrayRemove.call(handler, fn);
};
this.add = (method, type, fn) => {
if (type in this.handlers) {
method.call(this.handlers[type], fn);
}
else {
this.handlers[type] = [fn];
}
return this.unsub.bind(this, type, fn);
};
this.on = (type, fn) => this.add(this.UNKNOWN_ARR.push, type, fn);
this.observe = (type) => {
const result = new Subject();
const subjectFn = result.next.bind(result);
const originalUnsubscribe = result.unsubscribe.bind(result);
this.on(type, subjectFn);
result.unsubscribe = () => {
this.unsub(type, subjectFn);
originalUnsubscribe();
};
return result.asObservable();
};
this.onPrior = (type, fn) => this.add(this.UNKNOWN_ARR.unshift, type, fn);
this.off = this.unsub;
this.fire = (type, ...args) => {
var _a;
if (!this.muted) {
const arr = (_a = this.handlers[type]) !== null && _a !== void 0 ? _a : [];
let i;
for (i = 0, arr.length; i < arr.length; i++) {
// call each handler with arguments
const fn = arr[i];
fn.apply(null, args);
}
}
};
this.fireAsync = (type, event) => {
if (!this.muted) {
return window.setTimeout(this.fire.bind(this, type, event), 0);
}
};
this.setMuted = (val) => {
this.muted = val;
};
this.clear = () => {
this.handlers = {};
};
}
/**
* Triggers the draw event for the specified canvas IDs.
* @param {Array<string>} canvasIds - An optional array of canvas IDs to trigger the draw event for.
*/
fireDraw(canvasIds) {
this.fire(EVENT_DRAW, canvasIds);
}
}