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 patchState method #2788

Merged
merged 2 commits into from
Nov 23, 2020
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
71 changes: 71 additions & 0 deletions modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ describe('Component Store', () => {
})
);

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

expect(() => {
componentStore.patchState({ 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',
() => {
const componentStore = new ComponentStore();

expect(() => {
componentStore.patchState(() => ({ 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 updater is called before initialization',
marbles((m) => {
Expand Down Expand Up @@ -511,6 +541,47 @@ describe('Component Store', () => {
);
});

describe('patches the state', () => {
interface State {
value1: string;
value2: { foo: string };
}
const INIT_STATE: State = { value1: 'value1', value2: { foo: 'bar' } };
let componentStore: ComponentStore<State>;

beforeEach(() => {
componentStore = new ComponentStore(INIT_STATE);
});

it(
'with a specific value',
marbles((m) => {
componentStore.patchState({ value1: 'val1' });

m.expect(componentStore.state$).toBeObservable(
m.hot('s', {
s: { ...INIT_STATE, value1: 'val1' },
})
);
})
);

it(
'with a value based on the previous state',
marbles((m) => {
componentStore.patchState((state) => ({
value2: { foo: `${state.value2.foo}2` },
}));

m.expect(componentStore.state$).toBeObservable(
m.hot('s', {
s: { ...INIT_STATE, value2: { foo: 'bar2' } },
})
);
})
);
});

describe('selector', () => {
interface State {
value: string;
Expand Down
23 changes: 23 additions & 0 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,29 @@ export class ComponentStore<T extends object> implements OnDestroy {
}
}

/**
* Patches the state with provided partial state.
*
* @param partialStateOrUpdaterFn a partial state or a partial updater
* function that accepts the state and returns the partial state.
* @throws Error if the state is not initialized.
*/
patchState(
partialStateOrUpdaterFn: Partial<T> | ((state: T) => Partial<T>)
): void {
this.setState((state) => {
const patchedState =
typeof partialStateOrUpdaterFn === 'function'
? partialStateOrUpdaterFn(state)
: partialStateOrUpdaterFn;

return {
...state,
...patchedState,
};
});
}

protected get(): T;
protected get<R>(projector: (s: T) => R): R;
protected get<R>(projector?: (s: T) => R): R | T {
Expand Down