1 export type Listener<Arguments extends any[], Return> = (...args: Arguments) => Return;
3 export interface Listeners<Arguments extends any[], Return> {
4 notify: (...args: Arguments) => Return[];
5 subscribe: (listener: Listener<Arguments, Return>) => () => void;
10 const createListeners = <Arguments extends any[], Return = void>(): Listeners<Arguments, Return> => {
11 let listeners: Listener<Arguments, Return>[] = [];
13 const notify = (...args: Arguments) => {
14 return listeners.map((listener) => {
15 return listener(...args);
19 const subscribe = (listener: Listener<Arguments, Return>) => {
20 listeners.push(listener);
22 listeners.splice(listeners.indexOf(listener), 1);
30 const length = () => listeners.length;
40 export default createListeners;