Skip to content

Commit

Permalink
fix(store): renamed set method to overwrite
Browse files Browse the repository at this point in the history
Renamed the new set method to overwrite in an effort to avoid conflicts with the entityStore method set.

Closes salesforce#663
PR salesforce#661
  • Loading branch information
Robert Field committed Apr 27, 2021
1 parent 3ee3908 commit f08f610
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions docs/docs/store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export class SessionService {
}
```

### `set()`
### `overwrite()`

When you don't just want to update the current store state but replace the state completely, you can call the `set()` method passing the new `state`:
When you don't just want to update the current store state but replace the state completely, you can call the `overwrite()` method passing the new `state`:

```ts title="session.service.ts"
import { SessionStore } from './session.store';
Expand All @@ -74,12 +74,12 @@ export class SessionService {
constructor(private sessionStore: SessionStore) {}

updateUserName(newName: string) {
this.sessionStore.set({ name: newName });
this.sessionStore.overwrite({ name: newName });
}
}
```

As with `update()` the `set()` method also has a second option which gives you more control. It receives a `callback` function, which gets the current state, and returns a new **immutable** state, which will be the new value of the store. For example:
As with `update()` the `overwrite()` method also has a second option which gives you more control. It receives a `callback` function, which gets the current state, and returns a new **immutable** state, which will be the new value of the store. For example:

```ts title="session.service.ts"
import { SessionStore } from './session.store';
Expand All @@ -88,14 +88,14 @@ export class SessionService {
constructor(private sessionStore: SessionStore) {}

updateUserName(newName: string) {
this.sessionStore.set(state => ({
this.sessionStore.overwrite(state => ({
name: newName
}));
}
}
```

You should use `set()` over `update()` when you want to completely replace the current state at the top level.
You should use `overwrite()` over `update()` when you want to completely replace the current state at the top level.

```ts title="session.store.ts"
type SessionState = SessionStateAuthenticated | SessionStateUnauthenticated;
Expand Down Expand Up @@ -138,7 +138,7 @@ Using the above state model and value of current state, if we call `this.store.u
}
```

Our value for tag has been updated but the user value from our previous state has stayed, this could lead to unintended side effects so in this case `this.store.set({ _tag: 'session-state-unauthenticated' })` may be a better option. When we use the `set()` method the following is the result:
Our value for tag has been updated, but the user value from our previous state has stayed, this could lead to unintended side effects so in this case `this.store.overwrite({ _tag: 'session-state-unauthenticated' })` may be a better option. When we use the `overwrite()` method the following is the result:

```json "using the update() method"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ class ExampleStore extends Store<ExampleState> {

const exampleStore = new ExampleStore();

describe('Store Set', () => {
describe('Store Overwrite', () => {
beforeEach(() => {
exampleStore.reset();
});

it('should set the store value replacing the previous state using a state object', () => {
exampleStore.set({ _tag: 'b', uniqueToB: 'This value is unique to b' });
it('should overwrite the store value replacing the previous state using a state object', () => {
exampleStore.overwrite({ _tag: 'b', uniqueToB: 'This value is unique to b' });
expect(exampleStore._value()).toBeTruthy();
expect(exampleStore._value()).toEqual({_tag: 'b', uniqueToB: 'This value is unique to b'});
});

it('should set the store value replacing the previous state using a callback function', () => {
exampleStore.set((_) => ({ _tag: 'b', uniqueToB: 'This value is unique to b' }));
it('should overwrite the store value replacing the previous state using a callback function', () => {
exampleStore.overwrite((_) => ({ _tag: 'b', uniqueToB: 'This value is unique to b' }));
expect(exampleStore._value()).toBeTruthy();
expect(exampleStore._value()).toEqual({_tag: 'b', uniqueToB: 'This value is unique to b'});
});
Expand Down
18 changes: 9 additions & 9 deletions libs/akita/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,25 @@ export class Store<S = any> {

/**
*
* Set the store's value, replacing the previous value.
* Overwrite the store's value, replacing the previous value.
*
* @example
*
* this.store.update(state => {
* this.store.overwrite(state => {
* return {...}
* })
*/
set(stateCallback: UpdateStateCallback<S>);
overwrite(stateCallback: UpdateStateCallback<S>);
/**
*
* @example
*
* this.store.update({ token: token })
* this.store.overwrite({ token: token })
*/
set(state: S);
set(stateOrCallback: S | UpdateStateCallback<S>): void {
isDev() && setAction('Set');
const withHookFn = (curr: S, newS: S) => this.akitaPreSet(curr, newS as S);
overwrite(state: S);
overwrite(stateOrCallback: S | UpdateStateCallback<S>): void {
isDev() && setAction('Overwrite');
const withHookFn = (curr: S, newS: S) => this.akitaPreOverwrite(curr, newS as S);
this._setState(this.prepareNewState(stateOrCallback, this._value(), withHookFn));
}

Expand Down Expand Up @@ -300,7 +300,7 @@ export class Store<S = any> {
}

// @internal
akitaPreSet(_: Readonly<S>, nextState: Readonly<S>): S {
akitaPreOverwrite(_: Readonly<S>, nextState: Readonly<S>): S {
return nextState;
}

Expand Down

0 comments on commit f08f610

Please sign in to comment.