deep-storage
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -84,16 +84,22 @@ "use strict"; | ||
throw new AlreadyRunningError(); | ||
this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Running, request: request, response: undefined, error: undefined })); }); | ||
_a.label = 1; | ||
return [4 /*yield*/, this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Running, request: request, response: undefined, error: undefined })); })]; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 5, , 7]); | ||
return [4 /*yield*/, this.process(request)]; | ||
case 2: | ||
case 3: | ||
response_1 = _a.sent(); | ||
this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Succeeded, response: response_1, error: undefined })); }); | ||
return [4 /*yield*/, this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Succeeded, response: response_1, error: undefined })); })]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/, this.storage.state]; | ||
case 3: | ||
case 5: | ||
error_1 = _a.sent(); | ||
this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Failed, error: error_1, response: undefined })); }); | ||
return [4 /*yield*/, this.storage.update(function (state) { return (__assign({}, state, { status: AsyncStatus.Failed, error: error_1, response: undefined })); })]; | ||
case 6: | ||
_a.sent(); | ||
return [2 /*return*/, this.storage.state]; | ||
case 4: return [2 /*return*/]; | ||
case 7: return [2 /*return*/]; | ||
} | ||
@@ -100,0 +106,0 @@ }); |
@@ -14,7 +14,7 @@ export declare type StateUpdateCallback = <DeepState>(path: Path, newState: DeepState, oldState: DeepState) => void; | ||
*/ | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => void; | ||
setIn: (...path: Path) => <DeepState>(newValue: DeepState) => Promise<DeepState>; | ||
/** | ||
* Updates the whole state and notifies subscribers | ||
*/ | ||
update: (callback: (s: State) => State) => void; | ||
update: (callback: (s: State) => State) => Promise<State>; | ||
/** | ||
@@ -24,7 +24,7 @@ * Updates a value in deep storage by path and notifies subscribers. Must not | ||
*/ | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
updateIn: (...path: Path) => <DeepState>(callback: (s: DeepState) => DeepState) => Promise<DeepState>; | ||
/** | ||
* Updates a property of the current state and notifies subscribers. | ||
*/ | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => Promise<State[Key]>; | ||
/** | ||
@@ -52,4 +52,2 @@ * Returns the state that this deep storage is managing | ||
export declare type Path = stringOrNumber[]; | ||
declare const _default: <State>(s: State) => DeepStorage<State>; | ||
export default _default; | ||
export declare class DefaultDeepStorage<State> implements DeepStorage<State> { | ||
@@ -60,9 +58,9 @@ state: State; | ||
constructor(state: State); | ||
update: (callback: (s: State) => State) => void; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => void; | ||
setIn: (...path: (string | number)[]) => <DeepState>(newValue: DeepState) => void; | ||
update: (callback: (s: State) => State) => Promise<State>; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => Promise<State[Key]>; | ||
setIn: (...path: (string | number)[]) => <DeepState>(newValue: DeepState) => Promise<DeepState>; | ||
merge: (partial: { | ||
[P in keyof State]?: State[P]; | ||
}) => void; | ||
updateIn: (...path: (string | number)[]) => <DeepState>(callback: (s: DeepState) => DeepState) => void; | ||
updateIn: (...path: (string | number)[]) => <DeepState>(callback: (s: DeepState) => DeepState) => Promise<DeepState>; | ||
stateIn: <DeepState>(...path: (string | number)[]) => any; | ||
@@ -79,6 +77,6 @@ deep: <DeepState>(...path: (string | number)[]) => DeepStorage<DeepState>; | ||
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; | ||
setIn: (...path: (string | number)[]) => <DeepState>(newValue: DeepState) => Promise<DeepState>; | ||
update: (callback: (s: State) => State) => Promise<State>; | ||
updateIn: (...path: (string | number)[]) => <DeepState>(callback: (s: DeepState) => DeepState) => Promise<DeepState>; | ||
updateProperty: <Key extends keyof State>(key: Key, callback: (s: State[Key]) => State[Key]) => Promise<State[Key]>; | ||
readonly state: State; | ||
@@ -95,1 +93,3 @@ stateIn: <DeepState>(...path: (string | number)[]) => DeepState; | ||
}; | ||
export declare const deepStorage: <State>(s: State) => DeepStorage<State>; | ||
export default deepStorage; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -23,3 +58,2 @@ /** | ||
} | ||
exports.default = function (s) { return new DefaultDeepStorage(s); }; | ||
var DefaultDeepStorage = (function () { | ||
@@ -43,3 +77,3 @@ function DefaultDeepStorage(state) { | ||
return function (newValue) { | ||
_this.updateIn.apply(_this, path)(function () { return newValue; }); | ||
return _this.updateIn.apply(_this, path)(function () { return newValue; }); | ||
}; | ||
@@ -60,22 +94,26 @@ }; | ||
} | ||
return function (callback) { | ||
var oldState = _this.stateIn.apply(_this, path); | ||
var newState = callback(oldState); | ||
if (path.length === 0) { | ||
_this.state = newState; | ||
} | ||
else { | ||
// todo: this will no doubt cause some bugs... better to replace all the | ||
// parent objects too so that reference equality checks work in react | ||
_this.stateIn.apply(_this, path.slice(0, path.length - 1))[path[path.length - 1]] = newState; | ||
} | ||
var fullPath = path; | ||
for (var subscriberId in _this.subscriptions) { | ||
var 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); | ||
return function (callback) { return __awaiter(_this, void 0, void 0, function () { | ||
var oldState, newState, fullPath, subscriberId, subscriber; | ||
return __generator(this, function (_a) { | ||
oldState = this.stateIn.apply(this, path); | ||
newState = callback(oldState); | ||
if (path.length === 0) { | ||
this.state = newState; | ||
} | ||
} | ||
}; | ||
else { | ||
// todo: this will no doubt cause some bugs... better to replace all the | ||
// parent objects too so that reference equality checks work in react | ||
this.stateIn.apply(this, path.slice(0, path.length - 1))[path[path.length - 1]] = newState; | ||
} | ||
fullPath = 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); | ||
} | ||
} | ||
return [2 /*return*/, newState]; | ||
}); | ||
}); }; | ||
}; | ||
@@ -233,2 +271,4 @@ this.stateIn = function () { | ||
exports.parsePaths = parsePaths; | ||
exports.deepStorage = function (s) { return new DefaultDeepStorage(s); }; | ||
exports.default = exports.deepStorage; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "deep-storage", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Simple observable state management for reactive applications", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
173862
867