Comparing version 0.8.0-alpha.2 to 0.8.0-alpha.3
@@ -1,3 +0,7 @@ | ||
## 0.8.0 | ||
## 0.8.0.alpha-2 | ||
Added `IPullstateInstanceConsumable` as an export to help people who want to create code using the Pullstate stores' instance. | ||
## 0.8.0.alpha-1 | ||
Some refactoring of the Async Actions and adding of hooks for much finer grained control: | ||
@@ -4,0 +8,0 @@ |
import { IPullstateAllStores } from "./PullstateCore"; | ||
declare type TPullstateAsyncUpdateListener = () => void; | ||
export declare type TPullstateAsyncWatchResponse<R, T extends string> = [boolean, boolean, TAsyncActionResult<R, T>, boolean]; | ||
export declare type TPullstateAsyncResponseCacheFull<R, T extends string> = [boolean, boolean, TAsyncActionResult<R, T>, boolean, TAsyncActionResult<R, T> | true | null]; | ||
export declare type TPullstateAsyncBeckonResponse<R, T extends string> = [boolean, TAsyncActionResult<R, T>, boolean]; | ||
export declare type TPullstateAsyncRunResponse<R, T extends string> = Promise<TAsyncActionResult<R, T>>; | ||
export interface IPullstateAsyncResultState { | ||
[key: string]: TPullstateAsyncWatchResponse<any, string>; | ||
} | ||
export interface IPullstateAsyncActionOrdState { | ||
[key: string]: number; | ||
} | ||
export declare enum EAsyncEndTags { | ||
THREW_ERROR = "THREW_ERROR", | ||
RETURNED_ERROR = "RETURNED_ERROR", | ||
UNFINISHED = "UNFINISHED" | ||
} | ||
interface IAsyncActionResultBase<T extends string> { | ||
message: string; | ||
tags: (EAsyncEndTags | T)[]; | ||
} | ||
export interface IAsyncActionResultPositive<R, T extends string> extends IAsyncActionResultBase<T> { | ||
error: false; | ||
payload: R; | ||
} | ||
export interface IAsyncActionResultNegative<T extends string> extends IAsyncActionResultBase<T> { | ||
error: true; | ||
payload: null; | ||
} | ||
export declare type TAsyncActionResult<R, T extends string> = IAsyncActionResultPositive<R, T> | IAsyncActionResultNegative<T>; | ||
export declare type TPullstateAsyncAction<A, R, T extends string, S extends IPullstateAllStores> = (args: A, stores: S) => Promise<TAsyncActionResult<R, T>>; | ||
export declare type TPullstateAsyncShortCircuitHook<A, R, T extends string, S extends IPullstateAllStores> = (args: A, stores: S) => TAsyncActionResult<R, T> | false; | ||
export declare type TPullstateAsyncCacheBreakHook<A, R, T extends string, S extends IPullstateAllStores> = (args: A, result: TAsyncActionResult<R, T>, stores: S) => boolean; | ||
export declare enum EPostActionContext { | ||
WATCH_CACHE = "WATCH_CACHE", | ||
RUN_CACHE = "RUN_CACHE", | ||
SHORT_CIRCUIT = "SHORT_CIRCUIT", | ||
DIRECT_RUN = "DIRECT_RUN" | ||
} | ||
export declare type TPullstateAsyncPostActionHook<A, R, T extends string, S extends IPullstateAllStores> = (args: A, result: TAsyncActionResult<R, T>, stores: S, context: EPostActionContext) => TAsyncActionResult<R, T> | void; | ||
export interface IAsyncActionBeckonOptions { | ||
ssr?: boolean; | ||
} | ||
export interface IAsyncActionWatchOptions extends IAsyncActionBeckonOptions { | ||
initiate?: boolean; | ||
} | ||
export interface IAsyncActionRunOptions { | ||
treatAsUpdate?: boolean; | ||
ignoreShortCircuit?: boolean; | ||
respectCache?: boolean; | ||
} | ||
declare type TAsyncActionBeckon<A, R, T extends string> = (args?: A, options?: IAsyncActionBeckonOptions) => TPullstateAsyncBeckonResponse<R, T>; | ||
declare type TAsyncActionWatch<A, R, T extends string> = (args?: A, options?: IAsyncActionWatchOptions) => TPullstateAsyncWatchResponse<R, T>; | ||
declare type TAsyncActionRun<A, R, T extends string> = (args?: A, options?: IAsyncActionRunOptions) => TPullstateAsyncRunResponse<R, T>; | ||
declare type TAsyncActionClearCache<A> = (args?: A) => void; | ||
declare type TAsyncActionClearAllCache = () => void; | ||
export interface IOCreateAsyncActionOutput<A, R, T extends string> { | ||
useBeckon: TAsyncActionBeckon<A, R, T>; | ||
useWatch: TAsyncActionWatch<A, R, T>; | ||
run: TAsyncActionRun<A, R, T>; | ||
clearCache: TAsyncActionClearCache<A>; | ||
clearAllCache: TAsyncActionClearAllCache; | ||
} | ||
export interface IPullstateAsyncCache { | ||
results: IPullstateAsyncResultState; | ||
listeners: { | ||
[key: string]: { | ||
[watchId: string]: TPullstateAsyncUpdateListener; | ||
}; | ||
}; | ||
actions: { | ||
[key: string]: () => Promise<TAsyncActionResult<any, string>>; | ||
}; | ||
actionOrd: IPullstateAsyncActionOrdState; | ||
} | ||
import { EAsyncEndTags, IAsyncActionResultNegative, IAsyncActionResultPositive, ICreateAsyncActionOptions, IOCreateAsyncActionOutput, IPullstateAsyncCache, TPullstateAsyncAction } from "./async-types"; | ||
export declare const clientAsyncCache: IPullstateAsyncCache; | ||
@@ -80,9 +7,2 @@ export declare function keyFromObject(json: any): string; | ||
export declare function errorResult<R = any, T extends string = string>(tags?: (EAsyncEndTags | T)[], message?: string): IAsyncActionResultNegative<T>; | ||
export interface ICreateAsyncActionOptions<A, R, T extends string, S extends IPullstateAllStores> { | ||
clientStores?: S; | ||
shortCircuitHook?: TPullstateAsyncShortCircuitHook<A, R, T, S>; | ||
cacheBreakHook?: TPullstateAsyncCacheBreakHook<A, R, T, S>; | ||
postActionHook?: TPullstateAsyncPostActionHook<A, R, T, S>; | ||
} | ||
export declare function createAsyncAction<A = any, R = any, T extends string = string, S extends IPullstateAllStores = IPullstateAllStores>(action: TPullstateAsyncAction<A, R, T, S>, { clientStores, shortCircuitHook, cacheBreakHook, postActionHook, }?: ICreateAsyncActionOptions<A, R, T, S>): IOCreateAsyncActionOutput<A, R, T>; | ||
export {}; |
import { useStoreState } from "./useStoreState"; | ||
import { update, Store } from "./Store"; | ||
import { Store, update } from "./Store"; | ||
import { InjectStoreState } from "./InjectStoreState"; | ||
import { PullstateProvider, useStores, createPullstateCore, IPullstateInstanceConsumable } from "./PullstateCore"; | ||
import { createAsyncAction, successResult, errorResult, EAsyncEndTags } from "./async"; | ||
export { useStoreState, update, Store, InjectStoreState, PullstateProvider, useStores, createPullstateCore, createAsyncAction, successResult, errorResult, EAsyncEndTags, IPullstateInstanceConsumable, }; | ||
import { createPullstateCore, IPullstateInstanceConsumable, PullstateProvider, useStores } from "./PullstateCore"; | ||
import { createAsyncAction, errorResult, successResult } from "./async"; | ||
import { EAsyncActionInjectType, InjectAsyncAction } from "./InjectAsyncAction"; | ||
import { EAsyncEndTags } from "./async-types"; | ||
export { useStoreState, update, Store, InjectStoreState, PullstateProvider, useStores, createPullstateCore, createAsyncAction, successResult, errorResult, EAsyncEndTags, IPullstateInstanceConsumable, InjectAsyncAction, EAsyncActionInjectType, }; |
@@ -122,4 +122,3 @@ import React,{useState,useRef,useCallback,useEffect,useContext,useMemo}from'react';const shallowEqual = require("fbjs/lib/shallowEqual"); | ||
return children(state); | ||
}const shallowEqual$1 = require("fbjs/lib/shallowEqual"); | ||
var EAsyncEndTags; | ||
}var EAsyncEndTags; | ||
(function (EAsyncEndTags) { | ||
@@ -132,7 +131,8 @@ EAsyncEndTags["THREW_ERROR"] = "THREW_ERROR"; | ||
(function (EPostActionContext) { | ||
EPostActionContext["WATCH_CACHE"] = "WATCH_CACHE"; | ||
EPostActionContext["RUN_CACHE"] = "RUN_CACHE"; | ||
EPostActionContext["WATCH_HIT_CACHE"] = "WATCH_HIT_CACHE"; | ||
EPostActionContext["RUN_HIT_CACHE"] = "RUN_HIT_CACHE"; | ||
EPostActionContext["SHORT_CIRCUIT"] = "SHORT_CIRCUIT"; | ||
EPostActionContext["DIRECT_RUN"] = "DIRECT_RUN"; | ||
})(EPostActionContext || (EPostActionContext = {})); | ||
EPostActionContext["BECKON_RUN"] = "BECKON_RUN"; | ||
})(EPostActionContext || (EPostActionContext = {}));const shallowEqual$1 = require("fbjs/lib/shallowEqual"); | ||
const clientAsyncCache = { | ||
@@ -217,12 +217,11 @@ listeners: {}, | ||
if (postActionHook !== undefined) { | ||
const potentialResponse = postActionHook(args, result, stores, context); | ||
return potentialResponse != null ? potentialResponse : result; | ||
postActionHook({ args, result, stores, context }); | ||
} | ||
return result; | ||
} | ||
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores) { | ||
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false) { | ||
if (cache.results.hasOwnProperty(key)) { | ||
const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2; | ||
if (cacheBreakHook !== undefined && | ||
cacheBreakHook(args, cache.results[key][2], stores) && | ||
if (initiate && | ||
cacheBreakHook !== undefined && | ||
cacheBreakHook({ args, result: cache.results[key][2], stores }) && | ||
!cacheBreakLoop) { | ||
@@ -247,13 +246,6 @@ if (cacheBreakWatcher.hasOwnProperty(key)) { | ||
} | ||
if (cache.results[key][1]) { | ||
return [ | ||
cache.results[key][0], | ||
cache.results[key][1], | ||
runPostActionHook(cache.results[key][2], args, stores, EPostActionContext.WATCH_CACHE), | ||
cache.results[key][3], | ||
]; | ||
if (cache.results[key][1] && !fromListener) { | ||
runPostActionHook(cache.results[key][2], args, stores, EPostActionContext.WATCH_HIT_CACHE); | ||
} | ||
else { | ||
return cache.results[key]; | ||
} | ||
return cache.results[key]; | ||
} | ||
@@ -263,11 +255,7 @@ } | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook(args, stores); | ||
const shortCircuitResponse = shortCircuitHook({ args, stores }); | ||
if (shortCircuitResponse !== false) { | ||
cache.results[key] = [true, true, shortCircuitResponse, false]; | ||
return [ | ||
true, | ||
true, | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT), | ||
false, | ||
]; | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
return cache.results[key]; | ||
} | ||
@@ -285,12 +273,16 @@ } | ||
cache.results[key] = [true, true, resp, false]; | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
}) | ||
.catch(e => { | ||
console.error(e); | ||
if (currentActionOrd === cache.actionOrd[key]) { | ||
cache.results[key] = [ | ||
true, | ||
true, | ||
{ payload: null, error: true, tags: [EAsyncEndTags.THREW_ERROR], message: e.message }, | ||
false, | ||
]; | ||
const result = { | ||
payload: null, | ||
error: true, | ||
tags: [EAsyncEndTags.THREW_ERROR], | ||
message: e.message, | ||
}; | ||
cache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -336,3 +328,3 @@ }) | ||
} | ||
const prevKeyRef = useRef(key); | ||
const prevKeyRef = useRef(null); | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
@@ -353,3 +345,3 @@ shouldUpdate[key] = { | ||
if (shouldUpdate[key][watchId.current] && !shallowEqual$1(responseRef.current, cache.results[key])) { | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, true); | ||
setWatchUpdate(prev => { | ||
@@ -372,8 +364,7 @@ return prev + 1; | ||
const responseRef = useRef(null); | ||
if (responseRef.current === null) { | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
} | ||
const [watchUpdate, setWatchUpdate] = useState(0); | ||
const [_, setWatchUpdate] = useState(0); | ||
if (prevKeyRef.current !== key) { | ||
shouldUpdate[prevKeyRef.current][watchId.current] = false; | ||
if (prevKeyRef.current !== null) { | ||
shouldUpdate[prevKeyRef.current][watchId.current] = false; | ||
} | ||
prevKeyRef.current = key; | ||
@@ -388,19 +379,33 @@ responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
}; | ||
const run = async (args = {}, { treatAsUpdate = false, ignoreShortCircuit = false, respectCache = false } = {}) => { | ||
const run = async (args = {}, { treatAsUpdate = false, ignoreShortCircuit = false, respectCache = false, _asyncCache = clientAsyncCache, _stores = clientStores, } = {}) => { | ||
const key = createKey(ordinal, args); | ||
if (clientAsyncCache.results.hasOwnProperty(key) && respectCache) { | ||
if (_asyncCache.results.hasOwnProperty(key) && respectCache) { | ||
if (cacheBreakHook !== undefined && | ||
cacheBreakHook(args, clientAsyncCache.results[key][2], clientStores)) { | ||
delete clientAsyncCache.results[key]; | ||
cacheBreakHook({ | ||
args, | ||
result: _asyncCache.results[key][2], | ||
stores: _stores, | ||
})) { | ||
delete _asyncCache.results[key]; | ||
} | ||
else { | ||
if (clientAsyncCache.results[key][1]) { | ||
return runPostActionHook(clientAsyncCache.results[key][2], args, clientStores, EPostActionContext.RUN_CACHE); | ||
if (_asyncCache.results[key][1]) { | ||
runPostActionHook(_asyncCache.results[key][2], args, _stores, EPostActionContext.RUN_HIT_CACHE); | ||
return _asyncCache.results[key][2]; | ||
} | ||
else { | ||
return clientAsyncCache.results[key][2]; | ||
return _asyncCache.results[key][2]; | ||
} | ||
} | ||
} | ||
const [, prevFinished, prevResp] = clientAsyncCache.results[key] || [ | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook({ args, stores: _stores }); | ||
if (shortCircuitResponse !== false) { | ||
_asyncCache.results[key] = [true, true, shortCircuitResponse, false]; | ||
runPostActionHook(shortCircuitResponse, args, _stores, EPostActionContext.SHORT_CIRCUIT); | ||
notifyListeners(key); | ||
return shortCircuitResponse; | ||
} | ||
} | ||
const [, prevFinished, prevResp] = _asyncCache.results[key] || [ | ||
false, | ||
@@ -416,26 +421,20 @@ false, | ||
if (prevFinished && treatAsUpdate) { | ||
clientAsyncCache.results[key] = [true, true, prevResp, true]; | ||
_asyncCache.results[key] = [true, true, prevResp, true]; | ||
} | ||
else { | ||
clientAsyncCache.results[key] = [true, false, prevResp, false]; | ||
_asyncCache.results[key] = [true, false, prevResp, false]; | ||
} | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook(args, clientStores); | ||
if (shortCircuitResponse !== false) { | ||
clientAsyncCache.results[key] = [true, true, shortCircuitResponse, false]; | ||
notifyListeners(key); | ||
return runPostActionHook(shortCircuitResponse, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
} | ||
} | ||
notifyListeners(key); | ||
let currentActionOrd = actionOrdUpdate(clientAsyncCache, key); | ||
let currentActionOrd = actionOrdUpdate(_asyncCache, key); | ||
try { | ||
const result = await action(args, clientStores); | ||
if (currentActionOrd === clientAsyncCache.actionOrd[key]) { | ||
clientAsyncCache.results[key] = [true, true, result, false]; | ||
const result = await action(args, _stores); | ||
if (currentActionOrd === _asyncCache.actionOrd[key]) { | ||
_asyncCache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN); | ||
notifyListeners(key); | ||
} | ||
return runPostActionHook(result, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
return result; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
const result = { | ||
@@ -447,7 +446,8 @@ error: true, | ||
}; | ||
if (currentActionOrd === clientAsyncCache.actionOrd[key]) { | ||
clientAsyncCache.results[key] = [true, true, result, false]; | ||
if (currentActionOrd === _asyncCache.actionOrd[key]) { | ||
_asyncCache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN); | ||
notifyListeners(key); | ||
} | ||
return runPostActionHook(result, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
return result; | ||
} | ||
@@ -543,7 +543,13 @@ }; | ||
}) | ||
.catch(() => { | ||
.catch(e => { | ||
console.error(e); | ||
this._asyncCache.results[key] = [ | ||
true, | ||
true, | ||
{ error: true, message: "", tags: [EAsyncEndTags.THREW_ERROR], payload: null }, | ||
{ | ||
error: true, | ||
message: "Threw error on server", | ||
tags: [EAsyncEndTags.THREW_ERROR], | ||
payload: null, | ||
}, | ||
false, | ||
@@ -578,2 +584,7 @@ ]; | ||
} | ||
async runAsyncAction(asyncAction, args = {}, runOptions = {}) { | ||
runOptions._asyncCache = this._asyncCache; | ||
runOptions._stores = this._stores; | ||
return await asyncAction.run(args, runOptions); | ||
} | ||
hydrateFromSnapshot(snapshot) { | ||
@@ -597,2 +608,14 @@ for (const storeName of Object.keys(this._stores)) { | ||
return useContext(PullstateContext).stores; | ||
}export{useStoreState,update,Store,InjectStoreState,PullstateProvider,useStores,createPullstateCore,createAsyncAction,successResult,errorResult,EAsyncEndTags}; | ||
}var EAsyncActionInjectType; | ||
(function (EAsyncActionInjectType) { | ||
EAsyncActionInjectType["WATCH"] = "WATCH"; | ||
EAsyncActionInjectType["BECKON"] = "BECKON"; | ||
})(EAsyncActionInjectType || (EAsyncActionInjectType = {})); | ||
function InjectAsyncAction(props) { | ||
if (props.type === EAsyncActionInjectType.BECKON) { | ||
const response = props.action.useBeckon(props.args, props.options); | ||
return props.children(response); | ||
} | ||
const response = props.action.useWatch(props.args, props.options); | ||
return props.children(response); | ||
}export{useStoreState,update,Store,InjectStoreState,PullstateProvider,useStores,createPullstateCore,createAsyncAction,successResult,errorResult,EAsyncEndTags,InjectAsyncAction,EAsyncActionInjectType}; |
@@ -122,4 +122,3 @@ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var React=require('react'),React__default=_interopDefault(React);const shallowEqual = require("fbjs/lib/shallowEqual"); | ||
return children(state); | ||
}const shallowEqual$1 = require("fbjs/lib/shallowEqual"); | ||
(function (EAsyncEndTags) { | ||
}(function (EAsyncEndTags) { | ||
EAsyncEndTags["THREW_ERROR"] = "THREW_ERROR"; | ||
@@ -131,7 +130,8 @@ EAsyncEndTags["RETURNED_ERROR"] = "RETURNED_ERROR"; | ||
(function (EPostActionContext) { | ||
EPostActionContext["WATCH_CACHE"] = "WATCH_CACHE"; | ||
EPostActionContext["RUN_CACHE"] = "RUN_CACHE"; | ||
EPostActionContext["WATCH_HIT_CACHE"] = "WATCH_HIT_CACHE"; | ||
EPostActionContext["RUN_HIT_CACHE"] = "RUN_HIT_CACHE"; | ||
EPostActionContext["SHORT_CIRCUIT"] = "SHORT_CIRCUIT"; | ||
EPostActionContext["DIRECT_RUN"] = "DIRECT_RUN"; | ||
})(EPostActionContext || (EPostActionContext = {})); | ||
EPostActionContext["BECKON_RUN"] = "BECKON_RUN"; | ||
})(EPostActionContext || (EPostActionContext = {}));const shallowEqual$1 = require("fbjs/lib/shallowEqual"); | ||
const clientAsyncCache = { | ||
@@ -216,12 +216,11 @@ listeners: {}, | ||
if (postActionHook !== undefined) { | ||
const potentialResponse = postActionHook(args, result, stores, context); | ||
return potentialResponse != null ? potentialResponse : result; | ||
postActionHook({ args, result, stores, context }); | ||
} | ||
return result; | ||
} | ||
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores) { | ||
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false) { | ||
if (cache.results.hasOwnProperty(key)) { | ||
const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2; | ||
if (cacheBreakHook !== undefined && | ||
cacheBreakHook(args, cache.results[key][2], stores) && | ||
if (initiate && | ||
cacheBreakHook !== undefined && | ||
cacheBreakHook({ args, result: cache.results[key][2], stores }) && | ||
!cacheBreakLoop) { | ||
@@ -246,13 +245,6 @@ if (cacheBreakWatcher.hasOwnProperty(key)) { | ||
} | ||
if (cache.results[key][1]) { | ||
return [ | ||
cache.results[key][0], | ||
cache.results[key][1], | ||
runPostActionHook(cache.results[key][2], args, stores, EPostActionContext.WATCH_CACHE), | ||
cache.results[key][3], | ||
]; | ||
if (cache.results[key][1] && !fromListener) { | ||
runPostActionHook(cache.results[key][2], args, stores, EPostActionContext.WATCH_HIT_CACHE); | ||
} | ||
else { | ||
return cache.results[key]; | ||
} | ||
return cache.results[key]; | ||
} | ||
@@ -262,11 +254,7 @@ } | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook(args, stores); | ||
const shortCircuitResponse = shortCircuitHook({ args, stores }); | ||
if (shortCircuitResponse !== false) { | ||
cache.results[key] = [true, true, shortCircuitResponse, false]; | ||
return [ | ||
true, | ||
true, | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT), | ||
false, | ||
]; | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
return cache.results[key]; | ||
} | ||
@@ -284,12 +272,16 @@ } | ||
cache.results[key] = [true, true, resp, false]; | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
}) | ||
.catch(e => { | ||
console.error(e); | ||
if (currentActionOrd === cache.actionOrd[key]) { | ||
cache.results[key] = [ | ||
true, | ||
true, | ||
{ payload: null, error: true, tags: [exports.EAsyncEndTags.THREW_ERROR], message: e.message }, | ||
false, | ||
]; | ||
const result = { | ||
payload: null, | ||
error: true, | ||
tags: [exports.EAsyncEndTags.THREW_ERROR], | ||
message: e.message, | ||
}; | ||
cache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -335,3 +327,3 @@ }) | ||
} | ||
const prevKeyRef = React.useRef(key); | ||
const prevKeyRef = React.useRef(null); | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
@@ -352,3 +344,3 @@ shouldUpdate[key] = { | ||
if (shouldUpdate[key][watchId.current] && !shallowEqual$1(responseRef.current, cache.results[key])) { | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, true); | ||
setWatchUpdate(prev => { | ||
@@ -371,8 +363,7 @@ return prev + 1; | ||
const responseRef = React.useRef(null); | ||
if (responseRef.current === null) { | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
} | ||
const [watchUpdate, setWatchUpdate] = React.useState(0); | ||
const [_, setWatchUpdate] = React.useState(0); | ||
if (prevKeyRef.current !== key) { | ||
shouldUpdate[prevKeyRef.current][watchId.current] = false; | ||
if (prevKeyRef.current !== null) { | ||
shouldUpdate[prevKeyRef.current][watchId.current] = false; | ||
} | ||
prevKeyRef.current = key; | ||
@@ -387,19 +378,33 @@ responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores); | ||
}; | ||
const run = async (args = {}, { treatAsUpdate = false, ignoreShortCircuit = false, respectCache = false } = {}) => { | ||
const run = async (args = {}, { treatAsUpdate = false, ignoreShortCircuit = false, respectCache = false, _asyncCache = clientAsyncCache, _stores = clientStores, } = {}) => { | ||
const key = createKey(ordinal, args); | ||
if (clientAsyncCache.results.hasOwnProperty(key) && respectCache) { | ||
if (_asyncCache.results.hasOwnProperty(key) && respectCache) { | ||
if (cacheBreakHook !== undefined && | ||
cacheBreakHook(args, clientAsyncCache.results[key][2], clientStores)) { | ||
delete clientAsyncCache.results[key]; | ||
cacheBreakHook({ | ||
args, | ||
result: _asyncCache.results[key][2], | ||
stores: _stores, | ||
})) { | ||
delete _asyncCache.results[key]; | ||
} | ||
else { | ||
if (clientAsyncCache.results[key][1]) { | ||
return runPostActionHook(clientAsyncCache.results[key][2], args, clientStores, EPostActionContext.RUN_CACHE); | ||
if (_asyncCache.results[key][1]) { | ||
runPostActionHook(_asyncCache.results[key][2], args, _stores, EPostActionContext.RUN_HIT_CACHE); | ||
return _asyncCache.results[key][2]; | ||
} | ||
else { | ||
return clientAsyncCache.results[key][2]; | ||
return _asyncCache.results[key][2]; | ||
} | ||
} | ||
} | ||
const [, prevFinished, prevResp] = clientAsyncCache.results[key] || [ | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook({ args, stores: _stores }); | ||
if (shortCircuitResponse !== false) { | ||
_asyncCache.results[key] = [true, true, shortCircuitResponse, false]; | ||
runPostActionHook(shortCircuitResponse, args, _stores, EPostActionContext.SHORT_CIRCUIT); | ||
notifyListeners(key); | ||
return shortCircuitResponse; | ||
} | ||
} | ||
const [, prevFinished, prevResp] = _asyncCache.results[key] || [ | ||
false, | ||
@@ -415,26 +420,20 @@ false, | ||
if (prevFinished && treatAsUpdate) { | ||
clientAsyncCache.results[key] = [true, true, prevResp, true]; | ||
_asyncCache.results[key] = [true, true, prevResp, true]; | ||
} | ||
else { | ||
clientAsyncCache.results[key] = [true, false, prevResp, false]; | ||
_asyncCache.results[key] = [true, false, prevResp, false]; | ||
} | ||
if (shortCircuitHook !== undefined) { | ||
const shortCircuitResponse = shortCircuitHook(args, clientStores); | ||
if (shortCircuitResponse !== false) { | ||
clientAsyncCache.results[key] = [true, true, shortCircuitResponse, false]; | ||
notifyListeners(key); | ||
return runPostActionHook(shortCircuitResponse, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
} | ||
} | ||
notifyListeners(key); | ||
let currentActionOrd = actionOrdUpdate(clientAsyncCache, key); | ||
let currentActionOrd = actionOrdUpdate(_asyncCache, key); | ||
try { | ||
const result = await action(args, clientStores); | ||
if (currentActionOrd === clientAsyncCache.actionOrd[key]) { | ||
clientAsyncCache.results[key] = [true, true, result, false]; | ||
const result = await action(args, _stores); | ||
if (currentActionOrd === _asyncCache.actionOrd[key]) { | ||
_asyncCache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN); | ||
notifyListeners(key); | ||
} | ||
return runPostActionHook(result, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
return result; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
const result = { | ||
@@ -446,7 +445,8 @@ error: true, | ||
}; | ||
if (currentActionOrd === clientAsyncCache.actionOrd[key]) { | ||
clientAsyncCache.results[key] = [true, true, result, false]; | ||
if (currentActionOrd === _asyncCache.actionOrd[key]) { | ||
_asyncCache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN); | ||
notifyListeners(key); | ||
} | ||
return runPostActionHook(result, args, clientStores, EPostActionContext.DIRECT_RUN); | ||
return result; | ||
} | ||
@@ -542,7 +542,13 @@ }; | ||
}) | ||
.catch(() => { | ||
.catch(e => { | ||
console.error(e); | ||
this._asyncCache.results[key] = [ | ||
true, | ||
true, | ||
{ error: true, message: "", tags: [exports.EAsyncEndTags.THREW_ERROR], payload: null }, | ||
{ | ||
error: true, | ||
message: "Threw error on server", | ||
tags: [exports.EAsyncEndTags.THREW_ERROR], | ||
payload: null, | ||
}, | ||
false, | ||
@@ -577,2 +583,7 @@ ]; | ||
} | ||
async runAsyncAction(asyncAction, args = {}, runOptions = {}) { | ||
runOptions._asyncCache = this._asyncCache; | ||
runOptions._stores = this._stores; | ||
return await asyncAction.run(args, runOptions); | ||
} | ||
hydrateFromSnapshot(snapshot) { | ||
@@ -596,2 +607,13 @@ for (const storeName of Object.keys(this._stores)) { | ||
return React.useContext(PullstateContext).stores; | ||
}exports.useStoreState=useStoreState;exports.update=update;exports.Store=Store;exports.InjectStoreState=InjectStoreState;exports.PullstateProvider=PullstateProvider;exports.useStores=useStores;exports.createPullstateCore=createPullstateCore;exports.createAsyncAction=createAsyncAction;exports.successResult=successResult;exports.errorResult=errorResult; | ||
}(function (EAsyncActionInjectType) { | ||
EAsyncActionInjectType["WATCH"] = "WATCH"; | ||
EAsyncActionInjectType["BECKON"] = "BECKON"; | ||
})(exports.EAsyncActionInjectType || (exports.EAsyncActionInjectType = {})); | ||
function InjectAsyncAction(props) { | ||
if (props.type === exports.EAsyncActionInjectType.BECKON) { | ||
const response = props.action.useBeckon(props.args, props.options); | ||
return props.children(response); | ||
} | ||
const response = props.action.useWatch(props.args, props.options); | ||
return props.children(response); | ||
}exports.useStoreState=useStoreState;exports.update=update;exports.Store=Store;exports.InjectStoreState=InjectStoreState;exports.PullstateProvider=PullstateProvider;exports.useStores=useStores;exports.createPullstateCore=createPullstateCore;exports.createAsyncAction=createAsyncAction;exports.successResult=successResult;exports.errorResult=errorResult;exports.InjectAsyncAction=InjectAsyncAction; |
import React from "react"; | ||
import { Store } from "./Store"; | ||
import { ICreateAsyncActionOptions, IOCreateAsyncActionOutput, IPullstateAsyncActionOrdState, IPullstateAsyncCache, IPullstateAsyncResultState, TPullstateAsyncAction } from "./async"; | ||
import { IAsyncActionRunOptions, ICreateAsyncActionOptions, IOCreateAsyncActionOutput, IPullstateAsyncActionOrdState, IPullstateAsyncCache, IPullstateAsyncResultState, TPullstateAsyncAction, TPullstateAsyncRunResponse } from "./async-types"; | ||
declare type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
@@ -13,3 +13,2 @@ export interface IPullstateAllStores { | ||
}) => JSX.Element; | ||
export declare type IUseAsyncWatcherResponse<ET extends string[] = string[]> = [boolean, boolean, ET]; | ||
export declare class PullstateSingleton<S extends IPullstateAllStores = IPullstateAllStores> { | ||
@@ -38,2 +37,3 @@ private readonly originStores; | ||
hydrateFromSnapshot(snapshot: IPullstateSnapshot): void; | ||
runAsyncAction<A, R, X extends string>(asyncAction: IOCreateAsyncActionOutput<A, R, X>, args?: A, runOptions?: Pick<IAsyncActionRunOptions, "ignoreShortCircuit" | "respectCache">): TPullstateAsyncRunResponse<R, X>; | ||
} | ||
@@ -50,2 +50,3 @@ declare class PullstateInstance<T extends IPullstateAllStores = IPullstateAllStores> implements IPullstateInstanceConsumable<T> { | ||
readonly stores: T; | ||
runAsyncAction<A, R, X extends string>(asyncAction: IOCreateAsyncActionOutput<A, R, X>, args?: A, runOptions?: Pick<IAsyncActionRunOptions, "ignoreShortCircuit" | "respectCache">): TPullstateAsyncRunResponse<R, X>; | ||
hydrateFromSnapshot(snapshot: IPullstateSnapshot): void; | ||
@@ -52,0 +53,0 @@ } |
{ | ||
"name": "pullstate", | ||
"version": "0.8.0-alpha.2", | ||
"version": "0.8.0-alpha.3", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
90453
14
1447