-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(r): A new method of piping has been added
+ Adds `r` a new method of piping observables. This allows any valid observable input to be passed as the first argument, and it will implicitly convert the argument to an Observable and pass it to the function that may be present in the second argument, the return value of THAT function is then passed an argument to the next function, and so on + Corrects the types of `Observable#pipe` such that anything can be returned at any step of the functional chain, however the first pipeable function must accept an `Observable<T>`. + Adds dtslint and runtime tests for `r` and `pipe`. NOTE: This does NOT deprecate`Observable#pipe`. That will be done in a follow up, and we need to define what timeline we'll take to remove that, as it's an API that is broadly used - could be v9, could be v10, could be never. At this point, this is alpha/beta functionality. BREAKING CHANGE: `Observable#pipe` now allows any pipeable unary function as an argument, just so long as the types properly compose, this means in some cases it will now return `unknown` instead of `Observable<unknown>` the workaround is just to cast the result for those cases. (or you can break your operator piping into smaller pipe sets)
- Loading branch information
Showing
6 changed files
with
268 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { r, map, of, toArray, filter } from 'rxjs'; | ||
|
||
|
||
it('should infer conversions from ObservableInputs', () => { | ||
const o1 = r([1, 2, 3]); // $ExpectType Observable<number> | ||
const o2 = r(new Set<number>()); // $ExpectType Observable<number> | ||
const o3 = r(new Map<string, number>()); // $ExpectType Observable<[string, number]> | ||
const o4 = r(of(1, 2, 3)); // $ExpectType Observable<number> | ||
const o5 = r(Promise.resolve(1)); // $ExpectType Observable<number> | ||
const o6 = r(Promise.resolve([1, 2, 3])); // $ExpectType Observable<number[]> | ||
|
||
function* test() { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
|
||
const o7 = r(test()); // $ExpectType Observable<1 | 2 | 3> | ||
|
||
async function* test2() { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
|
||
const o8 = r(test2()); // $ExpectType Observable<1 | 2 | 3> | ||
const o9 = r({}); // $ExpectError | ||
}); | ||
|
||
it('should compose with pipeable functions, passing an Observable to the first of those functions', () => { | ||
const o1 = r([1, 2, 3], map(n => n + 1)); // $ExpectType Observable<number> | ||
const o2 = r([1, 2, 3], map(n => n + 1), filter(n => n < 3)); // $ExpectType Observable<number> | ||
const o3 = r([1, 2, 3], map(n => n + 1), filter(n => n < 3), toArray()); // $ExpectType Observable<number[]> | ||
const o4 = r([1, 2, 3], map(n => n + 1), filter(n => n < 3), toArray(), map(n => n.length)); // $ExpectType Observable<number> | ||
|
||
// Even with unary functions that are not RxJS operators | ||
const o5 = r([1, 2, 3], map(n => n + 1), toArray(), source => Object.keys(source), keys => keys.length); // $ExpectType number | ||
|
||
// Maybe as a means of subscription | ||
const o6 = r([1, 2, 3], map(n => n + 1), toArray(), source => source.subscribe()); // $ExpectType Subscription | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.