-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useExecutorSuspense accepts a single executor
- Loading branch information
1 parent
4db0f3b
commit f9be911
Showing
5 changed files
with
59 additions
and
105 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
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,32 +1,22 @@ | ||
import type { Executor } from './types'; | ||
import { noop } from './utils'; | ||
|
||
/** | ||
* Suspends rendering until all of provided executors are settled. | ||
* Suspends rendering until an executor satisfies a predicate. | ||
* | ||
* @param executors Executors to wait for. | ||
* @param executor An executor to wait for. | ||
* @param predicate The predicate which a pending executor must conform to suspend the rendering process. By default, | ||
* only non-fulfilled executors are awaited. | ||
* @template T Executors to wait for. | ||
* executor is awaited if it isn't fulfilled. | ||
* @template Value The value stored by the executor. | ||
*/ | ||
export function useExecutorSuspense<T extends Executor | Executor[]>( | ||
executors: T, | ||
predicate = (executor: Executor) => !executor.isFulfilled | ||
): T { | ||
if (Array.isArray(executors)) { | ||
const promises = []; | ||
|
||
for (const executor of executors) { | ||
if (executor.isPending && predicate(executor)) { | ||
promises.push(executor.getOrAwait().then(noop, noop)); | ||
} | ||
} | ||
if (promises.length !== 0) { | ||
throw Promise.all(promises); | ||
} | ||
} else if (executors.isPending && predicate(executors)) { | ||
throw executors.getOrAwait().then(noop, noop); | ||
export function useExecutorSuspense<Value>( | ||
executor: Executor<Value>, | ||
predicate: (executor: Executor<Value>) => boolean = isUnfulfilled | ||
): void { | ||
if (executor.isPending && predicate(executor)) { | ||
throw executor.getOrAwait(); | ||
} | ||
} | ||
|
||
return executors; | ||
function isUnfulfilled(executor: Executor) { | ||
return !executor.isFulfilled; | ||
} |
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