Skip to content

Commit

Permalink
feat(store): dispatch one update action when features are added or re…
Browse files Browse the repository at this point in the history
…moved (#1240)

BREAKING CHANGE:

BEFORE:
```ts
{type: '@ngrx/store/update-reducers', feature: 'feature1'}
{type: '@ngrx/store/update-reducers', feature: 'feature2'}
```

AFTER:
```ts
{type: '@ngrx/store/update-reducers', features: ['feature1',
'feature2']}
```
  • Loading branch information
timdeschryver authored and brandonroberts committed Aug 16, 2018
1 parent b3fc5dd commit 0b90f91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
38 changes: 12 additions & 26 deletions modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,15 @@ describe('ngRx Store', () => {
store.addReducer(key, counterReducer);
expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: key,
features: [key],
});
});

it('should dispatch an update reducers action when a reducer is removed', () => {
store.removeReducer(key);
expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: key,
features: [key],
});
});
});
Expand Down Expand Up @@ -373,11 +373,11 @@ describe('ngRx Store', () => {

expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: 'feature1',
features: ['feature1'],
});
});

it('should dispatch an update reducers action for each feature that is added', () => {
it('should dispatch an update reducers action when multiple features are added', () => {
reducerManager.addFeatures([
createFeature({
key: 'feature1',
Expand All @@ -387,18 +387,10 @@ describe('ngRx Store', () => {
}),
]);

expect(reducerManagerDispatcherSpy).toHaveBeenCalledTimes(2);

// get the first argument for the first call
expect(reducerManagerDispatcherSpy.calls.argsFor(0)[0]).toEqual({
type: UPDATE,
feature: 'feature1',
});

// get the first argument for the second call
expect(reducerManagerDispatcherSpy.calls.argsFor(1)[0]).toEqual({
expect(reducerManagerDispatcherSpy).toHaveBeenCalledTimes(1);
expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: 'feature2',
features: ['feature1', 'feature2'],
});
});

Expand All @@ -411,11 +403,11 @@ describe('ngRx Store', () => {

expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: 'feature1',
features: ['feature1'],
});
});

it('should dispatch an update reducers action for each feature that is removed', () => {
it('should dispatch an update reducers action when multiple features are removed', () => {
reducerManager.removeFeatures([
createFeature({
key: 'feature1',
Expand All @@ -425,16 +417,10 @@ describe('ngRx Store', () => {
}),
]);

// get the first argument for the first call
expect(reducerManagerDispatcherSpy.calls.argsFor(0)[0]).toEqual({
type: UPDATE,
feature: 'feature1',
});

// get the first argument for the second call
expect(reducerManagerDispatcherSpy.calls.argsFor(1)[0]).toEqual({
expect(reducerManagerDispatcherSpy).toHaveBeenCalledTimes(1);
expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
type: UPDATE,
feature: 'feature2',
features: ['feature1', 'feature2'],
});
});

Expand Down
9 changes: 3 additions & 6 deletions modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>

private updateReducers(featureKeys: string[]) {
this.next(this.reducerFactory(this.reducers, this.initialState));

featureKeys.forEach(feature => {
this.dispatcher.next(<Action>{
type: UPDATE,
feature,
});
this.dispatcher.next(<Action>{
type: UPDATE,
features: featureKeys,
});
}

Expand Down

0 comments on commit 0b90f91

Please sign in to comment.