deep-storage
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -35,3 +35,3 @@ export declare type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
*/ | ||
stateIn: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
stateIn: <DeepState>(...path: Path) => DeepState; | ||
/** | ||
@@ -56,5 +56,4 @@ * Creates a new DeepStorage at this point in the object path | ||
private id; | ||
path: Path; | ||
private subscriptions; | ||
constructor(state: State, ...path: Path); | ||
constructor(state: State); | ||
update: (callback: (s: State) => State) => void; | ||
@@ -74,2 +73,15 @@ updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
} | ||
export declare class NestedDeepStorage<RootState, State> implements DeepStorage<State> { | ||
path: Path; | ||
root: DeepStorage<RootState>; | ||
constructor(path: Path, root: DeepStorage<RootState>); | ||
setIn: (...path: (string | number)[]) => <DeepState>(newValue: DeepState) => void; | ||
update: (callback: (s: State) => State) => void; | ||
updateIn: (...path: (string | number)[]) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
readonly state: State; | ||
stateIn: <DeepState>(...path: (string | number)[]) => DeepState; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState>; | ||
subscription: (callback: StateUpdateCallback) => DeepSubscription; | ||
} | ||
export declare function parsePath(path: Path | stringOrNumber): Path; | ||
@@ -76,0 +88,0 @@ export declare function parsePaths(paths: { |
@@ -26,6 +26,2 @@ "use strict"; | ||
function DefaultDeepStorage(state) { | ||
var path = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
path[_i - 1] = arguments[_i]; | ||
} | ||
var _this = this; | ||
@@ -74,3 +70,3 @@ this.state = state; | ||
} | ||
var fullPath = _this.path.concat(path); | ||
var fullPath = path; | ||
for (var subscriberId in _this.subscriptions) { | ||
@@ -91,4 +87,4 @@ var subscriber = _this.subscriptions[subscriberId]; | ||
var currentState = _this.state; | ||
for (var _a = 0, _b = _this.path.concat(path); _a < _b.length; _a++) { | ||
var p = _b[_a]; | ||
for (var _a = 0, path_1 = path; _a < path_1.length; _a++) { | ||
var p = path_1[_a]; | ||
if (!(p in currentState)) { | ||
@@ -109,3 +105,3 @@ // todo: consider looking ahead to see if the next | ||
} | ||
return new (DefaultDeepStorage.bind.apply(DefaultDeepStorage, [void 0, _this.stateIn.apply(_this, path)].concat(_this.path)))(); | ||
return new NestedDeepStorage(path, _this); | ||
}; | ||
@@ -134,3 +130,2 @@ this.subscription = function (callback) { | ||
}; | ||
this.path = path; | ||
} | ||
@@ -140,2 +135,78 @@ return DefaultDeepStorage; | ||
exports.DefaultDeepStorage = DefaultDeepStorage; | ||
var NestedDeepStorage = (function () { | ||
function NestedDeepStorage(path, root) { | ||
var _this = this; | ||
this.path = path; | ||
this.root = root; | ||
this.setIn = function () { | ||
var path = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
path[_i] = arguments[_i]; | ||
} | ||
return function (newValue) { | ||
return (_a = _this.root).setIn.apply(_a, _this.path.concat(path))(newValue); | ||
var _a; | ||
}; | ||
}; | ||
this.update = function (callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path)(callback); | ||
var _a; | ||
}; | ||
this.updateIn = function () { | ||
var path = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
path[_i] = arguments[_i]; | ||
} | ||
return function (callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path.concat(path))(callback); | ||
var _a; | ||
}; | ||
}; | ||
this.updateProperty = function (key, callback) { | ||
return (_a = _this.root).updateIn.apply(_a, _this.path.concat(key))(callback); | ||
var _a; | ||
}; | ||
this.stateIn = function () { | ||
var path = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
path[_i] = arguments[_i]; | ||
} | ||
return (_a = _this.root).stateIn.apply(_a, _this.path.concat(path)); | ||
var _a; | ||
}; | ||
this.deep = function () { | ||
var path = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
path[_i] = arguments[_i]; | ||
} | ||
return (_a = _this.root).deep.apply(_a, _this.path.concat(path)); | ||
var _a; | ||
}; | ||
this.subscription = function (callback) { | ||
var rootSubscription = _this.root.subscription(function (path, newState, oldState) { | ||
callback(path.slice(path.length - _this.path.length, path.length), newState, oldState); | ||
}); | ||
return { | ||
subscribeTo: function () { | ||
var path = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
path[_i] = arguments[_i]; | ||
} | ||
return rootSubscription.subscribeTo.apply(rootSubscription, _this.path.concat(path)); | ||
}, | ||
cancel: rootSubscription.cancel | ||
}; | ||
}; | ||
} | ||
Object.defineProperty(NestedDeepStorage.prototype, "state", { | ||
get: function () { | ||
return (_a = this.root).stateIn.apply(_a, this.path); | ||
var _a; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return NestedDeepStorage; | ||
}()); | ||
exports.NestedDeepStorage = NestedDeepStorage; | ||
function numberOrString(value) { | ||
@@ -142,0 +213,0 @@ var parsed = parseInt(value); |
{ | ||
"name": "deep-storage", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -43,3 +43,3 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
*/ | ||
stateIn: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
stateIn: <DeepState>(...path: Path) => DeepState; | ||
@@ -87,7 +87,5 @@ /** | ||
private id: number = 0; | ||
public path: Path; | ||
private subscriptions: { [key: number]: { paths: Path[], callback: StateUpdateCallback } } = {}; | ||
constructor(public state: State, ...path: Path) { | ||
this.path = path; | ||
constructor(public state: State) { | ||
} | ||
@@ -105,3 +103,3 @@ update = (callback: (s: State) => State): void => { | ||
this.update(oldState => { | ||
for(let key in partial) { | ||
for (let key in partial) { | ||
oldState[key] = partial[key]; | ||
@@ -123,3 +121,3 @@ } | ||
} | ||
const fullPath = this.path.concat(path); | ||
const fullPath = path; | ||
for (let subscriberId in this.subscriptions) { | ||
@@ -135,3 +133,3 @@ const subscriber = this.subscriptions[subscriberId]; | ||
let currentState: any = this.state; | ||
for (let p of this.path.concat(path)) { | ||
for (let p of path) { | ||
if (!(p in currentState)) { | ||
@@ -148,3 +146,3 @@ // todo: consider looking ahead to see if the next | ||
deep = <DeepState>(...path: Path): DeepStorage<DeepState> => { | ||
return new DefaultDeepStorage<DeepState>(this.stateIn<DeepState>(...path), ...this.path); | ||
return new NestedDeepStorage<State, DeepState>(path, this); | ||
} | ||
@@ -171,2 +169,45 @@ subscription = (callback: StateUpdateCallback) => { | ||
export class NestedDeepStorage<RootState, State> implements DeepStorage<State> { | ||
constructor(public path: Path, public root: DeepStorage<RootState>) { | ||
} | ||
setIn = (...path: stringOrNumber[]) => <DeepState>(newValue: DeepState): void => { | ||
return this.root.setIn(...this.path.concat(path))(newValue); | ||
} | ||
update = (callback: (s: State) => State): void => { | ||
return this.root.updateIn(...this.path)(callback); | ||
} | ||
updateIn = (...path: stringOrNumber[]) => <DeepState>(callback: (s: DeepState) => DeepState): void => { | ||
return this.root.updateIn(...this.path.concat(path))(callback); | ||
} | ||
updateProperty = <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]): void => { | ||
return this.root.updateIn(...this.path.concat(key))(callback); | ||
} | ||
get state() { return this.root.stateIn<State>(...this.path); } | ||
stateIn = <DeepState>(...path: stringOrNumber[]): DeepState => { | ||
return this.root.stateIn(...this.path.concat(path)); | ||
} | ||
deep = <DeepState>(...path: stringOrNumber[]): DeepStorage<DeepState> => { | ||
return this.root.deep(...this.path.concat(path)); | ||
} | ||
subscription = (callback: StateUpdateCallback): DeepSubscription => { | ||
const rootSubscription = this.root.subscription((path, newState, oldState) => { | ||
callback(path.slice(path.length - this.path.length, path.length), newState, oldState); | ||
}); | ||
return { | ||
subscribeTo: (...path: Path) => { | ||
return rootSubscription.subscribeTo(...this.path.concat(path)); | ||
}, | ||
cancel: rootSubscription.cancel | ||
} | ||
} | ||
} | ||
function numberOrString(value: string): stringOrNumber { | ||
@@ -178,3 +219,3 @@ const parsed = parseInt(value); | ||
export function parsePath(path: Path | stringOrNumber): Path { | ||
if(path instanceof Array) { | ||
if (path instanceof Array) { | ||
return path; | ||
@@ -188,5 +229,5 @@ } else if (typeof path === 'number') { | ||
export function parsePaths(paths: {[key: string]: Path | stringOrNumber}): {[key: string]: Path} { | ||
const result: {[key: string]: Path} = {}; | ||
for(let key in paths) { | ||
export function parsePaths(paths: { [key: string]: Path | stringOrNumber }): { [key: string]: Path } { | ||
const result: { [key: string]: Path } = {}; | ||
for (let key in paths) { | ||
result[key] = parsePath(paths[key]); | ||
@@ -193,0 +234,0 @@ } |
@@ -9,3 +9,7 @@ { | ||
"jsx": "react", | ||
"declaration": true | ||
"declaration": true, | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
] | ||
}, | ||
@@ -12,0 +16,0 @@ "include": [ |
Sorry, the diff of this file is not supported yet
170344
17
819