deep-storage
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -57,2 +57,14 @@ export declare type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
/** | ||
* Is one array a prefix on another e.g. | ||
* | ||
* [] is a prefix of any array | ||
* ['asdf'] is a prefix of ['asdf', ...] | ||
* | ||
* etc. | ||
* | ||
* @param stateChangePath the full array to check, must not be null | ||
* @param subscriptionPath the partial array to check | ||
*/ | ||
export declare function isPathMatch<T>(stateChangePath: T[], subscriptionPath: T[]): boolean; | ||
/** | ||
* A cancelable way to subscribe to paths in state | ||
@@ -59,0 +71,0 @@ */ |
@@ -46,10 +46,10 @@ "use strict"; | ||
* | ||
* @param full the full array to check, must not be null | ||
* @param partial the partial array to check | ||
* @param stateChangePath the full array to check, must not be null | ||
* @param subscriptionPath the partial array to check | ||
*/ | ||
function isPrefix(full, partial) { | ||
if (partial.length > full.length) | ||
return false; | ||
for (var i = 0; i < partial.length; i++) { | ||
if (partial[i] !== full[i]) | ||
function isPathMatch(stateChangePath, subscriptionPath) { | ||
if (stateChangePath.length === 0) | ||
return true; | ||
for (var i = 0; i < subscriptionPath.length; i++) { | ||
if (stateChangePath[i] !== subscriptionPath[i]) | ||
return false; | ||
@@ -59,2 +59,3 @@ } | ||
} | ||
exports.isPathMatch = isPathMatch; | ||
var DefaultDeepStorage = (function () { | ||
@@ -95,3 +96,3 @@ function DefaultDeepStorage(state) { | ||
return function (callback) { return __awaiter(_this, void 0, void 0, function () { | ||
var oldState, newState, fullPath, subscriberId, subscriber; | ||
var oldState, newState, stateChangePath, subscriberId, subscriber; | ||
return __generator(this, function (_a) { | ||
@@ -108,8 +109,8 @@ oldState = this.stateIn.apply(this, path); | ||
} | ||
fullPath = path; | ||
stateChangePath = path; | ||
for (subscriberId in this.subscriptions) { | ||
subscriber = this.subscriptions[subscriberId]; | ||
// check to see if we have any matches | ||
if (subscriber.paths.some(function (subscriberPath) { return isPrefix(fullPath, subscriberPath); })) { | ||
subscriber.callback(fullPath, newState, oldState); | ||
if (subscriber.paths.some(function (subscriberPath) { return isPathMatch(stateChangePath, subscriberPath); })) { | ||
subscriber.callback(stateChangePath, newState, oldState); | ||
} | ||
@@ -116,0 +117,0 @@ } |
{ | ||
"name": "deep-storage", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -1,2 +0,2 @@ | ||
import deepStorage from '../'; | ||
import deepStorage, { isPathMatch } from '../'; | ||
@@ -48,1 +48,30 @@ test('stateIn', () => { | ||
}); | ||
test('isPathMatch', () => { | ||
// state change path of [] and subscription path of ['test'] | ||
expect(isPathMatch([], ['test'])).toBeTruthy(); | ||
expect(isPathMatch(['test'], ['test'])).toBeTruthy(); | ||
expect(isPathMatch(['test', 'something'], ['test'])).toBeTruthy(); | ||
expect(isPathMatch(['test'], ['test', 'something'])).toBeFalsy(); | ||
expect(isPathMatch(['notTest'], ['test'])).toBeFalsy(); | ||
}); | ||
test('subscription and update', (done) => { | ||
const storage = deepStorage({ | ||
todos: [] | ||
}); | ||
const subscription = storage.subscription((path, newState, oldState) => { | ||
expect(path).toEqual([]); | ||
expect(newState).toEqual({todos: [1]}); | ||
expect(oldState).toEqual({todos: []}); | ||
done(); | ||
}); | ||
subscription.subscribeTo('todos'); | ||
storage.update(prevState => ({ ...prevState, todos: [1] })); | ||
subscription.cancel(); | ||
}); |
@@ -75,9 +75,9 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
* | ||
* @param full the full array to check, must not be null | ||
* @param partial the partial array to check | ||
* @param stateChangePath the full array to check, must not be null | ||
* @param subscriptionPath the partial array to check | ||
*/ | ||
function isPrefix<T>(full: T[], partial: T[]) { | ||
if (partial.length > full.length) return false; | ||
for (let i = 0; i < partial.length; i++) { | ||
if (partial[i] !== full[i]) return false; | ||
export function isPathMatch<T>(stateChangePath: T[], subscriptionPath: T[]) { | ||
if(stateChangePath.length === 0) return true; | ||
for (let i = 0; i < subscriptionPath.length; i++) { | ||
if (stateChangePath[i] !== subscriptionPath[i]) return false; | ||
} | ||
@@ -137,8 +137,8 @@ return true; | ||
} | ||
const fullPath = path; | ||
const stateChangePath = path; | ||
for (let subscriberId in this.subscriptions) { | ||
const subscriber = this.subscriptions[subscriberId]; | ||
// check to see if we have any matches | ||
if (subscriber.paths.some(subscriberPath => isPrefix(fullPath, subscriberPath))) { | ||
subscriber.callback(fullPath, newState, oldState) | ||
if (subscriber.paths.some(subscriberPath => isPathMatch(stateChangePath, subscriberPath))) { | ||
subscriber.callback(stateChangePath, newState, oldState) | ||
} | ||
@@ -145,0 +145,0 @@ } |
Sorry, the diff of this file is not supported yet
185125
1149