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

store solution - exposing the subject value #1

Open
TheILPlace opened this issue Jun 5, 2017 · 1 comment
Open

store solution - exposing the subject value #1

TheILPlace opened this issue Jun 5, 2017 · 1 comment

Comments

@TheILPlace
Copy link

in the store.ts

get value() {
return this.subject.value;
}

if the component mutates the value received via this property (for example via a developer mistake), we actually mutate our store !
this is because your store's data is kept in the behaviorSubject .

isnt it better to keep an additional separate private property that will hold our store data.
and in the

set(name: string, state: any) {
this.subject.next({
...this.value, [name]: state
});
}

instead of using 'this.value' (which could have been mutated), we use that private property (and also update it during this process, to have the latest store data).

@toddmotto
Copy link
Member

Hey, yes you could do that - but we define the set/select API at the beginning and re-inforce it's mainly as a learning exercise. The only way we should update it is with .set() as shown :) With things like ngrx/store you needn't worry about things like this, but you could alternatively just not use the get value() {} should you wish to use it in a real project to conceal things a little further :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants