Last active
October 9, 2021 20:51
-
-
Save tshallenberger/26099bb8e90f90f3d5bd2c7da88abe91 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { | |
createRoutine, | |
// @ts-ignore | |
createRoutineCreator, | |
// @ts-ignore | |
defaultRoutineStages, | |
ResolveActionCreatorByPayload, | |
Routine, | |
} from "redux-saga-routines"; | |
export interface RoutinePayloadCreator< | |
TTriggerPayloadCreator, | |
TRequestPayloadCreator, | |
TSuccessPayloadCreator, | |
TFailurePayloadCreator, | |
TFulfillPayloadCreator | |
> { | |
TRIGGER?: TTriggerPayloadCreator; | |
trigger?: TTriggerPayloadCreator; | |
REQUEST?: TRequestPayloadCreator; | |
request?: TRequestPayloadCreator; | |
SUCCESS?: TSuccessPayloadCreator; | |
success?: TSuccessPayloadCreator; | |
FAILURE?: TFailurePayloadCreator; | |
failure?: TFailurePayloadCreator; | |
FULFILL?: TFulfillPayloadCreator; | |
fulfill?: TFulfillPayloadCreator; | |
} | |
type CacheableRoutine< | |
TTriggerPayloadCreator, | |
TRequestPayloadCreator, | |
TSuccessPayloadCreator, | |
TFailurePayloadCreator, | |
TFulfillPayloadCreator | |
> = Routine< | |
ResolveActionCreatorByPayload<TTriggerPayloadCreator>, | |
ResolveActionCreatorByPayload<TRequestPayloadCreator>, | |
ResolveActionCreatorByPayload<TSuccessPayloadCreator>, | |
ResolveActionCreatorByPayload<TFailurePayloadCreator>, | |
ResolveActionCreatorByPayload<TFulfillPayloadCreator> | |
> & { invalidate: ResolveActionCreatorByPayload<TTriggerPayloadCreator> }; | |
export const createCacheableRoutine = < | |
TTriggerPayloadCreator, | |
TRequestPayloadCreator, | |
TSuccessPayloadCreator, | |
TFailurePayloadCreator, | |
TFulfillPayloadCreator | |
>( | |
action: string, | |
payloadCreator: RoutinePayloadCreator< | |
TTriggerPayloadCreator, | |
TRequestPayloadCreator, | |
TSuccessPayloadCreator, | |
TFailurePayloadCreator, | |
TFulfillPayloadCreator | |
> | |
): CacheableRoutine< | |
TTriggerPayloadCreator, | |
TRequestPayloadCreator, | |
TSuccessPayloadCreator, | |
TFailurePayloadCreator, | |
TFulfillPayloadCreator | |
> => { | |
const routineStages = [...defaultRoutineStages, "INVALIDATE"]; | |
return createRoutineCreator(routineStages)( | |
`${action}-${JSON.stringify(payloadCreator.trigger)}`, | |
payloadCreator | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment