Skip to content

Commit

Permalink
Overload resolvePromiseAction and rejectPromiseAction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekkleszcz committed Jan 29, 2021
1 parent 86dc1e0 commit cc74893
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function* promiseActionWrapper<
) {
try {
const payload: Y = yield call(worker, action);
resolvePromiseAction(action, payload as any);
resolvePromiseAction(action, payload);
yield put(promiseAction.success(payload));
} catch (err) {
yield rejectPromiseAction(action, err);
Expand Down
35 changes: 31 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,55 @@ export const promiseMiddleware: PromiseMiddleware = () => next => action => {
});
};

/**
* Resolve promise action.
* @param {PromiseAction<RequestType, X, Y>} action Action to resolve
*/
export function resolvePromiseAction<RequestType extends TypeConstant, X, undefined>(
action: PromiseAction<RequestType, X, undefined>
): void;

/**
* Resolve promise action.
* @param {PromiseAction<RequestType, X, Y>} action Action to resolve
* @param {Y} payload Payload to resolve action with
*/
export function resolvePromiseAction<RequestType extends TypeConstant, X, Y>(
action: PromiseAction<RequestType, X, Y>,
...payload: Y extends undefined ? [] : [Y]
payload: Y
): void;

export function resolvePromiseAction<RequestType extends TypeConstant, X, Y>(
action: PromiseAction<RequestType, X, Y>,
payload?: Y
) {
action.meta.promise.resolve(payload[0]);
action.meta.promise.resolve(payload);
}

/**
* Reject promise action.
* @param {A} action Action to reject
*/
export function rejectPromiseAction<RequestType extends TypeConstant, X, Y, undefined>(
action: PromiseAction<RequestType, X, Y>
): void;


/**
* Reject promise action.
* @param {A} action Action to reject
* @param {TResolvePayload} payload Payload to reject action with
*/
export function rejectPromiseAction<RequestType extends TypeConstant, X, Y, Z>(
action: PromiseAction<RequestType, X, Y>,
...payload: Z extends undefined ? [] : [Z]
payload: Z
): void;

export function rejectPromiseAction<RequestType extends TypeConstant, X, Y, Z>(
action: PromiseAction<RequestType, X, Y>,
payload?: Z
) {
action.meta.promise.reject(payload[0]);
action.meta.promise.reject(payload);
}

/**
Expand Down

0 comments on commit cc74893

Please sign in to comment.