deep-storage
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "deep-storage", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -10,2 +10,21 @@ import deepStorage, { isPathMatch } from '../'; | ||
test('props', () => { | ||
const storage = deepStorage({ | ||
value1: 1, | ||
value2: 'two' | ||
}); | ||
const props = storage.props; | ||
expect(props.value1).toBeTruthy(); | ||
expect(props.value2).toBeTruthy(); | ||
expect(props.value1.state).toEqual(1); | ||
expect(props.value2.state).toEqual('two'); | ||
}); | ||
test('init', () => { | ||
const storage = deepStorage({ | ||
}); | ||
const testStorage = storage.init('test')(1); | ||
expect(testStorage.state).toEqual(1); | ||
}); | ||
test('deep', () => { | ||
@@ -12,0 +31,0 @@ const storage = deepStorage({ |
@@ -51,2 +51,8 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
/** | ||
* Creates a new DeepStorage at this point in the object path and | ||
* gives it an initial value if one hasn't already been set | ||
*/ | ||
init: <DeepState>(...path: Path) => (deepState: DeepState) => DeepStorage<DeepState>; | ||
/** | ||
* Gets the root deep storage | ||
@@ -104,2 +110,3 @@ */ | ||
private id: number = 0; | ||
private initialStates: {[key: string]: any} = {}; | ||
@@ -148,3 +155,3 @@ private subscriptions: { [key: number]: { paths: Path[], callback: StateUpdateCallback } } = {}; | ||
stateIn = <DeepState>(...path: Path) => { | ||
let currentState: any = this.state; | ||
let currentState: any = typeof this.state === 'undefined' ? this.initialStates[''] : this.state; | ||
for (let p of path) { | ||
@@ -155,3 +162,4 @@ if (!(p in currentState)) { | ||
// instead of an object | ||
currentState[p] = {}; | ||
const init = this.initialStates[path.join('.')]; | ||
currentState[p] = typeof init === 'undefined' ? {} : init; | ||
} | ||
@@ -162,2 +170,6 @@ currentState = currentState[p]; | ||
} | ||
init = <DeepState>(...path: Path) => (deepState: DeepState): DeepStorage<DeepState> => { | ||
this.initialStates[path.join('.')] = deepState; | ||
return new NestedDeepStorage<DeepState, State>(path, this); | ||
} | ||
deep = <DeepState>(...path: Path): DeepStorage<DeepState> => { | ||
@@ -188,4 +200,3 @@ return new NestedDeepStorage<DeepState, State>(path, this); | ||
const result = {} as {[P in keyof State]: DeepStorage<State[P]>}; | ||
const state = this.state; | ||
for (let key in state) { | ||
for (let key of Object.keys(this.state)) { | ||
result[key] = this.deep(key); | ||
@@ -223,2 +234,5 @@ } | ||
} | ||
init = <DeepState>(...path: stringOrNumber[]) => (deepState: DeepState): DeepStorage<DeepState> => { | ||
return this.rootStorage.init<DeepState>(...this.path.concat(path))(deepState); | ||
} | ||
deep = <DeepState>(...path: stringOrNumber[]): DeepStorage<DeepState> => { | ||
@@ -241,4 +255,3 @@ return this.rootStorage.deep(...this.path.concat(path)); | ||
const result = {} as {[P in keyof State]: DeepStorage<State[P]>}; | ||
const state = this.state; | ||
for (let key in state) { | ||
for (let key of Object.keys(this.state)) { | ||
result[key] = this.deep(key); | ||
@@ -245,0 +258,0 @@ } |
186558
1177