mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Replace apiclient event bus with local version
This commit is contained in:
parent
dabeda3fdd
commit
0a0e45d155
84 changed files with 159 additions and 91 deletions
50
src/utils/events.ts
Normal file
50
src/utils/events.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
interface Event {
|
||||
type: string;
|
||||
}
|
||||
|
||||
type callback = (e: Event, ...args: any[]) => void;
|
||||
|
||||
function getCallbacks(obj: any, type: string): callback[] {
|
||||
if (!obj) {
|
||||
throw new Error('obj cannot be null!');
|
||||
}
|
||||
|
||||
obj._callbacks = obj._callbacks || {};
|
||||
|
||||
let callbacks = obj._callbacks[type];
|
||||
|
||||
if (!callbacks) {
|
||||
obj._callbacks[type] = [];
|
||||
callbacks = obj._callbacks[type];
|
||||
}
|
||||
|
||||
return callbacks;
|
||||
}
|
||||
|
||||
export default {
|
||||
on(obj: any, type: string, fn: callback): void {
|
||||
const callbacks = getCallbacks(obj, type);
|
||||
|
||||
callbacks.push(fn);
|
||||
},
|
||||
|
||||
off(obj: any, type: string, fn: callback): void {
|
||||
const callbacks = getCallbacks(obj, type);
|
||||
|
||||
const i = callbacks.indexOf(fn);
|
||||
if (i !== -1) {
|
||||
callbacks.splice(i, 1);
|
||||
}
|
||||
},
|
||||
|
||||
trigger(obj: any, type: string, args: any[] = []) {
|
||||
const eventArgs: [Event, ...any] = [{ type }, ...args];
|
||||
|
||||
getCallbacks(obj, type).slice(0)
|
||||
.forEach(callback => {
|
||||
callback.apply(obj, eventArgs);
|
||||
});
|
||||
}
|
||||
};
|
||||
/* eslint-enable @typescript-eslint/no-explicit-any */
|
Loading…
Add table
Add a link
Reference in a new issue