@proscom/prostore
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -18,3 +18,3 @@ import { Observable } from 'rxjs'; | ||
* @example | ||
* // original$ - какой-то ObservableWithValue | ||
* // original$ - some other ObservableWithValue | ||
* const obs$ = attachValueToObservable( | ||
@@ -21,0 +21,0 @@ * original$.pipe(map(transform)), |
@@ -8,3 +8,3 @@ /** | ||
* @example | ||
* // original$ - какой-то ObservableWithValue | ||
* // original$ - some other ObservableWithValue | ||
* const obs$ = attachValueToObservable( | ||
@@ -11,0 +11,0 @@ * original$.pipe(map(transform)), |
import { Observable, Subject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
import { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
import { ObservableWithValue } from './ObservableWithValue'; | ||
export interface IRequestState<Vars, Data> { | ||
@@ -63,3 +64,3 @@ /** Request result */ | ||
/** Observable of the request state */ | ||
state$: Observable<IRequestState<Vars, Data>>; | ||
state$: ObservableWithValue<IRequestState<Vars, Data>>; | ||
/** Default request state. May be used to reset the request */ | ||
@@ -80,3 +81,3 @@ initialState: IRequestState<Vars, Data>; | ||
set state(newValue: IRequestState<Vars, Data>); | ||
createState(): Observable<IRequestState<Vars, Data>>; | ||
createState(): ObservableWithValue<IRequestState<Vars, Data>>; | ||
performSkippableRequest(variables: Vars, options: Options): IObservableData<Data | null>; | ||
@@ -83,0 +84,0 @@ performRequest(variables: Vars, options: Options): IObservableData<Data | null>; |
import { __assign, __awaiter, __generator } from "tslib"; | ||
import { from, merge, of, Subject } from 'rxjs'; | ||
import { catchError, map, first, switchMap, tap, share } from 'rxjs/operators'; | ||
import { catchError, first, map, shareReplay, switchMap, tap } from 'rxjs/operators'; | ||
import { attachValueToObservable } from './ObservableWithValue'; | ||
export var initialRequestState = { | ||
@@ -55,10 +56,12 @@ data: null, | ||
var _this = this; | ||
return this.variables$.pipe(switchMap(function (_a) { | ||
return attachValueToObservable(this.variables$.pipe(switchMap(function (_a) { | ||
var variables = _a.variables, options = _a.options; | ||
var state = __assign({ loading: true, variables: variables, error: null }, options.changeState); | ||
return merge(of(state), from(_this.performSkippableRequest(variables, options)).pipe(map(_this.mapRequestResult(variables, options)), catchError(function (error) { return of(_this.handleError(error)); }))); | ||
}), map(function (change) { return (__assign(__assign({}, _this.state), change)); }), tap(function (state) { | ||
}), map(function (change) { | ||
return (__assign(__assign({}, _this.state), change)); | ||
}), tap(function (state) { | ||
_this.state = state; | ||
_this.updateContextState(state); | ||
}), share()); | ||
}), shareReplay()), function () { return _this.state; }); | ||
}; | ||
@@ -65,0 +68,0 @@ RequestStore.prototype.performSkippableRequest = function (variables, options) { |
@@ -1,2 +0,2 @@ | ||
import { BehaviorSubject } from "rxjs"; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
@@ -3,0 +3,0 @@ export declare class ValueStore<Value> implements IStore<Value> { |
@@ -1,2 +0,2 @@ | ||
import { BehaviorSubject } from "rxjs"; | ||
import { BehaviorSubject } from 'rxjs'; | ||
var ValueStore = /** @class */ (function () { | ||
@@ -3,0 +3,0 @@ function ValueStore(value) { |
@@ -18,3 +18,3 @@ import { Observable } from 'rxjs'; | ||
* @example | ||
* // original$ - какой-то ObservableWithValue | ||
* // original$ - some other ObservableWithValue | ||
* const obs$ = attachValueToObservable( | ||
@@ -21,0 +21,0 @@ * original$.pipe(map(transform)), |
@@ -11,3 +11,3 @@ "use strict"; | ||
* @example | ||
* // original$ - какой-то ObservableWithValue | ||
* // original$ - some other ObservableWithValue | ||
* const obs$ = attachValueToObservable( | ||
@@ -14,0 +14,0 @@ * original$.pipe(map(transform)), |
import { Observable, Subject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
import { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
import { ObservableWithValue } from './ObservableWithValue'; | ||
export interface IRequestState<Vars, Data> { | ||
@@ -63,3 +64,3 @@ /** Request result */ | ||
/** Observable of the request state */ | ||
state$: Observable<IRequestState<Vars, Data>>; | ||
state$: ObservableWithValue<IRequestState<Vars, Data>>; | ||
/** Default request state. May be used to reset the request */ | ||
@@ -80,3 +81,3 @@ initialState: IRequestState<Vars, Data>; | ||
set state(newValue: IRequestState<Vars, Data>); | ||
createState(): Observable<IRequestState<Vars, Data>>; | ||
createState(): ObservableWithValue<IRequestState<Vars, Data>>; | ||
performSkippableRequest(variables: Vars, options: Options): IObservableData<Data | null>; | ||
@@ -83,0 +84,0 @@ performRequest(variables: Vars, options: Options): IObservableData<Data | null>; |
@@ -7,2 +7,3 @@ "use strict"; | ||
var operators_1 = require("rxjs/operators"); | ||
var ObservableWithValue_1 = require("./ObservableWithValue"); | ||
exports.initialRequestState = { | ||
@@ -59,10 +60,12 @@ data: null, | ||
var _this = this; | ||
return this.variables$.pipe(operators_1.switchMap(function (_a) { | ||
return ObservableWithValue_1.attachValueToObservable(this.variables$.pipe(operators_1.switchMap(function (_a) { | ||
var variables = _a.variables, options = _a.options; | ||
var state = tslib_1.__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 (tslib_1.__assign(tslib_1.__assign({}, _this.state), change)); }), operators_1.tap(function (state) { | ||
}), operators_1.map(function (change) { | ||
return (tslib_1.__assign(tslib_1.__assign({}, _this.state), change)); | ||
}), operators_1.tap(function (state) { | ||
_this.state = state; | ||
_this.updateContextState(state); | ||
}), operators_1.share()); | ||
}), operators_1.shareReplay()), function () { return _this.state; }); | ||
}; | ||
@@ -69,0 +72,0 @@ RequestStore.prototype.performSkippableRequest = function (variables, options) { |
@@ -1,2 +0,2 @@ | ||
import { BehaviorSubject } from "rxjs"; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
@@ -3,0 +3,0 @@ export declare class ValueStore<Value> implements IStore<Value> { |
{ | ||
"name": "@proscom/prostore", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "State management library with multiple stores based on rxjs", | ||
@@ -20,2 +20,3 @@ "author": "Andrew Starostin <a.starostin@proscom.ru>", | ||
"scripts": { | ||
"clean": "rimraf lib node_modules/.cache", | ||
"build": "tsc && tsc -p tsconfig.es.json", | ||
@@ -39,5 +40,6 @@ "watch": "concurrently \"tsc --watch --preserveWatchOutput\" \"tsc --watch --preserveWatchOutput -p tsconfig.es.json\"", | ||
"devDependencies": { | ||
"@types/lodash": "^4.14.165" | ||
"@types/lodash": "^4.14.165", | ||
"rimraf": "^3.0.2" | ||
}, | ||
"gitHead": "747d9c685c9cc10af0e164157a11ba5cd3391f9f" | ||
"gitHead": "0193a32edb750e238e42dea2a5527bc22610e0bf" | ||
} |
@@ -12,3 +12,5 @@ import { BehaviorStore, IStateUpdater } from './BehaviorStore'; | ||
*/ | ||
export class AsyncBehaviorStore<State extends object> extends BehaviorStore<State> { | ||
export class AsyncBehaviorStore< | ||
State extends object | ||
> extends BehaviorStore<State> { | ||
_queuedUpdates: IStateUpdater<State>[] = []; | ||
@@ -21,3 +23,2 @@ _queuedTimeout: any = null; | ||
/** | ||
@@ -24,0 +25,0 @@ * Update part of the store state. |
import { BehaviorSubject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
export type IStateUpdaterFunc<State extends object> = (state: State) => Partial<State>; | ||
export type IStateUpdater<State extends object> = Partial<State> | IStateUpdaterFunc<State>; | ||
export type IStateUpdaterFunc<State extends object> = ( | ||
state: State | ||
) => Partial<State>; | ||
export type IStateUpdater<State extends object> = | ||
| Partial<State> | ||
| IStateUpdaterFunc<State>; | ||
@@ -7,0 +11,0 @@ /** |
/** Function to transform the data to desired output */ | ||
export type IMapData<Data> = (result: any) => Data | Promise<Data>; |
export { insertPaginatedSlice } from '@proscom/ui-utils'; |
@@ -0,0 +0,0 @@ export interface IProstoreSsrContext { |
@@ -20,3 +20,3 @@ import { Observable } from 'rxjs'; | ||
* @example | ||
* // original$ - какой-то ObservableWithValue | ||
* // original$ - some other ObservableWithValue | ||
* const obs$ = attachValueToObservable( | ||
@@ -23,0 +23,0 @@ * original$.pipe(map(transform)), |
import { from, merge, Observable, of, Subject } from 'rxjs'; | ||
import { catchError, map, first, switchMap, tap, share } from 'rxjs/operators'; | ||
import { | ||
catchError, | ||
first, | ||
map, | ||
shareReplay, | ||
switchMap, | ||
tap | ||
} from 'rxjs/operators'; | ||
import { IStore } from './IStore'; | ||
import { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
import { | ||
attachValueToObservable, | ||
ObservableWithValue | ||
} from './ObservableWithValue'; | ||
@@ -88,3 +99,3 @@ export interface IRequestState<Vars, Data> { | ||
/** Observable of the request state */ | ||
public state$: Observable<IRequestState<Vars, Data>>; | ||
public state$: ObservableWithValue<IRequestState<Vars, Data>>; | ||
/** Default request state. May be used to reset the request */ | ||
@@ -134,33 +145,37 @@ public initialState: IRequestState<Vars, Data>; | ||
createState(): Observable<IRequestState<Vars, Data>> { | ||
return this.variables$.pipe( | ||
switchMap( | ||
({ | ||
variables, | ||
options | ||
}): Observable<Partial<IRequestState<Vars, Data>>> => { | ||
const state = { | ||
loading: true, | ||
createState(): ObservableWithValue<IRequestState<Vars, Data>> { | ||
return attachValueToObservable( | ||
this.variables$.pipe( | ||
switchMap( | ||
({ | ||
variables, | ||
error: null, | ||
...options.changeState | ||
}; | ||
options | ||
}): Observable<Partial<IRequestState<Vars, Data>>> => { | ||
const state = { | ||
loading: true, | ||
variables, | ||
error: null, | ||
...options.changeState | ||
}; | ||
return merge( | ||
of(state), | ||
from(this.performSkippableRequest(variables, options)).pipe( | ||
map(this.mapRequestResult(variables, options)), | ||
catchError((error) => of(this.handleError(error))) | ||
) | ||
); | ||
} | ||
return merge( | ||
of(state), | ||
from(this.performSkippableRequest(variables, options)).pipe( | ||
map(this.mapRequestResult(variables, options)), | ||
catchError((error) => of(this.handleError(error))) | ||
) | ||
); | ||
} | ||
), | ||
map( | ||
(change) => | ||
({ ...this.state, ...change } as IRequestState<Vars, Data>) | ||
), | ||
tap((state) => { | ||
this.state = state; | ||
this.updateContextState(state); | ||
}), | ||
shareReplay() | ||
), | ||
map( | ||
(change) => ({ ...this.state, ...change } as IRequestState<Vars, Data>) | ||
), | ||
tap((state) => { | ||
this.state = state; | ||
this.updateContextState(state); | ||
}), | ||
share() | ||
() => this.state | ||
); | ||
@@ -258,5 +273,26 @@ } | ||
export type GetRequestStoreVars<Store> = Store extends RequestStore<infer Vars, any, any> ? Vars : never; | ||
export type GetRequestStoreData<Store> = Store extends RequestStore<any, infer Data, any> ? Data : never; | ||
export type GetRequestStoreOptions<Store> = Store extends RequestStore<any, any, infer Options> ? Options : never; | ||
export type GetRequestStoreState<Store> = IRequestState<GetRequestStoreVars<Store>, GetRequestStoreData<Store>>; | ||
export type GetRequestStoreVars<Store> = Store extends RequestStore< | ||
infer Vars, | ||
any, | ||
any | ||
> | ||
? Vars | ||
: never; | ||
export type GetRequestStoreData<Store> = Store extends RequestStore< | ||
any, | ||
infer Data, | ||
any | ||
> | ||
? Data | ||
: never; | ||
export type GetRequestStoreOptions<Store> = Store extends RequestStore< | ||
any, | ||
any, | ||
infer Options | ||
> | ||
? Options | ||
: never; | ||
export type GetRequestStoreState<Store> = IRequestState< | ||
GetRequestStoreVars<Store>, | ||
GetRequestStoreData<Store> | ||
>; |
@@ -5,3 +5,4 @@ import isEqual from 'lodash/isEqual'; | ||
export type PartialKey<Obj, Key extends keyof Obj> = Omit<Obj, Key> & { [key in Key]?: Obj[Key] }; | ||
export type PartialKey<Obj, Key extends keyof Obj> = Omit<Obj, Key> & | ||
{ [key in Key]?: Obj[Key] }; | ||
@@ -36,3 +37,5 @@ export type VarsCondition<Vars> = (vars: Vars) => boolean; | ||
*/ | ||
export function skipIfNull<Vars, Data>(value: Data|null = null): ISkipQueryFn<Vars, Data> { | ||
export function skipIfNull<Vars, Data>( | ||
value: Data | null = null | ||
): ISkipQueryFn<Vars, Data> { | ||
return (vars: Vars) => { | ||
@@ -55,3 +58,6 @@ if (isNil(vars)) return value; | ||
export type CheckRequestStateFn<Vars, Data> = (state: IRequestState<Vars, Data>, variables: Vars|undefined) => CheckRequestResult; | ||
export type CheckRequestStateFn<Vars, Data> = ( | ||
state: IRequestState<Vars, Data>, | ||
variables: Vars | undefined | ||
) => CheckRequestResult; | ||
@@ -58,0 +64,0 @@ /** |
@@ -0,0 +0,0 @@ import { Subscribable, Unsubscribable } from 'rxjs'; |
@@ -1,2 +0,2 @@ | ||
import { BehaviorSubject, Observable, Subject } from "rxjs"; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { IStore } from './IStore'; | ||
@@ -3,0 +3,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
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
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
117900
1884
2