-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use promiseWithResolvers for RTKQ lifecycle management
- Loading branch information
ben.durrant
committed
Jun 6, 2023
1 parent
b65f6b5
commit 8c38f92
Showing
4 changed files
with
100 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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { safeAssign } from "../tsHelpers"; | ||
|
||
export interface PromiseConstructorWithKnownReason { | ||
/** | ||
* Creates a new Promise with a known rejection reason. | ||
* @param executor A callback used to initialize the promise. This callback is passed two arguments: | ||
* a resolve callback used to resolve the promise with a value or the result of another promise, | ||
* and a reject callback used to reject the promise with a provided reason or error. | ||
*/ | ||
new <T, R>( | ||
executor: ( | ||
resolve: (value: T | PromiseLike<T>) => void, | ||
reject: (reason?: R) => void | ||
) => void | ||
): PromiseWithKnownReason<T, R> | ||
} | ||
|
||
export interface PromiseWithKnownReason<T, R> | ||
extends Omit<Promise<T>, 'then' | 'catch'> { | ||
/** | ||
* Attaches callbacks for the resolution and/or rejection of the Promise. | ||
* @param onfulfilled The callback to execute when the Promise is resolved. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of which ever callback is executed. | ||
*/ | ||
then<TResult1 = T, TResult2 = never>( | ||
onfulfilled?: | ||
| ((value: T) => TResult1 | PromiseLike<TResult1>) | ||
| undefined | ||
| null, | ||
onrejected?: | ||
| ((reason: R) => TResult2 | PromiseLike<TResult2>) | ||
| undefined | ||
| null | ||
): Promise<TResult1 | TResult2> | ||
|
||
/** | ||
* Attaches a callback for only the rejection of the Promise. | ||
* @param onrejected The callback to execute when the Promise is rejected. | ||
* @returns A Promise for the completion of the callback. | ||
*/ | ||
catch<TResult = never>( | ||
onrejected?: | ||
| ((reason: R) => TResult | PromiseLike<TResult>) | ||
| undefined | ||
| null | ||
): Promise<T | TResult> | ||
} | ||
|
||
export const PromiseWithKnownReason = Promise as PromiseConstructorWithKnownReason | ||
|
||
|
||
type PromiseExecutor<T, R> = ConstructorParameters<typeof PromiseWithKnownReason<T, R>>[0]; | ||
|
||
export type PromiseWithResolvers<T, R> = { | ||
promise: PromiseWithKnownReason<T, R>; | ||
resolve: Parameters<PromiseExecutor<T, R>>[0]; | ||
reject: Parameters<PromiseExecutor<T, R>>[1]; | ||
}; | ||
|
||
export const promiseWithResolvers = <T, R = unknown>(): PromiseWithResolvers<T, R> => { | ||
const result = {} as PromiseWithResolvers<T, R>; | ||
result.promise = new PromiseWithKnownReason<T, R>((resolve, reject) => { | ||
safeAssign(result, { reject, resolve }); | ||
}); | ||
return result; | ||
}; |
8c38f92
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phryneas tagging for future review