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

feat(component-store): add ability for patchState to accept Observable #2937

Merged
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
38 changes: 38 additions & 0 deletions modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
interval,
timer,
Observable,
from,
} from 'rxjs';
import {
delayWhen,
Expand All @@ -15,6 +16,8 @@ import {
map,
tap,
finalize,
delay,
concatMap,
} from 'rxjs/operators';

describe('Component Store', () => {
Expand Down Expand Up @@ -96,6 +99,19 @@ describe('Component Store', () => {
);
});

it('throws an Error when patchState with Observable is called before initialization', () => {
const componentStore = new ComponentStore();

expect(() => {
componentStore.patchState(of({ foo: 'bar' }));
}).toThrow(
new Error(
'ComponentStore has not been initialized yet. ' +
'Please make sure it is initialized before updating/getting.'
)
);
});

it(
'throws an Error when patchState with a function/callback is called' +
' before initialization',
Expand Down Expand Up @@ -566,6 +582,28 @@ describe('Component Store', () => {
})
);

it(
'with the values from Observable',
marbles((m) => {
componentStore.patchState(
from([
{ value1: 'foo' },
{ value2: { foo: 'foo2' } },
{ value1: 'baz' },
]).pipe(concatMap((partialState) => of(partialState).pipe(delay(3))))
);

m.expect(componentStore.state$).toBeObservable(
m.hot('a--b--c--d', {
a: INIT_STATE,
b: { ...INIT_STATE, value1: 'foo' },
c: { value1: 'foo', value2: { foo: 'foo2' } },
d: { value1: 'baz', value2: { foo: 'foo2' } },
})
);
})
);

it(
'with a value based on the previous state',
marbles((m) => {
Expand Down
23 changes: 12 additions & 11 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,20 @@ export class ComponentStore<T extends object> implements OnDestroy {
* @throws Error if the state is not initialized.
*/
patchState(
partialStateOrUpdaterFn: Partial<T> | ((state: T) => Partial<T>)
partialStateOrUpdaterFn:
| Partial<T>
| Observable<Partial<T>>
| ((state: T) => Partial<T>)
): void {
this.setState((state) => {
const patchedState =
typeof partialStateOrUpdaterFn === 'function'
? partialStateOrUpdaterFn(state)
: partialStateOrUpdaterFn;
const patchedState =
typeof partialStateOrUpdaterFn === 'function'
? partialStateOrUpdaterFn(this.get())
: partialStateOrUpdaterFn;

return {
...state,
...patchedState,
};
});
this.updater((state, partialState: Partial<T>) => ({
...state,
...partialState,
}))(patchedState);
}

protected get(): T;
Expand Down