Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
feat: 🎸 Allow custom type for payload and option
Browse files Browse the repository at this point in the history
  • Loading branch information
Pham Tuan Manh authored and manhhailua committed Jun 25, 2019
1 parent b488d80 commit 0cb054f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 80,
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
Expand Down
19 changes: 6 additions & 13 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
import { AnyAction } from 'redux';
import { SagaIterator } from 'redux-saga';

export interface UnfoldSagaActionType extends AnyAction {
export interface UnfoldSagaActionType<PayloadType = any, OptionType = any> extends AnyAction {
callbacks: UnfoldSagaCallbacksType;
options: any;
payload: any;
options: OptionType;
payload: PayloadType;
}

export interface UnfoldSagaActionExecutionType {
(
payload?: any,
callbacks?: UnfoldSagaCallbacksType,
options?: any,
): UnfoldSagaActionType;
export interface UnfoldSagaActionExecutionType<PayloadType = any, OptionType = any> {
(payload?: PayloadType, callbacks?: UnfoldSagaCallbacksType, options?: OptionType): UnfoldSagaActionType;
}

export interface UnfoldSagaCallbacksType {
Expand All @@ -38,7 +34,4 @@ export function createActionTypeOnSuccess(key: string): string;

export function createAction(type: string): UnfoldSagaActionExecutionType;

export function unfoldSaga(
type: UnfoldSagaHandlerType,
callback: UnfoldSagaCallbacksType,
): SagaIterator;
export function unfoldSaga(type: UnfoldSagaHandlerType, callback: UnfoldSagaCallbacksType): SagaIterator;
7 changes: 1 addition & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@ export function createAction(type: string): Function {
*/
export function* unfoldSaga(
{ handler, key }: UnfoldSagaHandlerType,
{
onBeginning = noop,
onFailure = noop,
onFinish = noop,
onSuccess = noop,
}: UnfoldSagaCallbacksType = {},
{ onBeginning = noop, onFailure = noop, onFinish = noop, onSuccess = noop }: UnfoldSagaCallbacksType = {},
): Saga<void> {
try {
yield put({ type: createActionTypeOnBeginning(key) });
Expand Down
24 changes: 6 additions & 18 deletions test/unfoldSaga.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ describe('unfoldSaga', () => {

test('should PUT onBeginning action', () => {
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnBeginning(key) }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnBeginning(key) }));
});

test('should CALL onBeginning callback', () => {
Expand All @@ -46,9 +44,7 @@ describe('unfoldSaga', () => {

test('should PUT onSuccess action', () => {
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnSuccess(key) }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnSuccess(key) }));
});

test('should CALL onSuccess callback', () => {
Expand All @@ -57,9 +53,7 @@ describe('unfoldSaga', () => {

test('should PUT onFinish action', () => {
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnFinish(key) }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnFinish(key) }));
});

test('should CALL onFinish callback', () => {
Expand Down Expand Up @@ -88,9 +82,7 @@ describe('unfoldSaga', () => {
test('should PUT onBeginning action', () => {
result = generator.next();
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnBeginning(key) }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnBeginning(key) }));
});

test('should CALL onBeginning callback', () => {
Expand All @@ -106,9 +98,7 @@ describe('unfoldSaga', () => {
test('should PUT onFailure action', () => {
result = generator.throw(fakeError);
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnFailure(key), payload: fakeError }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnFailure(key), payload: fakeError }));
});

test('should CALL onFailure callback', () => {
Expand All @@ -119,9 +109,7 @@ describe('unfoldSaga', () => {
test('should PUT onFinish action', () => {
result = generator.next();
expect(result.done).toBe(false);
expect(result.value).toEqual(
put({ type: createActionTypeOnFinish(key) }),
);
expect(result.value).toEqual(put({ type: createActionTypeOnFinish(key) }));
});

test('should CALL onFinish callback', () => {
Expand Down

0 comments on commit 0cb054f

Please sign in to comment.