import { source, type Observable } from '../core/index.mjs'; /** * Creates an event emitter for void events (events without payload). * Returns a tuple of [observable, emitter function]. * * @returns A tuple containing the observable and the emitter function * * @example * ```ts * const [click$, emitClick] = createEventEmitter(); * * const mut_clickCount = { value: 0 }; * * click$.subscribe(() => { * mut_clickCount.value += 1; * }); * * emitClick(); // logs: Clicked! * * assert.deepStrictEqual(mut_clickCount.value, 1); * * emitClick(); * * emitClick(); * * assert.deepStrictEqual(mut_clickCount.value, 3); * ``` */ export const createEventEmitter = (): readonly [ Observable, () => void, ] => { const src$ = source(); const emitter = (): void => { src$.next(undefined); }; return [src$, emitter]; }; /** * Creates an event emitter with typed payload. * Returns a tuple of [observable, emitter function]. * * @template A - The type of the event payload * @returns A tuple containing the observable and the emitter function * * @example * ```ts * const [message$, emitMessage] = createValueEmitter(); * * const messageHistory: string[] = []; * * message$.subscribe((msg) => { * messageHistory.push(msg); * }); * * emitMessage('Hello'); // logs: Hello * * assert.deepStrictEqual(messageHistory, ['Hello']); * * emitMessage('World'); * * assert.deepStrictEqual(messageHistory, ['Hello', 'World']); * ``` */ export const createValueEmitter = (): readonly [ Observable, (value: A) => void, ] => { const src$ = source(); const emitter = (value: A): void => { src$.next(value); }; return [src$, emitter]; };