deep-storage
Advanced tools
Comparing version 0.4.0 to 1.0.0
@@ -172,5 +172,6 @@ "use strict"; | ||
exports.DefaultDeepAsync = DefaultDeepAsync; | ||
exports.default = function (storage, process) { | ||
exports.deepAsync = function (storage, process) { | ||
return new DefaultDeepAsync(storage, process); | ||
}; | ||
exports.default = exports.deepAsync; | ||
//# sourceMappingURL=async.js.map |
@@ -46,10 +46,8 @@ "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) { | ||
for (var i = 0; i < Math.min(subscriptionPath.length, stateChangePath.length); i++) { | ||
if (stateChangePath[i] !== subscriptionPath[i]) | ||
return false; | ||
@@ -59,2 +57,3 @@ } | ||
} | ||
exports.isPathMatch = isPathMatch; | ||
var DefaultDeepStorage = (function () { | ||
@@ -95,3 +94,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 +107,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); | ||
} | ||
@@ -171,2 +170,14 @@ } | ||
} | ||
Object.defineProperty(DefaultDeepStorage.prototype, "props", { | ||
get: function () { | ||
var result = {}; | ||
var state = this.state; | ||
for (var key in state) { | ||
result[key] = this.deep(key); | ||
} | ||
return result; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return DefaultDeepStorage; | ||
@@ -249,2 +260,14 @@ }()); | ||
}); | ||
Object.defineProperty(NestedDeepStorage.prototype, "props", { | ||
get: function () { | ||
var result = {}; | ||
var state = this.state; | ||
for (var key in state) { | ||
result[key] = this.deep(key); | ||
} | ||
return result; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return NestedDeepStorage; | ||
@@ -251,0 +274,0 @@ }()); |
{ | ||
"name": "deep-storage", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -21,3 +21,4 @@ "main": "./lib/index.js", | ||
"test": "jest", | ||
"build": "rimraf lib && tsc -p tsconfig.prod.json" | ||
"build": "rimraf lib *.d.ts && tsc -p tsconfig.prod.json", | ||
"publish-patch": "yarn build && git commit -am 'build' && git push && npm version patch && git push && npm publish" | ||
}, | ||
@@ -24,0 +25,0 @@ "dependencies": {}, |
@@ -49,3 +49,3 @@ Deep Storage provides observable state for reactive applications in JavaScript. | ||
const DeepTimerView = connect({timer: storage.deep('timer')})(TimerView); | ||
const DeepTimerView = deep({timer: storage.deep('timer')})(TimerView); | ||
@@ -52,0 +52,0 @@ ReactDOM.render(( |
@@ -1,2 +0,2 @@ | ||
import deepStorage from '../'; | ||
import deepStorage, { isPathMatch } from '../'; | ||
@@ -48,1 +48,33 @@ 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'])).toBeTruthy(); | ||
expect(isPathMatch(['notTest'], ['test'])).toBeFalsy(); | ||
expect(isPathMatch(['app', 'trello'], ['app', 'trello', 'connections'])).toBeTruthy(); | ||
}); | ||
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(); | ||
}); |
@@ -70,3 +70,3 @@ import { DeepStorage, UsesDeepStorage } from "./index"; | ||
export default <Request, Response>( | ||
export const deepAsync = <Request, Response>( | ||
storage: DeepStorage<DeepAsyncData<Request, Response>>, | ||
@@ -77,1 +77,3 @@ process: (request: Request) => Promise<Response> | ||
} | ||
export default deepAsync; |
@@ -58,3 +58,9 @@ export type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
*/ | ||
path: Path | ||
path: Path; | ||
/** | ||
* Returns an object with keys from State and values of | ||
* DeepStorage for that key | ||
*/ | ||
props: {[P in keyof State]: DeepStorage<State[P]>} | ||
} | ||
@@ -70,9 +76,8 @@ | ||
* | ||
* @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[]) { | ||
for (let i = 0; i < Math.min(subscriptionPath.length, stateChangePath.length); i++) { | ||
if (stateChangePath[i] !== subscriptionPath[i]) return false; | ||
} | ||
@@ -132,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) | ||
} | ||
@@ -179,2 +184,10 @@ } | ||
path: Path = []; | ||
get props() { | ||
const result = {} as {[P in keyof State]: DeepStorage<State[P]>}; | ||
const state = this.state; | ||
for (let key in state) { | ||
result[key] = this.deep(key); | ||
} | ||
return result; | ||
} | ||
} | ||
@@ -223,2 +236,10 @@ | ||
root = () => this.rootStorage; | ||
get props() { | ||
const result = {} as {[P in keyof State]: DeepStorage<State[P]>}; | ||
const state = this.state; | ||
for (let key in state) { | ||
result[key] = this.deep(key); | ||
} | ||
return result; | ||
} | ||
} | ||
@@ -225,0 +246,0 @@ |
@@ -10,2 +10,3 @@ { | ||
"declaration": true, | ||
"declarationDir": ".", | ||
"lib": [ | ||
@@ -12,0 +13,0 @@ "es6", |
{ | ||
"extends": "./tsconfig", | ||
"exclude": [ | ||
"./src/**/__test__" | ||
"./src/**/__tests__/*" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
185271
1148
1