import { Observable as ObservableT } from './index' import Observable from './observable' type Callback = (...args: any) => T type CallableFunction0 = (callback: Callback) => any type CallableFunction1 = (a: A, callback: Callback) => any type CallableFunction2 = (a: A, b: B, callback: Callback) => any type CallableFunction3 = ( a: A, b: B, c: C, callback: Callback ) => any type CallableFunction4 = ( a: A, b: B, c: C, d: D, callback: Callback ) => any type CallableFunctionT = (...args: Array>) => any /** * Create function (which expects a callback) to a function that returns an observable. * * {@link fromCallback} translates a callback taking function, and returns a function which no longer takes that * callback - instead returning an Observable that, when subscribed to, will call the original function with the given * arguments and emit next events any time the callback is called. * * @example Example using a function with a callback * * function readFile(name: string, callback: (contents: Buffer) => void) { } * const newReadFile = fromCallback(readFile) * // => newReadFile(name: string) => Observable. * * @param {TFunction extends (...args: any[]) => any} func the original callback * @returns {ObservableT} observable that calls the callback upon subscription */ export default function fromCallback( func: CallableFunction0 ): () => ObservableT export default function fromCallback( func: CallableFunction1 ): (a: A) => ObservableT export default function fromCallback( func: CallableFunction2 ): (a: A, b: B) => ObservableT export default function fromCallback( func: CallableFunction3 ): (a: A, b: B, c: C) => ObservableT export default function fromCallback( func: CallableFunction4 ): (a: A, b: B, c: C, d: D) => ObservableT export default function fromCallback( func: CallableFunctionT ): (...arg: T[]) => ObservableT { // eslint-disable-next-line @typescript-eslint/explicit-function-return-type return (...args) => // eslint-disable-next-line @typescript-eslint/explicit-function-return-type new Observable(({ next }) => func(...args.concat(next as any))) }