-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Provide OperationResultSource from Client methods (#3060)
- Loading branch information
Showing
7 changed files
with
110 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@urql/core': minor | ||
--- | ||
|
||
Return a new `OperationResultSource` from all `Client` methods (which replaces `PromisifiedSource` on shortcut methods). This allows not only `toPromise()` to be called, but it can also be used as an awaitable `PromiseLike` and has a `.subscribe(onResult)` method aliasing the subscribe utility from `wonka`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import { Source, take, filter, toPromise, pipe } from 'wonka'; | ||
import { OperationResult, PromisifiedSource } from '../types'; | ||
import { Sink, Source, subscribe, take, filter, toPromise, pipe } from 'wonka'; | ||
import { OperationResult, OperationResultSource } from '../types'; | ||
|
||
/** Patches a `toPromise` method onto the `Source` passed to it. | ||
* @param source$ - the Wonka {@link Source} to patch. | ||
* @returns The passed `source$` with a patched `toPromise` method as a {@link PromisifiedSource}. | ||
* @internal | ||
*/ | ||
export function withPromise<T extends OperationResult>( | ||
source$: Source<T> | ||
): PromisifiedSource<T> { | ||
(source$ as PromisifiedSource<T>).toPromise = () => | ||
_source$: Source<T> | ||
): OperationResultSource<T> { | ||
const source$ = ((sink: Sink<T>) => | ||
_source$(sink)) as OperationResultSource<T>; | ||
source$.toPromise = () => | ||
pipe( | ||
source$, | ||
filter(result => !result.stale && !result.hasNext), | ||
take(1), | ||
toPromise | ||
); | ||
|
||
return source$ as PromisifiedSource<T>; | ||
source$.then = (onResolve, onReject) => | ||
source$.toPromise().then(onResolve, onReject); | ||
source$.subscribe = onResult => subscribe(onResult)(source$); | ||
return source$; | ||
} |
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