| 1 |
/* @prettier */ |
| 2 |
import { Observable } from '../Observable'; |
| 3 |
import { SchedulerLike } from '../types'; |
| 4 |
import { bindCallbackInternals } from './bindCallbackInternals'; |
| 5 |
|
| 6 |
export function bindNodeCallback( |
| 7 |
callbackFunc: (...args: any[]) => void, |
| 8 |
resultSelector: (...args: any[]) => any, |
| 9 |
scheduler?: SchedulerLike |
| 10 |
): (...args: any[]) => Observable<any>; |
| 11 |
|
| 12 |
// args is the arguments array and we push the callback on the rest tuple since the rest parameter must be last (only item) in a parameter list |
| 13 |
export function bindNodeCallback<A extends readonly unknown[], R extends readonly unknown[]>( |
| 14 |
callbackFunc: (...args: [...A, (err: any, ...res: R) => void]) => void, |
| 15 |
schedulerLike?: SchedulerLike |
| 16 |
): (...arg: A) => Observable<R extends [] ? void : R extends [any] ? R[0] : R>; |
| 17 |
|
| 18 |
/** |
| 19 |
* Converts a Node.js-style callback API to a function that returns an |
| 20 |
* Observable. |
| 21 |
* |
| 22 |
* <span class="informal">It's just like {@link bindCallback}, but the |
| 23 |
* callback is expected to be of type `callback(error, result)`.</span> |
| 24 |
* |
| 25 |
* `bindNodeCallback` is not an operator because its input and output are not |
| 26 |
* Observables. The input is a function `func` with some parameters, but the |
| 27 |
* last parameter must be a callback function that `func` calls when it is |
| 28 |
* done. The callback function is expected to follow Node.js conventions, |
| 29 |
* where the first argument to the callback is an error object, signaling |
| 30 |
* whether call was successful. If that object is passed to callback, it means |
| 31 |
* something went wrong. |
| 32 |
* |
| 33 |
* The output of `bindNodeCallback` is a function that takes the same |
| 34 |
* parameters as `func`, except the last one (the callback). When the output |
| 35 |
* function is called with arguments, it will return an Observable. |
| 36 |
* If `func` calls its callback with error parameter present, Observable will |
| 37 |
* error with that value as well. If error parameter is not passed, Observable will emit |
| 38 |
* second parameter. If there are more parameters (third and so on), |
| 39 |
* Observable will emit an array with all arguments, except first error argument. |
| 40 |
* |
| 41 |
* Note that `func` will not be called at the same time output function is, |
| 42 |
* but rather whenever resulting Observable is subscribed. By default call to |
| 43 |
* `func` will happen synchronously after subscription, but that can be changed |
| 44 |
* with proper `scheduler` provided as optional third parameter. {@link SchedulerLike} |
| 45 |
* can also control when values from callback will be emitted by Observable. |
| 46 |
* To find out more, check out documentation for {@link bindCallback}, where |
| 47 |
* {@link SchedulerLike} works exactly the same. |
| 48 |
* |
| 49 |
* As in {@link bindCallback}, context (`this` property) of input function will be set to context |
| 50 |
* of returned function, when it is called. |
| 51 |
* |
| 52 |
* After Observable emits value, it will complete immediately. This means |
| 53 |
* even if `func` calls callback again, values from second and consecutive |
| 54 |
* calls will never appear on the stream. If you need to handle functions |
| 55 |
* that call callbacks multiple times, check out {@link fromEvent} or |
| 56 |
* {@link fromEventPattern} instead. |
| 57 |
* |
| 58 |
* Note that `bindNodeCallback` can be used in non-Node.js environments as well. |
| 59 |
* "Node.js-style" callbacks are just a convention, so if you write for |
| 60 |
* browsers or any other environment and API you use implements that callback style, |
| 61 |
* `bindNodeCallback` can be safely used on that API functions as well. |
| 62 |
* |
| 63 |
* Remember that Error object passed to callback does not have to be an instance |
| 64 |
* of JavaScript built-in `Error` object. In fact, it does not even have to an object. |
| 65 |
* Error parameter of callback function is interpreted as "present", when value |
| 66 |
* of that parameter is truthy. It could be, for example, non-zero number, non-empty |
| 67 |
* string or boolean `true`. In all of these cases resulting Observable would error |
| 68 |
* with that value. This means usually regular style callbacks will fail very often when |
| 69 |
* `bindNodeCallback` is used. If your Observable errors much more often then you |
| 70 |
* would expect, check if callback really is called in Node.js-style and, if not, |
| 71 |
* switch to {@link bindCallback} instead. |
| 72 |
* |
| 73 |
* Note that even if error parameter is technically present in callback, but its value |
| 74 |
* is falsy, it still won't appear in array emitted by Observable. |
| 75 |
* |
| 76 |
* ## Examples |
| 77 |
* |
| 78 |
* Read a file from the filesystem and get the data as an Observable |
| 79 |
* |
| 80 |
* ```ts |
| 81 |
* import * as fs from 'fs'; |
| 82 |
* const readFileAsObservable = bindNodeCallback(fs.readFile); |
| 83 |
* const result = readFileAsObservable('./roadNames.txt', 'utf8'); |
| 84 |
* result.subscribe(x => console.log(x), e => console.error(e)); |
| 85 |
* ``` |
| 86 |
* |
| 87 |
* Use on function calling callback with multiple arguments |
| 88 |
* |
| 89 |
* ```ts |
| 90 |
* someFunction((err, a, b) => { |
| 91 |
* console.log(err); // null |
| 92 |
* console.log(a); // 5 |
| 93 |
* console.log(b); // "some string" |
| 94 |
* }); |
| 95 |
* const boundSomeFunction = bindNodeCallback(someFunction); |
| 96 |
* boundSomeFunction() |
| 97 |
* .subscribe(value => { |
| 98 |
* console.log(value); // [5, "some string"] |
| 99 |
* }); |
| 100 |
* ``` |
| 101 |
* |
| 102 |
* Use on function calling callback in regular style |
| 103 |
* |
| 104 |
* ```ts |
| 105 |
* someFunction(a => { |
| 106 |
* console.log(a); // 5 |
| 107 |
* }); |
| 108 |
* const boundSomeFunction = bindNodeCallback(someFunction); |
| 109 |
* boundSomeFunction() |
| 110 |
* .subscribe( |
| 111 |
* value => {} // never gets called |
| 112 |
* err => console.log(err) // 5 |
| 113 |
* ); |
| 114 |
* ``` |
| 115 |
* |
| 116 |
* @see {@link bindCallback} |
| 117 |
* @see {@link from} |
| 118 |
* |
| 119 |
* @param callbackFunc Function with a Node.js-style callback as the last parameter. |
| 120 |
* @param resultSelector A mapping function used to transform callback events. |
| 121 |
* @param scheduler The scheduler on which to schedule the callbacks. |
| 122 |
* @return A function which returns the Observable that delivers the same values the |
| 123 |
* Node.js callback would deliver. |
| 124 |
*/ |
| 125 |
export function bindNodeCallback( |
| 126 |
callbackFunc: (...args: [...any[], (err: any, ...res: any) => void]) => void, |
| 127 |
resultSelector?: ((...args: any[]) => any) | SchedulerLike, |
| 128 |
scheduler?: SchedulerLike |
| 129 |
): (...args: any[]) => Observable<any> { |
| 130 |
return bindCallbackInternals(true, callbackFunc, resultSelector, scheduler); |
| 131 |
} |