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(store,storeconfig): config for deepFreeze function #209

Merged
merged 2 commits into from
Apr 17, 2019
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
79 changes: 79 additions & 0 deletions akita/__tests__/deepFreeze.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Store } from '../src/store';
import { StoreConfig } from '../src/storeConfig';
import { deepFreeze } from '../src/deepFreeze';

class SpecialObject {
specialString: string = 'special';
specialNumber: number = 2;

constructor(params: Partial<ComplexState>) {
Object.assign(this, params);
}
}

class ComplexState {
propertyString: string = '';
propertyNumber: number = 1;
specialObject: SpecialObject = new SpecialObject({});

constructor(params: Partial<ComplexState>) {
Object.assign(this, params);
}
}

@StoreConfig({
name: 'complexState'
})
class ComplexStore extends Store<ComplexState> {
constructor() {
super(new ComplexState({}));
}
}

const complexStore = new ComplexStore();

describe('store with no custom deepFreeze', () => {
it('should use default function', () => {
expect(complexStore.deepFreeze).toEqual(deepFreeze);
});

it('should freeze all properties', () => {
expect(Object.isFrozen(complexStore._value().propertyNumber)).toBeTruthy();
expect(Object.isFrozen(complexStore._value().propertyString)).toBeTruthy();
expect(Object.isFrozen(complexStore._value().specialObject)).toBeTruthy();
expect(Object.isFrozen(complexStore._value().specialObject.specialNumber)).toBeTruthy();
expect(Object.isFrozen(complexStore._value().specialObject.specialString)).toBeTruthy();
});
});

function deepFreezeCustom(o: ComplexState) {
Object.freeze(o);

return o;
}

@StoreConfig({
name: 'complexState2',
deepFreezeFn: deepFreezeCustom
})
class ComplexStoreCustom extends Store<ComplexState> {
constructor() {
super(new ComplexState({}));
}
}

const complexStoreCustom = new ComplexStoreCustom();

describe('store with custom deepFreeze', () => {
it('should use custom function', () => {
expect(complexStoreCustom.deepFreeze).toEqual(deepFreezeCustom);
});

it('should not freeze all properties', () => {
expect(Object.isFrozen(complexStoreCustom._value().propertyNumber)).toBeTruthy();
expect(Object.isFrozen(complexStoreCustom._value().propertyString)).toBeTruthy();
expect(Object.isFrozen(complexStoreCustom._value().specialObject)).toBeFalsy();
expect(Object.isFrozen(complexStoreCustom._value().specialObject.specialNumber)).toBeTruthy();
expect(Object.isFrozen(complexStoreCustom._value().specialObject.specialString)).toBeTruthy();
});
});
7 changes: 6 additions & 1 deletion akita/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,19 @@ export class Store<S> {
return this.config.idKey || this.options.idKey || DEFAULT_ID_KEY;
}

// @internal
get deepFreeze() {
return this.config.deepFreezeFn || this.options.deepFreezeFn || deepFreeze;
}

// @internal
get cacheConfig() {
return this.config.cache || this.options.cache;
}

// @internal
_setState(newStateFn: (state: Readonly<S>) => S, _dispatchAction = true) {
this.storeValue = __DEV__ ? deepFreeze(newStateFn(this._value())) : newStateFn(this._value());
this.storeValue = __DEV__ ? this.deepFreeze(newStateFn(this._value())) : newStateFn(this._value());

if (!this.store) {
this.store = new BehaviorSubject(this.storeValue);
Expand Down
1 change: 1 addition & 0 deletions akita/src/storeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type StoreConfigOptions = {
idKey?: string;
storeName?: string;
cache?: { ttl: number };
deepFreezeFn?: (o: any) => any;
};

export type UpdatableStoreConfigOptions = {
Expand Down