@proscom/prostore
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -1,4 +0,4 @@ | ||
export { IStore } from './IStore'; | ||
export { RequestStore } from './RequestStore'; | ||
export { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
export * from './IStore'; | ||
export * from './RequestStore'; | ||
export * from './IProstoreSsrContext'; | ||
export * from './storeUtils'; |
@@ -6,5 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var RequestStore_1 = require("./RequestStore"); | ||
exports.RequestStore = RequestStore_1.RequestStore; | ||
__export(require("./RequestStore")); | ||
__export(require("./storeUtils")); | ||
//# sourceMappingURL=prostore.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -10,5 +21,32 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
}; | ||
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 = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, 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 }); | ||
const rxjs_1 = require("rxjs"); | ||
const operators_1 = require("rxjs/operators"); | ||
var rxjs_1 = require("rxjs"); | ||
var operators_1 = require("rxjs/operators"); | ||
exports.initialRequestState = { | ||
@@ -21,18 +59,20 @@ data: null, | ||
}; | ||
class RequestStore { | ||
constructor({ initialData = null, skipQuery = (x) => undefined, ssrId = null, updateData = (data) => data }) { | ||
var RequestStore = /** @class */ (function () { | ||
function RequestStore(_a) { | ||
var _this = this; | ||
var _b = _a.initialData, initialData = _b === void 0 ? null : _b, _c = _a.skipQuery, skipQuery = _c === void 0 ? function (x) { return undefined; } : _c, _d = _a.ssrId, ssrId = _d === void 0 ? null : _d, _e = _a.updateData, updateData = _e === void 0 ? function (data) { return data; } : _e; | ||
this.variables$ = new rxjs_1.Subject(); | ||
this.loadedValueFromContext = false; | ||
this.mapRequestResult = (variables, options) => (data) => ({ | ||
data: this.updateData(data, this.state.data, { store: this, variables, options }), | ||
this.mapRequestResult = function (variables, options) { return function (data) { return ({ | ||
data: _this.updateData(data, _this.state.data, { store: _this, variables: variables, options: options }), | ||
loading: false, | ||
loaded: true, | ||
error: null, | ||
variables | ||
}); | ||
this.handleError = (error) => { | ||
variables: variables | ||
}); }; }; | ||
this.handleError = function (error) { | ||
console.error(error); | ||
return { error, loading: false }; | ||
return { error: error, loading: false }; | ||
}; | ||
this.initialState = Object.assign({}, exports.initialRequestState, { data: initialData }); | ||
this.initialState = __assign({}, exports.initialRequestState, { data: initialData }); | ||
this.state = this.initialState; | ||
@@ -44,16 +84,22 @@ this.ssrId = ssrId; | ||
} | ||
get state() { | ||
return this._state; | ||
} | ||
set state(newValue) { | ||
this._state = newValue; | ||
} | ||
createState() { | ||
return this.variables$.pipe(operators_1.switchMap(({ variables, options }) => { | ||
const state = Object.assign({ loading: true, variables, error: null }, options.changeState); | ||
return rxjs_1.merge(rxjs_1.of(state), rxjs_1.from(this.performSkippableRequest(variables, options)).pipe(operators_1.map(this.mapRequestResult(variables, options)), operators_1.catchError((error) => rxjs_1.of(this.handleError(error))))); | ||
}), operators_1.map((change) => (Object.assign({}, this.state, change))), operators_1.tap((state) => this.state = state), operators_1.share()); | ||
} | ||
performSkippableRequest(variables, options) { | ||
const skipped = this.skipQuery(variables); | ||
Object.defineProperty(RequestStore.prototype, "state", { | ||
get: function () { | ||
return this._state; | ||
}, | ||
set: function (newValue) { | ||
this._state = newValue; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
RequestStore.prototype.createState = function () { | ||
var _this = this; | ||
return this.variables$.pipe(operators_1.switchMap(function (_a) { | ||
var variables = _a.variables, options = _a.options; | ||
var state = __assign({ loading: true, variables: variables, error: null }, options.changeState); | ||
return rxjs_1.merge(rxjs_1.of(state), rxjs_1.from(_this.performSkippableRequest(variables, options)).pipe(operators_1.map(_this.mapRequestResult(variables, options)), operators_1.catchError(function (error) { return rxjs_1.of(_this.handleError(error)); }))); | ||
}), operators_1.map(function (change) { return (__assign({}, _this.state, change)); }), operators_1.tap(function (state) { return _this.state = state; }), operators_1.share()); | ||
}; | ||
RequestStore.prototype.performSkippableRequest = function (variables, options) { | ||
var skipped = this.skipQuery(variables); | ||
if (skipped !== undefined) { | ||
@@ -63,17 +109,25 @@ return rxjs_1.of(skipped); | ||
return this.performRequest(variables, options); | ||
} | ||
performRequest(variables, options) { | ||
}; | ||
RequestStore.prototype.performRequest = function (variables, options) { | ||
throw new Error('Please override performRequest function in your RequestStore extension'); | ||
} | ||
loadData(variables, options = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const promise = this.state$.pipe(operators_1.first((state) => !state.loading && state.loaded && state.variables === variables)).toPromise(); | ||
this.variables$.next({ variables, options }); | ||
return yield promise; | ||
}; | ||
RequestStore.prototype.loadData = function (variables, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var promise; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
promise = this.state$.pipe(operators_1.first(function (state) { return !state.loading && state.loaded && state.variables === variables; })).toPromise(); | ||
this.variables$.next({ variables: variables, options: options }); | ||
return [4 /*yield*/, promise]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
} | ||
getValueFromContext(context) { | ||
}; | ||
RequestStore.prototype.getValueFromContext = function (context) { | ||
if (this.ssrId && context && !this.loadedValueFromContext) { | ||
this.loadedValueFromContext = true; | ||
const state = context.getState(this.ssrId); | ||
var state = context.getState(this.ssrId); | ||
if (state) { | ||
@@ -84,13 +138,14 @@ this.state = state; | ||
return this.state; | ||
} | ||
setContextValue(context) { | ||
}; | ||
RequestStore.prototype.setContextValue = function (context) { | ||
if (this.ssrId && context) { | ||
context.setState(this.ssrId, this.state); | ||
} | ||
} | ||
reset(state) { | ||
this.state = Object.assign({}, exports.initialRequestState, state); | ||
} | ||
} | ||
}; | ||
RequestStore.prototype.reset = function (state) { | ||
this.state = __assign({}, exports.initialRequestState, state); | ||
}; | ||
return RequestStore; | ||
}()); | ||
exports.RequestStore = RequestStore; | ||
//# sourceMappingURL=RequestStore.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const lodash_1 = require("lodash"); | ||
var isEqual_1 = require("lodash/isEqual"); | ||
var isNil_1 = require("lodash/isNil"); | ||
function skipIf(condition, value) { | ||
return (vars) => { | ||
return function (vars) { | ||
if (condition(vars)) | ||
@@ -13,4 +14,4 @@ return value; | ||
function skipIfNull(value) { | ||
return (vars) => { | ||
if (lodash_1.isNil(vars)) | ||
return function (vars) { | ||
if (isNil_1.default(vars)) | ||
return value; | ||
@@ -27,3 +28,3 @@ return undefined; | ||
function checkRequestState(state, variables) { | ||
const result = { | ||
var result = { | ||
request: false, | ||
@@ -38,3 +39,3 @@ spinner: false, | ||
result.spinner = true; | ||
if (!lodash_1.isEqual(state.variables, variables)) { | ||
if (!isEqual_1.default(state.variables, variables)) { | ||
result.request = true; | ||
@@ -47,3 +48,3 @@ } | ||
} | ||
else if (lodash_1.isEqual(state.variables, variables)) { | ||
else if (isEqual_1.default(state.variables, variables)) { | ||
result.component = true; | ||
@@ -67,4 +68,4 @@ } | ||
function timeoutPromise(cb, timeout) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
cb(); | ||
@@ -71,0 +72,0 @@ resolve(); |
{ | ||
"name": "@proscom/prostore", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "> TODO: description", | ||
@@ -9,2 +9,3 @@ "author": "Andrew Starostin <a.starostin@proscom.ru>", | ||
"main": "lib/prostore.js", | ||
"module": "lib/es/prostore.js", | ||
"typings": "lib/prostore.d.ts", | ||
@@ -20,3 +21,3 @@ "directories": { | ||
"scripts": { | ||
"build": "tsc", | ||
"build": "tsc && tsc -p tsconfig.es.json", | ||
"watch": "tsc --watch --preserveWatchOutput", | ||
@@ -29,3 +30,4 @@ "watch-push": "nodemon --config yalc-push.nodemon.json", | ||
}, | ||
"gitHead": "c3ef0173786c47df1b29ba06574f958ad91cf416" | ||
"sideEffects": false, | ||
"gitHead": "7b81abe2f49a11d3bfa193006bf14b8f9b6a96a5" | ||
} |
@@ -1,4 +0,4 @@ | ||
export { IStore } from './IStore'; | ||
export { RequestStore } from './RequestStore'; | ||
export { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
export * from './IStore'; | ||
export * from './RequestStore'; | ||
export * from './IProstoreSsrContext'; | ||
export * from './storeUtils'; |
@@ -1,2 +0,3 @@ | ||
import { isEqual, isNil } from 'lodash'; | ||
import isEqual from 'lodash/isEqual'; | ||
import isNil from 'lodash/isNil'; | ||
import {IRequestState, ISkipQueryFn} from './RequestStore'; | ||
@@ -3,0 +4,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
45386
37
831