@proscom/prostore
Advanced tools
Comparing version 0.2.10 to 0.2.11
import { Subject } from 'rxjs'; | ||
import { IRequestState, IRequestStoreOptions, IRxRequestStoreParams, IVariablesSubjectType, RxRequestStore } from './RxRequestStore'; | ||
export interface IRequestStoreParams<Vars, Data, Options = any> extends Omit<IRxRequestStoreParams<Vars, Data, Options>, 'variables$'> { | ||
import { IRequestState, IRequestStoreOptions, IRxRequestStoreParams, IVariablesObservable, IVariablesSubjectType, RxRequestStore } from "./RxRequestStore"; | ||
import { CustomError } from "@proscom/ui-utils"; | ||
export interface IRequestStoreParams<Vars, Data, Options = any> extends IRxRequestStoreParams<Vars, Data, Options> { | ||
} | ||
export declare class RequestStoreReactivityError extends CustomError { | ||
} | ||
export declare class RequestStore<Vars = any, Data = any, Options extends IRequestStoreOptions<Vars, Data> = any> extends RxRequestStore<Vars, Data, Options> { | ||
variables$: Subject<IVariablesSubjectType<Vars, Options>>; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext }?: IRequestStoreParams<Vars, Data>); | ||
isReactive: boolean; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext, variables$ }?: IRequestStoreParams<Vars, Data>); | ||
setVariables$(variables$: IVariablesObservable<Vars, Options> | null): void; | ||
loadData(variables: Vars, options?: Options): Promise<IRequestState<Vars, Data>>; | ||
@@ -9,0 +14,0 @@ /** |
import { __assign, __awaiter, __extends, __generator } from "tslib"; | ||
import { Subject } from 'rxjs'; | ||
import { first } from 'rxjs/operators'; | ||
import { initialRequestState, RxRequestStore } from './RxRequestStore'; | ||
import { initialRequestState, RxRequestStore } from "./RxRequestStore"; | ||
import { CustomError } from "@proscom/ui-utils"; | ||
var RequestStoreReactivityError = /** @class */ (function (_super) { | ||
__extends(RequestStoreReactivityError, _super); | ||
function RequestStoreReactivityError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return RequestStoreReactivityError; | ||
}(CustomError)); | ||
export { RequestStoreReactivityError }; | ||
var RequestStore = /** @class */ (function (_super) { | ||
__extends(RequestStore, _super); | ||
function RequestStore(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.initialData, initialData = _c === void 0 ? null : _c, _d = _b.skipQuery, skipQuery = _d === void 0 ? function (x) { return undefined; } : _d, _e = _b.ssrId, ssrId = _e === void 0 ? null : _e, _f = _b.updateData, updateData = _f === void 0 ? function (data) { return data; } : _f, _g = _b.ssrContext, ssrContext = _g === void 0 ? null : _g; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.initialData, initialData = _c === void 0 ? null : _c, _d = _b.skipQuery, skipQuery = _d === void 0 ? function (x) { return undefined; } : _d, _e = _b.ssrId, ssrId = _e === void 0 ? null : _e, _f = _b.updateData, updateData = _f === void 0 ? function (data) { return data; } : _f, _g = _b.ssrContext, ssrContext = _g === void 0 ? null : _g, variables$ = _b.variables$; | ||
var _this = _super.call(this, { | ||
@@ -14,8 +23,23 @@ initialData: initialData, | ||
updateData: updateData, | ||
ssrContext: ssrContext | ||
ssrContext: ssrContext, | ||
variables$: variables$ | ||
}) || this; | ||
_this.variables$ = new Subject(); | ||
_this.variables$$.next(_this.variables$); | ||
_this.isReactive = false; | ||
_this.isReactive = !!variables$; | ||
if (!variables$) { | ||
_this.variables$$.next(_this.variables$); | ||
} | ||
return _this; | ||
} | ||
RequestStore.prototype.setVariables$ = function (variables$) { | ||
if (variables$) { | ||
_super.prototype.setVariables$.call(this, variables$); | ||
this.isReactive = true; | ||
} | ||
else { | ||
_super.prototype.setVariables$.call(this, this.variables$); | ||
this.isReactive = false; | ||
} | ||
}; | ||
RequestStore.prototype.loadData = function (variables, options) { | ||
@@ -27,2 +51,5 @@ return __awaiter(this, void 0, void 0, function () { | ||
case 0: | ||
if (this.isReactive) { | ||
throw new RequestStoreReactivityError('RequestStoreReactivityError: Cannot use loadData when "variables$" are supplied as an Observable'); | ||
} | ||
promise = this.state$ | ||
@@ -35,3 +62,3 @@ .pipe(first(function (state) { | ||
.toPromise(); | ||
this.variables$.next({ variables: variables, options: options || {} }); | ||
this.variables$.next({ variables: variables, options: options }); | ||
this.registerSsrPromise(promise); | ||
@@ -38,0 +65,0 @@ return [4 /*yield*/, promise]; |
@@ -26,3 +26,3 @@ import { BehaviorSubject, Observable } from 'rxjs'; | ||
variables: Vars; | ||
options: Options; | ||
options?: Options; | ||
} | ||
@@ -77,3 +77,4 @@ export interface IRxRequestStoreParams<Vars, Data, Options> { | ||
protected loadedValueFromContext: boolean; | ||
sub: SubscriptionManager; | ||
/** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ | ||
_sub: SubscriptionManager; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext, variables$ }: IRxRequestStoreParams<Vars, Data, Options>); | ||
@@ -86,3 +87,3 @@ get state(): IRequestState<Vars, Data>; | ||
projectVariables: (variables$: IVariablesObservable<Vars, Options>) => Observable<IRequestState<Vars, Data>>; | ||
projectRequest: ({ variables, options }: IVariablesSubjectType<Vars, Options>) => Observable<Partial<IRequestState<Vars, Data>>>; | ||
projectRequest: ({ variables, options: optionsProp }: IVariablesSubjectType<Vars, Options>) => Observable<Partial<IRequestState<Vars, Data>>>; | ||
performSkippableRequest(variables: Vars, options: Options): IObservableData<Data | null>; | ||
@@ -89,0 +90,0 @@ performRequest(variables: Vars, options: Options): IObservableData<Data | null>; |
@@ -20,3 +20,4 @@ import { __assign, __extends } from "tslib"; | ||
_this.loadedValueFromContext = false; | ||
_this.sub = new SubscriptionManager(); | ||
/** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ | ||
_this._sub = new SubscriptionManager(); | ||
_this.projectVariables = function (variables$) { | ||
@@ -31,3 +32,4 @@ if (!variables$) { | ||
_this.projectRequest = function (_a) { | ||
var variables = _a.variables, options = _a.options; | ||
var variables = _a.variables, optionsProp = _a.options; | ||
var options = optionsProp || {}; | ||
var state = __assign({ loading: true, variables: variables, error: null }, options.changeState); | ||
@@ -73,6 +75,6 @@ return merge(of(state), from(_this.performSkippableRequest(variables, options)).pipe(map(_this.mapRequestResult(variables, options)), catchError(function (error) { return of(_this.handleError(error)); }))); | ||
RxRequestStore.prototype.onSubscribe = function () { | ||
this.sub.subscribe(this.variables$$.pipe(switchMap(this.projectVariables)), this.state$); | ||
this._sub.subscribe(this.variables$$.pipe(switchMap(this.projectVariables)), this.state$); | ||
}; | ||
RxRequestStore.prototype.onUnsubscribe = function () { | ||
this.sub.destroy(); | ||
this._sub.destroy(); | ||
}; | ||
@@ -79,0 +81,0 @@ RxRequestStore.prototype.performSkippableRequest = function (variables, options) { |
import { Subject } from 'rxjs'; | ||
import { IRequestState, IRequestStoreOptions, IRxRequestStoreParams, IVariablesSubjectType, RxRequestStore } from './RxRequestStore'; | ||
export interface IRequestStoreParams<Vars, Data, Options = any> extends Omit<IRxRequestStoreParams<Vars, Data, Options>, 'variables$'> { | ||
import { IRequestState, IRequestStoreOptions, IRxRequestStoreParams, IVariablesObservable, IVariablesSubjectType, RxRequestStore } from "./RxRequestStore"; | ||
import { CustomError } from "@proscom/ui-utils"; | ||
export interface IRequestStoreParams<Vars, Data, Options = any> extends IRxRequestStoreParams<Vars, Data, Options> { | ||
} | ||
export declare class RequestStoreReactivityError extends CustomError { | ||
} | ||
export declare class RequestStore<Vars = any, Data = any, Options extends IRequestStoreOptions<Vars, Data> = any> extends RxRequestStore<Vars, Data, Options> { | ||
variables$: Subject<IVariablesSubjectType<Vars, Options>>; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext }?: IRequestStoreParams<Vars, Data>); | ||
isReactive: boolean; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext, variables$ }?: IRequestStoreParams<Vars, Data>); | ||
setVariables$(variables$: IVariablesObservable<Vars, Options> | null): void; | ||
loadData(variables: Vars, options?: Options): Promise<IRequestState<Vars, Data>>; | ||
@@ -9,0 +14,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RequestStore = void 0; | ||
exports.RequestStore = exports.RequestStoreReactivityError = void 0; | ||
var tslib_1 = require("tslib"); | ||
@@ -8,6 +8,15 @@ var rxjs_1 = require("rxjs"); | ||
var RxRequestStore_1 = require("./RxRequestStore"); | ||
var ui_utils_1 = require("@proscom/ui-utils"); | ||
var RequestStoreReactivityError = /** @class */ (function (_super) { | ||
tslib_1.__extends(RequestStoreReactivityError, _super); | ||
function RequestStoreReactivityError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return RequestStoreReactivityError; | ||
}(ui_utils_1.CustomError)); | ||
exports.RequestStoreReactivityError = RequestStoreReactivityError; | ||
var RequestStore = /** @class */ (function (_super) { | ||
tslib_1.__extends(RequestStore, _super); | ||
function RequestStore(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.initialData, initialData = _c === void 0 ? null : _c, _d = _b.skipQuery, skipQuery = _d === void 0 ? function (x) { return undefined; } : _d, _e = _b.ssrId, ssrId = _e === void 0 ? null : _e, _f = _b.updateData, updateData = _f === void 0 ? function (data) { return data; } : _f, _g = _b.ssrContext, ssrContext = _g === void 0 ? null : _g; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.initialData, initialData = _c === void 0 ? null : _c, _d = _b.skipQuery, skipQuery = _d === void 0 ? function (x) { return undefined; } : _d, _e = _b.ssrId, ssrId = _e === void 0 ? null : _e, _f = _b.updateData, updateData = _f === void 0 ? function (data) { return data; } : _f, _g = _b.ssrContext, ssrContext = _g === void 0 ? null : _g, variables$ = _b.variables$; | ||
var _this = _super.call(this, { | ||
@@ -18,8 +27,23 @@ initialData: initialData, | ||
updateData: updateData, | ||
ssrContext: ssrContext | ||
ssrContext: ssrContext, | ||
variables$: variables$ | ||
}) || this; | ||
_this.variables$ = new rxjs_1.Subject(); | ||
_this.variables$$.next(_this.variables$); | ||
_this.isReactive = false; | ||
_this.isReactive = !!variables$; | ||
if (!variables$) { | ||
_this.variables$$.next(_this.variables$); | ||
} | ||
return _this; | ||
} | ||
RequestStore.prototype.setVariables$ = function (variables$) { | ||
if (variables$) { | ||
_super.prototype.setVariables$.call(this, variables$); | ||
this.isReactive = true; | ||
} | ||
else { | ||
_super.prototype.setVariables$.call(this, this.variables$); | ||
this.isReactive = false; | ||
} | ||
}; | ||
RequestStore.prototype.loadData = function (variables, options) { | ||
@@ -31,2 +55,5 @@ return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
case 0: | ||
if (this.isReactive) { | ||
throw new RequestStoreReactivityError('RequestStoreReactivityError: Cannot use loadData when "variables$" are supplied as an Observable'); | ||
} | ||
promise = this.state$ | ||
@@ -39,3 +66,3 @@ .pipe(operators_1.first(function (state) { | ||
.toPromise(); | ||
this.variables$.next({ variables: variables, options: options || {} }); | ||
this.variables$.next({ variables: variables, options: options }); | ||
this.registerSsrPromise(promise); | ||
@@ -42,0 +69,0 @@ return [4 /*yield*/, promise]; |
@@ -26,3 +26,3 @@ import { BehaviorSubject, Observable } from 'rxjs'; | ||
variables: Vars; | ||
options: Options; | ||
options?: Options; | ||
} | ||
@@ -77,3 +77,4 @@ export interface IRxRequestStoreParams<Vars, Data, Options> { | ||
protected loadedValueFromContext: boolean; | ||
sub: SubscriptionManager; | ||
/** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ | ||
_sub: SubscriptionManager; | ||
constructor({ initialData, skipQuery, ssrId, updateData, ssrContext, variables$ }: IRxRequestStoreParams<Vars, Data, Options>); | ||
@@ -86,3 +87,3 @@ get state(): IRequestState<Vars, Data>; | ||
projectVariables: (variables$: IVariablesObservable<Vars, Options>) => Observable<IRequestState<Vars, Data>>; | ||
projectRequest: ({ variables, options }: IVariablesSubjectType<Vars, Options>) => Observable<Partial<IRequestState<Vars, Data>>>; | ||
projectRequest: ({ variables, options: optionsProp }: IVariablesSubjectType<Vars, Options>) => Observable<Partial<IRequestState<Vars, Data>>>; | ||
performSkippableRequest(variables: Vars, options: Options): IObservableData<Data | null>; | ||
@@ -89,0 +90,0 @@ performRequest(variables: Vars, options: Options): IObservableData<Data | null>; |
@@ -23,3 +23,4 @@ "use strict"; | ||
_this.loadedValueFromContext = false; | ||
_this.sub = new SubscriptionManager_1.SubscriptionManager(); | ||
/** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ | ||
_this._sub = new SubscriptionManager_1.SubscriptionManager(); | ||
_this.projectVariables = function (variables$) { | ||
@@ -34,3 +35,4 @@ if (!variables$) { | ||
_this.projectRequest = function (_a) { | ||
var variables = _a.variables, options = _a.options; | ||
var variables = _a.variables, optionsProp = _a.options; | ||
var options = optionsProp || {}; | ||
var state = tslib_1.__assign({ loading: true, variables: variables, error: null }, options.changeState); | ||
@@ -76,6 +78,6 @@ 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)); }))); | ||
RxRequestStore.prototype.onSubscribe = function () { | ||
this.sub.subscribe(this.variables$$.pipe(operators_1.switchMap(this.projectVariables)), this.state$); | ||
this._sub.subscribe(this.variables$$.pipe(operators_1.switchMap(this.projectVariables)), this.state$); | ||
}; | ||
RxRequestStore.prototype.onUnsubscribe = function () { | ||
this.sub.destroy(); | ||
this._sub.destroy(); | ||
}; | ||
@@ -82,0 +84,0 @@ RxRequestStore.prototype.performSkippableRequest = function (variables, options) { |
{ | ||
"name": "@proscom/prostore", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"description": "State management library with multiple stores based on rxjs", | ||
@@ -31,3 +31,3 @@ "author": "Andrew Starostin <a.starostin@proscom.ru>", | ||
"dependencies": { | ||
"@proscom/ui-utils": "<0.2", | ||
"@proscom/ui-utils": "^0.1.15", | ||
"lodash": "^4.17.14", | ||
@@ -43,3 +43,3 @@ "tslib": "^2.0.3" | ||
}, | ||
"gitHead": "e00165a18de9b35cd6c8f3528d34cc779943904a" | ||
"gitHead": "8211af07428ce9663c5f909109e01aa4be9c7b5f" | ||
} |
@@ -7,10 +7,14 @@ import { Subject } from 'rxjs'; | ||
IRequestStoreOptions, | ||
IRxRequestStoreParams, | ||
IRxRequestStoreParams, IVariablesObservable, | ||
IVariablesSubjectType, | ||
RxRequestStore | ||
} from './RxRequestStore'; | ||
} from "./RxRequestStore"; | ||
import { CustomError } from "@proscom/ui-utils"; | ||
export interface IRequestStoreParams<Vars, Data, Options = any> | ||
extends Omit<IRxRequestStoreParams<Vars, Data, Options>, 'variables$'> {} | ||
extends IRxRequestStoreParams<Vars, Data, Options> {} | ||
export class RequestStoreReactivityError extends CustomError { | ||
} | ||
export class RequestStore< | ||
@@ -22,2 +26,3 @@ Vars = any, | ||
variables$ = new Subject<IVariablesSubjectType<Vars, Options>>(); | ||
isReactive: boolean = false; | ||
@@ -29,3 +34,4 @@ constructor({ | ||
updateData = (data) => data, | ||
ssrContext = null | ||
ssrContext = null, | ||
variables$ | ||
}: IRequestStoreParams<Vars, Data> = {}) { | ||
@@ -37,7 +43,21 @@ super({ | ||
updateData, | ||
ssrContext | ||
ssrContext, | ||
variables$ | ||
}); | ||
this.variables$$.next(this.variables$); | ||
this.isReactive = !!variables$; | ||
if (!variables$) { | ||
this.variables$$.next(this.variables$); | ||
} | ||
} | ||
setVariables$(variables$: IVariablesObservable<Vars, Options> | null) { | ||
if (variables$) { | ||
super.setVariables$(variables$); | ||
this.isReactive = true; | ||
} else { | ||
super.setVariables$(this.variables$); | ||
this.isReactive = false; | ||
} | ||
} | ||
async loadData( | ||
@@ -47,2 +67,6 @@ variables: Vars, | ||
): Promise<IRequestState<Vars, Data>> { | ||
if (this.isReactive) { | ||
throw new RequestStoreReactivityError('RequestStoreReactivityError: Cannot use loadData when "variables$" are supplied as an Observable'); | ||
} | ||
const promise = this.state$ | ||
@@ -58,3 +82,3 @@ .pipe( | ||
.toPromise(); | ||
this.variables$.next({ variables, options: options || ({} as Options) }); | ||
this.variables$.next({ variables, options: options }); | ||
this.registerSsrPromise(promise); | ||
@@ -61,0 +85,0 @@ return await promise; |
@@ -38,3 +38,3 @@ import { BehaviorSubject, from, merge, NEVER, Observable, of } from 'rxjs'; | ||
variables: Vars; | ||
options: Options; | ||
options?: Options; | ||
} | ||
@@ -108,3 +108,4 @@ | ||
sub = new SubscriptionManager(); | ||
/** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ | ||
_sub = new SubscriptionManager(); | ||
@@ -148,3 +149,3 @@ constructor({ | ||
onSubscribe() { | ||
this.sub.subscribe( | ||
this._sub.subscribe( | ||
this.variables$$.pipe(switchMap(this.projectVariables)), | ||
@@ -156,3 +157,3 @@ this.state$ | ||
onUnsubscribe() { | ||
this.sub.destroy(); | ||
this._sub.destroy(); | ||
} | ||
@@ -177,6 +178,8 @@ | ||
variables, | ||
options | ||
options: optionsProp | ||
}: IVariablesSubjectType<Vars, Options>): Observable< | ||
Partial<IRequestState<Vars, Data>> | ||
> => { | ||
const options = optionsProp || ({} as Options); | ||
const state = { | ||
@@ -183,0 +186,0 @@ loading: true, |
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
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
168039
2819
Updated@proscom/ui-utils@^0.1.15