deep-storage
Advanced tools
Comparing version 0.1.3 to 0.2.0
{ | ||
"name": "deep-storage", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -35,9 +35,9 @@ import { DeepStorage } from "./index"; | ||
if (this.status === AsyncStatus.Running) throw new AlreadyRunningError(); | ||
this.storage.update(state => ({ ...state, status: AsyncStatus.Running, request, response: undefined, error: undefined })); | ||
await this.storage.update(state => ({ ...state, status: AsyncStatus.Running, request, response: undefined, error: undefined })); | ||
try { | ||
const response = await this.process(request); | ||
this.storage.update(state => ({ ...state, status: AsyncStatus.Succeeded, response, error: undefined })); | ||
await this.storage.update(state => ({ ...state, status: AsyncStatus.Succeeded, response, error: undefined })); | ||
return this.storage.state; | ||
} catch (error) { | ||
this.storage.update(state => ({ ...state, status: AsyncStatus.Failed, error, response: undefined })); | ||
await this.storage.update(state => ({ ...state, status: AsyncStatus.Failed, error, response: undefined })); | ||
return this.storage.state; | ||
@@ -44,0 +44,0 @@ } |
@@ -17,3 +17,3 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
*/ | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => void; | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => Promise<DeepState>; | ||
@@ -23,3 +23,3 @@ /** | ||
*/ | ||
update: (callback: (s: State) => State) => void; | ||
update: (callback: (s: State) => State) => Promise<State>; | ||
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => Promise<DeepState>; | ||
@@ -36,3 +36,3 @@ /** | ||
*/ | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => Promise<State[Key]>; | ||
@@ -85,4 +85,2 @@ /** | ||
export default <State>(s: State): DeepStorage<State> => new DefaultDeepStorage(s); | ||
export class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
@@ -95,10 +93,10 @@ | ||
} | ||
update = (callback: (s: State) => State): void => { | ||
update = (callback: (s: State) => State): Promise<State> => { | ||
return this.updateIn()(callback); | ||
} | ||
updateProperty = <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]): void => { | ||
updateProperty = <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]): Promise<State[Key]> => { | ||
return this.updateIn(key)(callback); | ||
} | ||
setIn = (...path: Path) => <DeepState>(newValue: DeepState) => { | ||
this.updateIn(...path)(() => newValue); | ||
return this.updateIn(...path)(() => newValue); | ||
} | ||
@@ -113,3 +111,3 @@ merge = (partial: {[P in keyof State]?: State[P]}) => { | ||
} | ||
updateIn = (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState): void => { | ||
updateIn = (...path: Path) => async <DeepState>(callback: (s: DeepState) => DeepState): Promise<DeepState> => { | ||
const oldState = this.stateIn<DeepState>(...path); | ||
@@ -133,2 +131,3 @@ const newState = callback(oldState); | ||
} | ||
return newState; | ||
} | ||
@@ -176,15 +175,15 @@ stateIn = <DeepState>(...path: Path) => { | ||
setIn = (...path: stringOrNumber[]) => <DeepState>(newValue: DeepState): void => { | ||
setIn = (...path: stringOrNumber[]) => <DeepState>(newValue: DeepState): Promise<DeepState> => { | ||
return this.root.setIn(...this.path.concat(path))(newValue); | ||
} | ||
update = (callback: (s: State) => State): void => { | ||
update = (callback: (s: State) => State): Promise<State> => { | ||
return this.root.updateIn(...this.path)(callback); | ||
} | ||
updateIn = (...path: stringOrNumber[]) => <DeepState>(callback: (s: DeepState) => DeepState): void => { | ||
updateIn = (...path: stringOrNumber[]) => <DeepState>(callback: (s: DeepState) => DeepState): Promise<DeepState> => { | ||
return this.root.updateIn(...this.path.concat(path))(callback); | ||
} | ||
updateProperty = <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]): void => { | ||
updateProperty = <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]): Promise<State[Key]> => { | ||
return this.root.updateIn(...this.path.concat(key))(callback); | ||
@@ -236,2 +235,5 @@ } | ||
return result; | ||
} | ||
} | ||
export const deepStorage = <State>(s: State): DeepStorage<State> => new DefaultDeepStorage(s); | ||
export default deepStorage; |
170585
821