@proscom/prostore
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -63,3 +63,7 @@ var __assign = (this && this.__assign) || function () { | ||
this.mapRequestResult = function (variables, options) { return function (data) { return ({ | ||
data: _this.updateData(data, _this.state.data, { store: _this, variables: variables, options: options }), | ||
data: _this.updateData(data, _this.state.data, { | ||
store: _this, | ||
variables: variables, | ||
options: options | ||
}), | ||
loading: false, | ||
@@ -97,3 +101,3 @@ loaded: true, | ||
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({}, _this.state, change)); }), tap(function (state) { return _this.state = state; }), share()); | ||
}), map(function (change) { return (__assign({}, _this.state, change)); }), tap(function (state) { return (_this.state = state); }), share()); | ||
}; | ||
@@ -117,3 +121,9 @@ RequestStore.prototype.performSkippableRequest = function (variables, options) { | ||
case 0: | ||
promise = this.state$.pipe(first(function (state) { return !state.loading && (state.loaded || state.error) && state.variables === variables; })).toPromise(); | ||
promise = this.state$ | ||
.pipe(first(function (state) { | ||
return !state.loading && | ||
(state.loaded || state.error) && | ||
state.variables === variables; | ||
})) | ||
.toPromise(); | ||
this.variables$.next({ variables: variables, options: options }); | ||
@@ -120,0 +130,0 @@ return [4 /*yield*/, promise]; |
@@ -27,3 +27,3 @@ import * as isEqual from 'lodash/isEqual'; | ||
error: false, | ||
component: false, | ||
component: false | ||
}; | ||
@@ -30,0 +30,0 @@ if (!state) |
@@ -65,3 +65,7 @@ "use strict"; | ||
this.mapRequestResult = function (variables, options) { return function (data) { return ({ | ||
data: _this.updateData(data, _this.state.data, { store: _this, variables: variables, options: options }), | ||
data: _this.updateData(data, _this.state.data, { | ||
store: _this, | ||
variables: variables, | ||
options: options | ||
}), | ||
loading: false, | ||
@@ -99,3 +103,3 @@ loaded: true, | ||
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()); | ||
}), operators_1.map(function (change) { return (__assign({}, _this.state, change)); }), operators_1.tap(function (state) { return (_this.state = state); }), operators_1.share()); | ||
}; | ||
@@ -119,3 +123,9 @@ RequestStore.prototype.performSkippableRequest = function (variables, options) { | ||
case 0: | ||
promise = this.state$.pipe(operators_1.first(function (state) { return !state.loading && (state.loaded || state.error) && state.variables === variables; })).toPromise(); | ||
promise = this.state$ | ||
.pipe(operators_1.first(function (state) { | ||
return !state.loading && | ||
(state.loaded || state.error) && | ||
state.variables === variables; | ||
})) | ||
.toPromise(); | ||
this.variables$.next({ variables: variables, options: options }); | ||
@@ -122,0 +132,0 @@ return [4 /*yield*/, promise]; |
@@ -31,3 +31,3 @@ "use strict"; | ||
error: false, | ||
component: false, | ||
component: false | ||
}; | ||
@@ -34,0 +34,0 @@ if (!state) |
{ | ||
"name": "@proscom/prostore", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "> TODO: description", | ||
@@ -29,3 +29,3 @@ "author": "Andrew Starostin <a.starostin@proscom.ru>", | ||
"sideEffects": false, | ||
"gitHead": "8205e91b12ac93b36c5e6b56762defb8f6a0ffa5" | ||
"gitHead": "b8b6b42dcb3ac856330ec90e37e1ee937fb67dec" | ||
} |
@@ -0,0 +0,0 @@ # `prostore` |
import { Observable } from 'rxjs'; | ||
export interface IStore<State> { | ||
readonly state: State | ||
readonly state$: Observable<State> | ||
readonly state: State; | ||
readonly state$: Observable<State>; | ||
} |
@@ -1,5 +0,5 @@ | ||
import {IStore} from './IStore'; | ||
import {from, merge, Observable, of, Subject} from 'rxjs'; | ||
import {catchError, map, first, switchMap, tap, share} from 'rxjs/operators'; | ||
import {IProstoreSsrContext} from './IProstoreSsrContext'; | ||
import { IStore } from './IStore'; | ||
import { from, merge, Observable, of, Subject } from 'rxjs'; | ||
import { catchError, map, first, switchMap, tap, share } from 'rxjs/operators'; | ||
import { IProstoreSsrContext } from './IProstoreSsrContext'; | ||
@@ -24,3 +24,7 @@ export interface IRequestState<Vars, Data> { | ||
export type IUpdateDataFn<Vars, Data> = (data: Data, oldData: Data, params: {store: any, variables: Vars, options: any}) => Data; | ||
export type IUpdateDataFn<Vars, Data> = ( | ||
data: Data, | ||
oldData: Data, | ||
params: { store: any; variables: Vars; options: any } | ||
) => Data; | ||
@@ -46,3 +50,4 @@ export interface IRequestStoreParams<Vars, Data> { | ||
export class RequestStore<Vars, Data, Options extends IRequestStoreOptions> implements IStore<IRequestState<Vars, Data>> { | ||
export class RequestStore<Vars, Data, Options extends IRequestStoreOptions> | ||
implements IStore<IRequestState<Vars, Data>> { | ||
public ssrId?: string; | ||
@@ -85,20 +90,27 @@ public state$: Observable<IRequestState<Vars, Data>>; | ||
return this.variables$.pipe( | ||
switchMap(({ variables, options }): Observable<Partial<IRequestState<Vars, Data>>> => { | ||
const state = { | ||
loading: true, | ||
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))) | ||
) | ||
); | ||
}), | ||
map((change) => ({ ...this.state, ...change } as IRequestState<Vars, Data>)), | ||
tap((state) => this.state = state), | ||
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)), | ||
share() | ||
@@ -108,3 +120,6 @@ ); | ||
performSkippableRequest(variables: Vars, options: Options): IObservableData<Data> { | ||
performSkippableRequest( | ||
variables: Vars, | ||
options: Options | ||
): IObservableData<Data> { | ||
const skipped = this.skipQuery(variables); | ||
@@ -119,9 +134,21 @@ if (skipped !== undefined) { | ||
performRequest(variables: Vars, options: Options): IObservableData<Data> { | ||
throw new Error('Please override performRequest function in your RequestStore extension'); | ||
throw new Error( | ||
'Please override performRequest function in your RequestStore extension' | ||
); | ||
} | ||
async loadData(variables: Vars, options: any = {}): Promise<IRequestState<Vars, Data>> { | ||
const promise = this.state$.pipe( | ||
first((state) => !state.loading && (state.loaded || state.error) && state.variables === variables) | ||
).toPromise(); | ||
async loadData( | ||
variables: Vars, | ||
options: any = {} | ||
): Promise<IRequestState<Vars, Data>> { | ||
const promise = this.state$ | ||
.pipe( | ||
first( | ||
(state) => | ||
!state.loading && | ||
(state.loaded || state.error) && | ||
state.variables === variables | ||
) | ||
) | ||
.toPromise(); | ||
this.variables$.next({ variables, options }); | ||
@@ -131,4 +158,10 @@ return await promise; | ||
mapRequestResult = (variables: Vars, options: Options) => (data: Data): IRequestState<Vars, Data> => ({ | ||
data: this.updateData(data, this.state.data, { store: this, variables, options }), | ||
mapRequestResult = (variables: Vars, options: Options) => ( | ||
data: Data | ||
): IRequestState<Vars, Data> => ({ | ||
data: this.updateData(data, this.state.data, { | ||
store: this, | ||
variables, | ||
options | ||
}), | ||
loading: false, | ||
@@ -168,3 +201,2 @@ loaded: true, | ||
} | ||
} |
import * as isEqual from 'lodash/isEqual'; | ||
import * as isNil from 'lodash/isNil'; | ||
import {IRequestState, ISkipQueryFn} from './RequestStore'; | ||
import { IRequestState, ISkipQueryFn } from './RequestStore'; | ||
export type VarsCondition<Vars> = (vars: Vars) => boolean; | ||
export function skipIf<Vars, Data>(condition: VarsCondition<Vars>, value: Data): ISkipQueryFn<Vars, Data> { | ||
export function skipIf<Vars, Data>( | ||
condition: VarsCondition<Vars>, | ||
value: Data | ||
): ISkipQueryFn<Vars, Data> { | ||
return (vars: Vars) => { | ||
@@ -18,3 +21,3 @@ if (condition(vars)) return value; | ||
return undefined; | ||
} | ||
}; | ||
} | ||
@@ -34,3 +37,6 @@ | ||
*/ | ||
export function checkRequestState<Vars>(state: IRequestState<Vars, any>, variables: Vars): CheckRequestResult { | ||
export function checkRequestState<Vars>( | ||
state: IRequestState<Vars, any>, | ||
variables: Vars | ||
): CheckRequestResult { | ||
const result = { | ||
@@ -40,3 +46,3 @@ request: false, | ||
error: false, | ||
component: false, | ||
component: false | ||
}; | ||
@@ -43,0 +49,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
59979
54
1148