deep-storage
Advanced tools
Comparing version 0.1.0 to 0.1.1
export declare type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
export interface DeepSubscriptions { | ||
subscription: (callback: StateUpdateCallback) => Subscription; | ||
/** | ||
* Returns a new subscription that can subscribeTo paths in state. Note, | ||
* the subscription must be cancelled when no longer in use. | ||
*/ | ||
subscription: (callback: StateUpdateCallback) => DeepSubscription; | ||
} | ||
export interface Storage<State> extends DeepSubscriptions { | ||
export interface DeepStorage<State> extends DeepSubscriptions { | ||
/** | ||
* sets a value in deep storage by path and notifies subscribers. shortcut for | ||
* updateIn where the old value is ignored | ||
*/ | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => void; | ||
/** | ||
* Updates the whole state and notifies subscribers | ||
*/ | ||
update: (callback: (s: State) => State) => void; | ||
/** | ||
* Updates a value in deep storage by path and notifies subscribers. Must not | ||
* mutate the oldValue | ||
*/ | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
/** | ||
* Updates a property of the current state and notifies subscribers. | ||
*/ | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
/** | ||
* Returns the state that this deep storage is managing | ||
*/ | ||
state: State; | ||
stateIn: <DeepState>(...path: Path) => Storage<DeepState>; | ||
deep: <DeepState>(...path: Path) => Storage<DeepState>; | ||
/** | ||
* Returns state by a path | ||
*/ | ||
stateIn: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
/** | ||
* Creates a new DeepStorage at this point in the object path | ||
*/ | ||
deep: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
} | ||
export interface Subscription { | ||
/** | ||
* A cancelable way to subscribe to paths in state | ||
*/ | ||
export interface DeepSubscription { | ||
subscribeTo: (...path: Path) => void; | ||
@@ -20,3 +50,5 @@ cancel: () => void; | ||
export declare type Path = stringOrNumber[]; | ||
export declare class DeepStorage<State> implements Storage<State> { | ||
declare const _default: <State>(s: State) => DeepStorage<State>; | ||
export default _default; | ||
export declare class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
state: State; | ||
@@ -35,3 +67,3 @@ private id; | ||
stateIn: <DeepState>(...path: (string | number)[]) => any; | ||
deep: <DeepState>(...path: (string | number)[]) => Storage<DeepState>; | ||
deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState>; | ||
subscription: (callback: StateUpdateCallback) => { | ||
@@ -38,0 +70,0 @@ subscribeTo: (...path: (string | number)[]) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Is one array a prefix on another e.g. | ||
* | ||
* [] is a prefix of any array | ||
* ['asdf'] is a prefix of ['asdf', ...] | ||
* | ||
* etc. | ||
* | ||
* @param full the full array to check, must not be null | ||
* @param partial the partial array to check | ||
*/ | ||
function isPrefix(full, partial) { | ||
@@ -12,4 +23,5 @@ if (partial.length > full.length) | ||
} | ||
var DeepStorage = (function () { | ||
function DeepStorage(state) { | ||
exports.default = function (s) { return new DefaultDeepStorage(s); }; | ||
var DefaultDeepStorage = (function () { | ||
function DefaultDeepStorage(state) { | ||
var path = []; | ||
@@ -95,3 +107,3 @@ for (var _i = 1; _i < arguments.length; _i++) { | ||
} | ||
return new (DeepStorage.bind.apply(DeepStorage, [void 0, _this.stateIn.apply(_this, path)].concat(_this.path)))(); | ||
return new (DefaultDeepStorage.bind.apply(DefaultDeepStorage, [void 0, _this.stateIn.apply(_this, path)].concat(_this.path)))(); | ||
}; | ||
@@ -122,5 +134,5 @@ this.subscription = function (callback) { | ||
} | ||
return DeepStorage; | ||
return DefaultDeepStorage; | ||
}()); | ||
exports.DeepStorage = DeepStorage; | ||
exports.DefaultDeepStorage = DefaultDeepStorage; | ||
function numberOrString(value) { | ||
@@ -127,0 +139,0 @@ var parsed = parseInt(value); |
{ | ||
"name": "deep-storage", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -26,3 +26,5 @@ Deep Storage provides observable state for reactive applications in JavaScript. | ||
const storage = new DeepStorage({ | ||
import deepStorage from 'deep-storage'; | ||
const storage = deepStorage({ | ||
timer: 0 | ||
@@ -29,0 +31,0 @@ }); |
@@ -1,6 +0,47 @@ | ||
function sum(a: number, b: number) { | ||
return a + b; | ||
} | ||
test('adds 1 + 2 to equal 3', () => { | ||
expect(sum(1, 2)).toBe(3); | ||
}); | ||
import deepStorage from '../'; | ||
test('stateIn', () => { | ||
const storage = deepStorage({ | ||
todos: { abc: { id: 'abc', title: 'do something' } } | ||
}); | ||
expect(storage.stateIn('todos', 'abc', 'title')).toBe('do something'); | ||
}); | ||
test('deep', () => { | ||
const storage = deepStorage({ | ||
todos: { abc: { id: 'abc', title: 'do something' } } | ||
}); | ||
const deep = storage.deep('todos', 'abc'); | ||
expect(deep.stateIn('title')).toBe('do something'); | ||
}); | ||
test('updateIn', () => { | ||
const storage = deepStorage({ | ||
todos: { abc: { id: 'abc', title: 'do something' } } | ||
}); | ||
storage.updateIn('todos', 'abc')<{ title: string }>(abc => ({ ...abc, title: 'test' })); | ||
expect(storage.stateIn('todos', 'abc', 'title')).toBe('test'); | ||
}); | ||
test('setIn', () => { | ||
const storage = deepStorage({ | ||
todos: { abc: { id: 'abc', title: 'do something' } } | ||
}); | ||
storage.setIn('todos', 'abc', 'title')<string>('test'); | ||
expect(storage.stateIn('todos', 'abc', 'title')).toBe('test'); | ||
}); | ||
test('subscription', (done) => { | ||
const storage = deepStorage({ | ||
todos: { abc: { id: 'abc', title: 'do something' } } | ||
}); | ||
const subscription = storage.subscription((path, newState, oldState) => { | ||
expect(path).toEqual(['todos', 'abc', 'title']); | ||
expect(newState).toBe('test'); | ||
expect(oldState).toBe('do something'); | ||
done(); | ||
}); | ||
subscription.subscribeTo('todos'); | ||
storage.setIn('todos', 'abc', 'title')<string>('test'); | ||
subscription.cancel(); | ||
}); |
export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
export interface DeepSubscriptions { | ||
subscription: (callback: StateUpdateCallback) => Subscription; | ||
/** | ||
* Returns a new subscription that can subscribeTo paths in state. Note, | ||
* the subscription must be cancelled when no longer in use. | ||
*/ | ||
subscription: (callback: StateUpdateCallback) => DeepSubscription; | ||
} | ||
export interface Storage<State> extends DeepSubscriptions { | ||
export interface DeepStorage<State> extends DeepSubscriptions { | ||
/** | ||
* sets a value in deep storage by path and notifies subscribers. shortcut for | ||
* updateIn where the old value is ignored | ||
*/ | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => void; | ||
/** | ||
* Updates the whole state and notifies subscribers | ||
*/ | ||
update: (callback: (s: State) => State) => void; | ||
/** | ||
* Updates a value in deep storage by path and notifies subscribers. Must not | ||
* mutate the oldValue | ||
*/ | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
/** | ||
* Updates a property of the current state and notifies subscribers. | ||
*/ | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
/** | ||
* Returns the state that this deep storage is managing | ||
*/ | ||
state: State; | ||
stateIn: <DeepState>(...path: Path) => Storage<DeepState>; | ||
deep: <DeepState>(...path: Path) => Storage<DeepState>; | ||
/** | ||
* Returns state by a path | ||
*/ | ||
stateIn: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
/** | ||
* Creates a new DeepStorage at this point in the object path | ||
*/ | ||
deep: <DeepState>(...path: Path) => DeepStorage<DeepState>; | ||
} | ||
/** | ||
* Is one array a prefix on another e.g. | ||
* | ||
* [] is a prefix of any array | ||
* ['asdf'] is a prefix of ['asdf', ...] | ||
* | ||
* etc. | ||
* | ||
* @param full the full array to check, must not be null | ||
* @param partial the partial array to check | ||
*/ | ||
function isPrefix<T>(full: T[], partial: T[]) { | ||
@@ -25,3 +70,6 @@ if (partial.length > full.length) return false; | ||
export interface Subscription { | ||
/** | ||
* A cancelable way to subscribe to paths in state | ||
*/ | ||
export interface DeepSubscription { | ||
subscribeTo: (...path: Path) => void; | ||
@@ -34,4 +82,6 @@ cancel: () => void; | ||
export class DeepStorage<State> implements Storage<State> { | ||
export default <State>(s: State): DeepStorage<State> => new DefaultDeepStorage(s); | ||
export class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
private id: number = 0; | ||
@@ -94,4 +144,4 @@ public path: Path; | ||
} | ||
deep = <DeepState>(...path: Path): Storage<DeepState> => { | ||
return new DeepStorage<DeepState>(this.stateIn<DeepState>(...path), ...this.path); | ||
deep = <DeepState>(...path: Path): DeepStorage<DeepState> => { | ||
return new DefaultDeepStorage<DeepState>(this.stateIn<DeepState>(...path), ...this.path); | ||
} | ||
@@ -98,0 +148,0 @@ subscription = (callback: StateUpdateCallback) => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
150764
474
64
0