Skip to content

Commit

Permalink
implementPromiseAction supports now iterator and (async) function
Browse files Browse the repository at this point in the history
  • Loading branch information
teneko committed Oct 29, 2021
1 parent 7890293 commit 2b6ad05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function rejectPromise<TA extends PayloadActionAndMeta<any, any, any, Resolvable
*
* @param executor A function that returns a value or throws a error that get applied on promise.
*/
export function* implementPromiseAction<TA extends PayloadActionAndMeta<any, any, any, any>>(action: TA, executor: () => ResolveValueFromTriggerAction<TA>) {
export function* implementPromiseAction<TA extends PayloadActionAndMeta<any, any, any, any>>(action: TA, executor: TriggerExecutor<ResolveValueFromTriggerAction<TA>>) {
verify(action, "implementPromiseAction");

try {
Expand Down Expand Up @@ -107,6 +107,8 @@ function createPromiseActions<T extends string>(type: T) {
};
}

type TriggerExecutor<RT> = (() => PromiseLike<RT> | RT | Iterator<any, RT, any>);

function createUpdatedTrigger<V, P, T extends string, TA extends PayloadActionCreator<any, any>>(
type: T,
triggerAction: TA,
Expand All @@ -129,7 +131,7 @@ function createUpdatedTrigger<V, P, T extends string, TA extends PayloadActionCr
} = {} as any;

const sagas = {
implement: implementPromiseAction as <TA2 extends typeof types["action"]>(action: TA2, executor: () => ResolveValueFromTriggerAction<TA2>) => ReturnType<typeof implementPromiseAction>,
implement: implementPromiseAction as <TA2 extends typeof types["action"], RT extends ResolveValueFromTriggerAction<TA2>>(action: TA2, executor: TriggerExecutor<RT>) => ReturnType<typeof implementPromiseAction>,
resolve: resolvePromiseAction as <TA2 extends typeof types["action"]>(action: TA2, value: ResolveValueFromTriggerAction<TA2>) => ReturnType<typeof resolvePromiseAction>,
reject: rejectPromiseAction as <TA2 extends typeof types["action"]>(action: TA2, error: any) => ReturnType<typeof rejectPromiseAction>,
};
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ describe("implementPromiseAction", function () {
store.dispatch(sagas.controlAction({}));
expect(caughtMiddlewareError() instanceof ConfigurationError).toBeTruthy();
});

it("should correctly infer value type", function () {
const promiseAction = promiseActionFactory<string>().simple("test");
call(promiseAction.sagas.implement, promiseAction(), async () => Promise.resolve(""));
call(promiseAction.sagas.implement, promiseAction(), function* () { yield "dummy"; return ""; });
call(promiseAction.sagas.implement, promiseAction(), () => "");
});
});

describe("resolvePromiseAction", function () {
Expand Down Expand Up @@ -232,6 +239,12 @@ describe("resolvePromiseAction", function () {
store.dispatch(sagas.controlAction({}));
expect(caughtMiddlewareError() instanceof ConfigurationError).toBeTruthy();
});

it("should correctly infer value type", function () {
const promiseAction = promiseActionFactory<string>().simple("test");
call(promiseAction.sagas.resolve, promiseAction(), "");
call(promiseAction.sagas.resolve, promiseAction.trigger(), "");
});
});

describe("rejectPromiseAction", function () {
Expand Down

0 comments on commit 2b6ad05

Please sign in to comment.