Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
state freeze done
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic committed Sep 1, 2020
1 parent 0ebc0c8 commit 225a86f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"new-parens": "error",
"no-bitwise": "off",
"no-extra-boolean-cast": "off",
"no-prototype-builtins": "off",
"no-restricted-imports": ["error", "rxjs/Rx"],
"no-shadow": [
"error",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ as a state management solution.
## V1.0.0 To-Do List

- Selector composition ✔️
- State immutability runtime checks
- State immutability runtime checks ✔️
- Support for lazy loading feature modules ✔️

## Support
Expand Down
4 changes: 2 additions & 2 deletions projects/juliette-ng/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "juliette-ng",
"version": "0.11.2",
"version": "0.12.0",
"peerDependencies": {
"juliette": "0.11.2",
"juliette": "0.12.0",
"@angular/core": "^10.0.5",
"rxjs": "^6.5.5"
},
Expand Down
4 changes: 2 additions & 2 deletions projects/juliette-react/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "juliette-react",
"version": "0.11.2",
"version": "0.12.0",
"main": "public-api.js",
"types": "public-api.d.ts",
"peerDependencies": {
"juliette": "0.11.2",
"juliette": "0.12.0",
"react": "^16.13.1"
}
}
2 changes: 1 addition & 1 deletion projects/juliette/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juliette",
"version": "0.11.2",
"version": "0.12.0",
"main": "public-api.js",
"types": "public-api.d.ts",
"peerDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions projects/juliette/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const deepFreeze = (obj: any): any => {
if (!Object.isFrozen(obj)) Object.freeze(obj);

for (const key in obj) {
if (obj && obj.hasOwnProperty(key) && typeof obj[key] === 'object') deepFreeze(obj[key]);
}

return obj;
};
10 changes: 5 additions & 5 deletions projects/juliette/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { Handler, Selector } from './models';
import { log } from './log';
import { deepFreeze } from './helpers';

export class Store<T> {
private state: BehaviorSubject<T>;
private handlers = new Subject<Handler<any, any>>();

state$: Observable<T>;
handlers$: Observable<Handler<any, any>>;
handlers$ = this.handlers.asObservable();

constructor(initialState: T) {
this.state = new BehaviorSubject(initialState);
this.state = new BehaviorSubject(deepFreeze(initialState));
this.state$ = this.state.asObservable();
this.handlers$ = this.handlers.asObservable();
}

dispatch(handler: Handler<any, any>): void {
Expand All @@ -22,7 +22,7 @@ export class Store<T> {

this.state.next({
...this.state.value,
[handler.featureKey]: handler.reducer(currentState, handler.payload),
[handler.featureKey]: handler.reducer(deepFreeze(currentState), handler.payload),
});
}

Expand All @@ -43,7 +43,7 @@ export class Store<T> {
}

addFeatureState(featureKey: keyof T, initialState: T[keyof T]): void {
this.state.next({ ...this.state.value, [featureKey]: initialState });
this.state.next({ ...this.state.value, [featureKey]: deepFreeze(initialState) });
}
}

Expand Down

0 comments on commit 225a86f

Please sign in to comment.