deep-storage
Advanced tools
Comparing version
@@ -18,2 +18,3 @@ import { DeepStorage } from "./index"; | ||
rerun(): Promise<DeepAsyncData<Request, Response>>; | ||
storage: DeepStorage<DeepAsyncData<Request, Response>>; | ||
} | ||
@@ -34,3 +35,3 @@ export declare class AlreadyRunningError extends Error { | ||
} | ||
declare const _default: <Request, Response>(storage: DeepStorage<DeepAsyncData<Request, Response>>, process: (request: Request) => Promise<Response>) => DefaultDeepAsync<Request, Response>; | ||
declare const _default: <Request, Response>(storage: DeepStorage<DeepAsyncData<Request, Response>, {}>, process: (request: Request) => Promise<Response>) => DefaultDeepAsync<Request, Response>; | ||
export default _default; |
@@ -9,3 +9,3 @@ export declare type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
} | ||
export interface DeepStorage<State> extends DeepSubscriptions { | ||
export interface DeepStorage<State, RootState = {}> extends DeepSubscriptions { | ||
/** | ||
@@ -41,2 +41,6 @@ * sets a value in deep storage by path and notifies subscribers. shortcut for | ||
deep: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
/** | ||
* Gets the root deep storage | ||
*/ | ||
root: () => DeepStorage<RootState>; | ||
} | ||
@@ -52,3 +56,3 @@ /** | ||
export declare type Path = stringOrNumber[]; | ||
export declare class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
export declare class DefaultDeepStorage<State> implements DeepStorage<State, State> { | ||
state: State; | ||
@@ -66,3 +70,3 @@ private id; | ||
stateIn: <DeepState>(...path: (string | number)[]) => any; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState>; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState, {}>; | ||
subscription: (callback: StateUpdateCallback) => { | ||
@@ -72,7 +76,8 @@ subscribeTo: (...path: (string | number)[]) => void; | ||
}; | ||
root: () => this; | ||
} | ||
export declare class NestedDeepStorage<RootState, State> implements DeepStorage<State> { | ||
export declare class NestedDeepStorage<State, RootState> implements DeepStorage<State, RootState> { | ||
path: Path; | ||
root: DeepStorage<RootState>; | ||
constructor(path: Path, root: DeepStorage<RootState>); | ||
rootStorage: DeepStorage<RootState>; | ||
constructor(path: Path, rootStorage: DeepStorage<RootState>); | ||
setIn: (...path: (string | number)[]) => <DeepState>(newValue: DeepState) => Promise<DeepState>; | ||
@@ -84,4 +89,5 @@ update: (callback: (s: State) => State) => Promise<State>; | ||
stateIn: <DeepState>(...path: (string | number)[]) => DeepState; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState>; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState, {}>; | ||
subscription: (callback: StateUpdateCallback) => DeepSubscription; | ||
root: () => DeepStorage<RootState, {}>; | ||
} | ||
@@ -94,3 +100,3 @@ export declare function parsePath(path: Path | stringOrNumber): Path; | ||
}; | ||
export declare const deepStorage: <State>(s: State) => DeepStorage<State>; | ||
export declare const deepStorage: <State>(s: State) => DeepStorage<State, {}>; | ||
export default deepStorage; |
@@ -164,2 +164,3 @@ "use strict"; | ||
}; | ||
this.root = function () { return _this; }; | ||
} | ||
@@ -170,6 +171,6 @@ return DefaultDeepStorage; | ||
var NestedDeepStorage = (function () { | ||
function NestedDeepStorage(path, root) { | ||
function NestedDeepStorage(path, rootStorage) { | ||
var _this = this; | ||
this.path = path; | ||
this.root = root; | ||
this.rootStorage = rootStorage; | ||
this.setIn = function () { | ||
@@ -181,3 +182,3 @@ var path = []; | ||
return function (newValue) { | ||
return (_a = _this.root).setIn.apply(_a, _this.path.concat(path))(newValue); | ||
return (_a = _this.rootStorage).setIn.apply(_a, _this.path.concat(path))(newValue); | ||
var _a; | ||
@@ -187,3 +188,3 @@ }; | ||
this.update = function (callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path)(callback); | ||
return (_a = _this.rootStorage).updateIn.apply(_a, _this.path)(callback); | ||
var _a; | ||
@@ -197,3 +198,3 @@ }; | ||
return function (callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path.concat(path))(callback); | ||
return (_a = _this.rootStorage).updateIn.apply(_a, _this.path.concat(path))(callback); | ||
var _a; | ||
@@ -203,3 +204,3 @@ }; | ||
this.updateProperty = function (key, callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path.concat(key))(callback); | ||
return (_a = _this.rootStorage).updateIn.apply(_a, _this.path.concat(key))(callback); | ||
var _a; | ||
@@ -212,3 +213,3 @@ }; | ||
} | ||
return (_a = _this.root).stateIn.apply(_a, _this.path.concat(path)); | ||
return (_a = _this.rootStorage).stateIn.apply(_a, _this.path.concat(path)); | ||
var _a; | ||
@@ -221,7 +222,7 @@ }; | ||
} | ||
return (_a = _this.root).deep.apply(_a, _this.path.concat(path)); | ||
return (_a = _this.rootStorage).deep.apply(_a, _this.path.concat(path)); | ||
var _a; | ||
}; | ||
this.subscription = function (callback) { | ||
var rootSubscription = _this.root.subscription(function (path, newState, oldState) { | ||
var rootSubscription = _this.rootStorage.subscription(function (path, newState, oldState) { | ||
callback(path.slice(path.length - _this.path.length, path.length), newState, oldState); | ||
@@ -240,6 +241,7 @@ }); | ||
}; | ||
this.root = function () { return _this.rootStorage; }; | ||
} | ||
Object.defineProperty(NestedDeepStorage.prototype, "state", { | ||
get: function () { | ||
return (_a = this.root).stateIn.apply(_a, this.path); | ||
return (_a = this.rootStorage).stateIn.apply(_a, this.path); | ||
var _a; | ||
@@ -246,0 +248,0 @@ }, |
{ | ||
"name": "deep-storage", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Simple observable state management for reactive applications", | ||
"main": "lib/index.js", | ||
"main": "./lib/index.js", | ||
"license": "MIT", | ||
@@ -21,6 +21,6 @@ "repository": { | ||
"test": "jest", | ||
"build": "rimraf lib && tsc" | ||
"build": "rimraf lib && tsc -p tsconfig.prod.json" | ||
}, | ||
"dependencies": {}, | ||
"types": "lib/index.d.ts" | ||
"types": "./lib/index.d.ts" | ||
} |
@@ -15,3 +15,3 @@ import { DeepStorage } from "./index"; | ||
response?: Response; | ||
error?: any | ||
error?: any; | ||
} | ||
@@ -22,2 +22,3 @@ | ||
rerun(): Promise<DeepAsyncData<Request, Response>>; | ||
storage: DeepStorage<DeepAsyncData<Request, Response>> | ||
} | ||
@@ -24,0 +25,0 @@ |
@@ -11,3 +11,3 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
export interface DeepStorage<State> extends DeepSubscriptions { | ||
export interface DeepStorage<State, RootState = {}> extends DeepSubscriptions { | ||
@@ -50,2 +50,8 @@ /** | ||
deep: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
/** | ||
* Gets the root deep storage | ||
*/ | ||
root: () => DeepStorage<RootState>; | ||
} | ||
@@ -83,3 +89,3 @@ | ||
export class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
export class DefaultDeepStorage<State> implements DeepStorage<State, State> { | ||
@@ -143,3 +149,3 @@ private id: number = 0; | ||
deep = <DeepState>(...path: Path): DeepStorage<DeepState> => { | ||
return new NestedDeepStorage<State, DeepState>(path, this); | ||
return new NestedDeepStorage<DeepState, State>(path, this); | ||
} | ||
@@ -164,35 +170,36 @@ subscription = (callback: StateUpdateCallback) => { | ||
} | ||
root = () => this; | ||
} | ||
export class NestedDeepStorage<RootState, State> implements DeepStorage<State> { | ||
export class NestedDeepStorage<State, RootState> implements DeepStorage<State, RootState> { | ||
constructor(public path: Path, public root: DeepStorage<RootState>) { | ||
constructor(public path: Path, public rootStorage: DeepStorage<RootState>) { | ||
} | ||
setIn = (...path: stringOrNumber[]) => <DeepState>(newValue: DeepState): Promise<DeepState> => { | ||
return this.root.setIn(...this.path.concat(path))(newValue); | ||
return this.rootStorage.setIn(...this.path.concat(path))(newValue); | ||
} | ||
update = (callback: (s: State) => State): Promise<State> => { | ||
return this.root.updateIn(...this.path)(callback); | ||
return this.rootStorage.updateIn(...this.path)(callback); | ||
} | ||
updateIn = (...path: stringOrNumber[]) => <DeepState>(callback: (s: DeepState) => DeepState): Promise<DeepState> => { | ||
return this.root.updateIn(...this.path.concat(path))(callback); | ||
return this.rootStorage.updateIn(...this.path.concat(path))(callback); | ||
} | ||
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); | ||
return this.rootStorage.updateIn(...this.path.concat(key))(callback); | ||
} | ||
get state() { return this.root.stateIn<State>(...this.path); } | ||
get state() { return this.rootStorage.stateIn<State>(...this.path); } | ||
stateIn = <DeepState>(...path: stringOrNumber[]): DeepState => { | ||
return this.root.stateIn(...this.path.concat(path)); | ||
return this.rootStorage.stateIn(...this.path.concat(path)); | ||
} | ||
deep = <DeepState>(...path: stringOrNumber[]): DeepStorage<DeepState> => { | ||
return this.root.deep(...this.path.concat(path)); | ||
return this.rootStorage.deep(...this.path.concat(path)); | ||
} | ||
subscription = (callback: StateUpdateCallback): DeepSubscription => { | ||
const rootSubscription = this.root.subscription((path, newState, oldState) => { | ||
const rootSubscription = this.rootStorage.subscription((path, newState, oldState) => { | ||
callback(path.slice(path.length - this.path.length, path.length), newState, oldState); | ||
@@ -207,3 +214,3 @@ }); | ||
} | ||
root = () => this.rootStorage; | ||
} | ||
@@ -210,0 +217,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
177578
2.14%19
11.76%985
13.61%