Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a way for @dispatch methods to return a value that won't trigger a dispatch #497

Merged
merged 6 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/decorators/dispatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('@dispatch', () => {
const { value = null, instanceProperty = null } =
action.payload || {};
return Object.assign({}, state, { value, instanceProperty });
case 'CONDITIONAL_DISPATCH_TEST':
return { ...state, ...action.payload };
default:
return state;
}
Expand All @@ -58,6 +60,23 @@ describe('@dispatch', () => {
};
}

@dispatch()
conditionalDispatchMethod(
shouldDispatch: boolean
): PayloadAction | false {
if (shouldDispatch) {
return {
type: 'CONDITIONAL_DISPATCH_TEST',
payload: {
value: 'Conditional Dispatch Action',
instanceProperty: this.instanceProperty,
},
};
} else {
return false;
}
}

@dispatch()
boundProperty = (value: string): PayloadAction => ({
type: 'TEST',
Expand Down Expand Up @@ -89,6 +108,35 @@ describe('@dispatch', () => {
).toHaveBeenCalledWith(expectedArgs);
});

it('should not call dispatch', () => {
const stateBeforeAction = NgRedux.instance && NgRedux.instance.getState();
const result = instance.conditionalDispatchMethod(false);
expect(result).toBe(false);
expect(NgRedux.instance).toBeTruthy();
expect(NgRedux.instance && NgRedux.instance.getState()).toEqual(
stateBeforeAction
);
});

it('should call dispatch with result of function normally', () => {
const result = <PayloadAction>instance.conditionalDispatchMethod(true);
expect(result.type).toBe('CONDITIONAL_DISPATCH_TEST');
expect(result.payload && result.payload.value).toBe(
'Conditional Dispatch Action'
);
expect(result.payload && result.payload.instanceProperty).toBe('test');
expect(NgRedux.instance).toBeTruthy();
expect(
NgRedux.instance && NgRedux.instance.dispatch
).toHaveBeenCalledWith({
type: 'CONDITIONAL_DISPATCH_TEST',
payload: {
value: 'Conditional Dispatch Action',
instanceProperty: 'test',
},
});
});

it('should work with property initalizers', () => {
const result = instance.boundProperty('bound property');
const expectedArgs = {
Expand Down
8 changes: 5 additions & 3 deletions src/decorators/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export function dispatch(): PropertyDecorator {

const wrapped = function(this: any, ...args: any[]) {
const result = originalMethod.apply(this, args);
const store = getBaseStore(this) || NgRedux.instance;
if (store) {
store.dispatch(result);
if (result !== false) {
const store = getBaseStore(this) || NgRedux.instance;
if (store) {
store.dispatch(result);
}
}
return result;
};
Expand Down