Skip to content

feat(store): add option outsideZone in root store dispatch #106

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion packages/store/src/components/ng-redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export abstract class NgRedux<RootState> implements ObservableStore<RootState> {
abstract provideStore: (store: Store<RootState>) => void;

// Redux Store methods
abstract dispatch: Dispatch<AnyAction>;
abstract dispatch: <A extends AnyAction>(
action: A,
outsideZone?: boolean,
) => A;
abstract getState: () => RootState;
abstract subscribe: (listener: () => void) => Unsubscribe;
abstract replaceReducer: (nextReducer: Reducer<RootState, AnyAction>) => void;
Expand Down
7 changes: 5 additions & 2 deletions packages/store/src/components/root-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ export class RootStore<RootState> extends NgRedux<RootState> {
this.store!.replaceReducer(nextReducer);
};

dispatch: Dispatch<AnyAction> = <A extends AnyAction>(action: A): A => {
dispatch: Dispatch<AnyAction> = <A extends AnyAction>(
action: A,
outsideZone: boolean = false,
): A => {
assert(
!!this.store,
'Dispatch failed: did you forget to configure your store? ' +
'https://github.com/angular-redux/platform/blob/master/packages/store/' +
'README.md#quick-start',
);

if (!NgZone.isInAngularZone()) {
if (!NgZone.isInAngularZone() && !outsideZone) {
return this.ngZone.run(() => this.store!.dispatch(action));
} else {
return this.store!.dispatch(action);
Expand Down