Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 939 B view raw
1import type { Sink, Source } from 'wonka'; 2import { subscribe, take, filter, toPromise, pipe } from 'wonka'; 3import type { OperationResult, OperationResultSource } from '../types'; 4 5/** Patches a `toPromise` method onto the `Source` passed to it. 6 * @param source$ - the Wonka {@link Source} to patch. 7 * @returns The passed `source$` with a patched `toPromise` method as a {@link PromisifiedSource}. 8 * @internal 9 */ 10export function withPromise<T extends OperationResult>( 11 _source$: Source<T> 12): OperationResultSource<T> { 13 const source$ = ((sink: Sink<T>) => 14 _source$(sink)) as OperationResultSource<T>; 15 source$.toPromise = () => 16 pipe( 17 source$, 18 filter(result => !result.stale && !result.hasNext), 19 take(1), 20 toPromise 21 ); 22 source$.then = (onResolve, onReject) => 23 source$.toPromise().then(onResolve, onReject); 24 source$.subscribe = onResult => subscribe(onResult)(source$); 25 return source$; 26}