-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
usePromise defaultValue (via config)
- Loading branch information
Showing
4 changed files
with
66 additions
and
31 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './model.ts' | ||
export * from './useAtomicPromise.ts' | ||
export * from './usePromise.ts' |
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,22 @@ | ||
/** | ||
* Represents the state of a promise. | ||
* | ||
* @type {UsePromiseState} | ||
* @property {'pending'} pending - The promise is still pending. | ||
* @property {'rejected'} rejected - The promise has been rejected. | ||
* @property {'resolved'} resolved - The promise has been resolved. | ||
*/ | ||
export type UsePromiseState = 'pending' | 'rejected' | 'resolved' | ||
|
||
/** | ||
* Configuration options for the usePromise hook. | ||
* | ||
* @template TResult - The type of the result value. | ||
* | ||
* @property {string} [debug] - Optional debug string for logging purposes. | ||
* @property {TResult} [defaultValue] - Optional default value to be used before the promise resolves. | ||
*/ | ||
export interface UsePromiseConfig<TResult> { | ||
debug?: string | ||
defaultValue?: TResult | ||
} |
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,22 +1,28 @@ | ||
// Inspired from https://github.com/bsonntag/react-use-promise | ||
|
||
import { Mutex } from 'async-mutex' | ||
import type { DependencyList } from 'react' | ||
|
||
import type { State } from './usePromise.ts' | ||
import type { UsePromiseConfig, UsePromiseState } from './model.ts' | ||
import { usePromise } from './usePromise.ts' | ||
|
||
const mutexDictionary: Record<string, Mutex> = {} | ||
|
||
/** | ||
* useAtomicPromise - The same as usePromise, but ensures that only one promise is running at a time. | ||
* A custom hook that ensures a promise is executed atomically, using a mutex to prevent concurrent executions. | ||
* | ||
* @template TResult - The type of the result that the promise resolves to. | ||
* @param {string} name - A unique name for the mutex to ensure atomic execution. | ||
* @param {() => Promise<TResult | undefined>} promise - A function that returns the promise to be executed. | ||
* @param {DependencyList} dependencies - An array of dependencies that will trigger the promise execution when changed. | ||
* @param {UsePromiseConfig<TResult>} [config] - Optional configuration for the promise execution. | ||
* @returns {[TResult | undefined, Error | undefined, UsePromiseState | undefined]} | ||
* An array containing the result of the promise, any error that occurred, and the state of the promise. | ||
*/ | ||
export const useAtomicPromise = <TResult>( | ||
name: string, | ||
promise: () => Promise<TResult | undefined>, | ||
dependencies: DependencyList, | ||
debug: string | undefined = undefined, | ||
): [TResult | undefined, Error | undefined, State | undefined] => { | ||
config?: UsePromiseConfig<TResult>, | ||
): [TResult | undefined, Error | undefined, UsePromiseState | undefined] => { | ||
mutexDictionary[name] = mutexDictionary[name] ?? new Mutex() | ||
return usePromise(() => mutexDictionary[name].runExclusive(promise), dependencies, debug) | ||
return usePromise(() => mutexDictionary[name].runExclusive(promise), dependencies, config) | ||
} |
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