export type StatefulEvt = { current: T; subscribe: (next: (data: T) => void) => Subscription; }; export type StatefulReadonlyEvt = { readonly current: T; subscribe: (next: (data: T) => void) => Subscription; }; export type Subscription = { unsubscribe: () => void; }; export function createStatefulEvt(getInitialValue: () => T): StatefulEvt { let nextFunctions: ((data: T) => void)[] = []; let wrappedState: [T] | undefined = undefined; return { get current() { if (wrappedState === undefined) { wrappedState = [getInitialValue()]; } return wrappedState[0]; }, set current(data: T) { wrappedState = [data]; nextFunctions.forEach(next => next(data)); }, subscribe: (next: (data: T) => void) => { nextFunctions.push(next); return { unsubscribe: () => nextFunctions.splice(nextFunctions.indexOf(next), 1) }; } }; }