-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
206 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,200 @@ | ||
import {signal} from '@quilted/signals'; | ||
|
||
export interface AsyncFetchCallResult<Data = unknown, Input = unknown> { | ||
readonly value?: Data; | ||
readonly error?: unknown; | ||
readonly input?: Input; | ||
} | ||
|
||
export interface AsyncFetchFunction<Data = unknown, Input = unknown> { | ||
( | ||
input: Input, | ||
options: { | ||
signal?: AbortSignal; | ||
}, | ||
): PromiseLike<Data>; | ||
} | ||
|
||
export class AsyncFetch<Data = unknown, Input = unknown> { | ||
get status() { | ||
return this.finished.value?.status ?? 'pending'; | ||
} | ||
|
||
get isRunning() { | ||
return this.running.value != null; | ||
} | ||
|
||
get value() { | ||
return this.finished.value?.value; | ||
} | ||
|
||
get error() { | ||
return this.finished.value?.error; | ||
} | ||
|
||
get promise(): AsyncFetchPromise<Data, Input> { | ||
return ( | ||
this.running.value?.promise ?? | ||
this.finished.value?.promise ?? | ||
this.initial.promise | ||
); | ||
} | ||
|
||
private readonly running = signal<AsyncFetchCall<Data, Input> | undefined>( | ||
undefined, | ||
); | ||
private readonly finished = signal<AsyncFetchCall<Data, Input> | undefined>( | ||
undefined, | ||
); | ||
private readonly function: AsyncFetchFunction<Data, Input>; | ||
private readonly initial: AsyncFetchCall<Data, Input>; | ||
|
||
constructor( | ||
fetchFunction: AsyncFetchFunction<Data, Input>, | ||
{initial}: {initial?: AsyncFetchCallResult<Data, Input>} = {}, | ||
) { | ||
this.function = fetchFunction; | ||
this.initial = new AsyncFetchCall(fetchFunction, initial); | ||
} | ||
|
||
call = ( | ||
input?: Input, | ||
{signal}: {signal?: AbortSignal} = {}, | ||
): AsyncFetchPromise<Data, Input> => { | ||
const fetchCall = | ||
this.running.peek() == null && | ||
this.finished.peek() == null && | ||
!this.initial.signal.aborted | ||
? this.initial | ||
: new AsyncFetchCall(this.function); | ||
|
||
const finalizeFetchCall = () => () => { | ||
if (this.running.peek() === fetchCall) { | ||
this.running.value = undefined; | ||
} | ||
|
||
if (fetchCall.signal.aborted) return; | ||
|
||
this.finished.value = fetchCall; | ||
}; | ||
|
||
fetchCall.call(input, {signal}).then(finalizeFetchCall, finalizeFetchCall); | ||
|
||
this.running.value = fetchCall; | ||
|
||
return fetchCall.promise; | ||
}; | ||
} | ||
|
||
export class AsyncFetchCall<Data = unknown, Input = unknown> { | ||
readonly promise: AsyncFetchPromise<Data, Input>; | ||
readonly function: AsyncFetchFunction<Data, Input>; | ||
readonly input!: Input; | ||
|
||
get signal() { | ||
return this.abortController.signal; | ||
} | ||
|
||
get isRunning() { | ||
return this.runningSignal.value; | ||
} | ||
|
||
get status() { | ||
return this.promise.status; | ||
} | ||
|
||
get value() { | ||
return this.promise.status === 'fulfilled' ? this.promise.value : undefined; | ||
} | ||
|
||
get error() { | ||
return this.promise.status === 'rejected' ? this.promise.reason : undefined; | ||
} | ||
|
||
private readonly resolve: (value: Data) => void; | ||
private readonly reject: (cause: unknown) => void; | ||
private readonly abortController = new AbortController(); | ||
private readonly runningSignal = signal(false); | ||
|
||
constructor( | ||
fetchFunction: AsyncFetchFunction<Data, Input>, | ||
initial?: AsyncFetchCallResult<Data, Input>, | ||
) { | ||
this.function = fetchFunction; | ||
|
||
let resolve!: (value: Data) => void; | ||
let reject!: (cause: unknown) => void; | ||
|
||
this.promise = new AsyncFetchPromise(this, (res, rej) => { | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
|
||
this.resolve = resolve; | ||
this.reject = reject; | ||
|
||
if (initial) { | ||
this.input = initial.input!; | ||
|
||
if (initial.error) { | ||
this.reject(initial.error); | ||
} else { | ||
this.resolve(initial.value!); | ||
} | ||
} | ||
} | ||
|
||
abort = () => { | ||
this.abortController.abort(); | ||
}; | ||
|
||
call = (input?: Input, {signal}: {signal?: AbortSignal} = {}) => { | ||
if ( | ||
this.runningSignal.peek() || | ||
this.promise.status === 'pending' || | ||
this.signal.aborted | ||
) { | ||
throw new Error(`Can’t perform fetch()`); | ||
} | ||
|
||
if (signal) { | ||
signal.addEventListener('abort', () => { | ||
this.abortController.abort(); | ||
}); | ||
} | ||
|
||
this.function(input!, {signal}).then(this.resolve, this.reject); | ||
|
||
return this.promise; | ||
}; | ||
} | ||
|
||
export class AsyncFetchPromise< | ||
Data = unknown, | ||
Input = unknown, | ||
> extends Promise<Data> { | ||
readonly status: 'pending' | 'fulfilled' | 'rejected' = 'pending'; | ||
readonly value?: Data; | ||
readonly reason?: unknown; | ||
readonly source: AsyncFetchCall<Data, Input>; | ||
|
||
constructor( | ||
source: AsyncFetchCall<Data, Input>, | ||
executor: ConstructorParameters<typeof Promise<Data>>[0], | ||
) { | ||
super((resolve, reject) => { | ||
executor( | ||
(value) => { | ||
Object.assign(this, {status: 'fulfilled', value}); | ||
resolve(value); | ||
}, | ||
(reason) => { | ||
Object.assign(this, {status: 'rejected', reason}); | ||
reject(reason); | ||
}, | ||
); | ||
}); | ||
|
||
this.source = 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