Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,14 @@ | ||
## 1.1.0 | ||
Fixed issue with postActionHook not being called on the server for Async Actions. | ||
Added the following methods on Async Actions: | ||
* `setCached()` | ||
* `updateCached()` | ||
For a more finer-grained control of async action cache. | ||
`updateCached()` functions exactly the same as `update()` on stores, except it only runs on a previously successfully returned cached value. If nothing is cached, nothing is run. | ||
## 1.0.0-beta.7 | ||
@@ -2,0 +15,0 @@ |
import { IPullstateAllStores } from "./PullstateCore"; | ||
import { TUpdateFunction } from "./Store"; | ||
declare type TPullstateAsyncUpdateListener = () => void; | ||
@@ -85,2 +86,4 @@ export declare type TPullstateAsyncWatchResponse<R = any, T extends string = string> = [boolean, boolean, TAsyncActionResult<R, T>, boolean]; | ||
export declare type TAsyncActionClearAllCache = () => void; | ||
export declare type TAsyncActionSetCached<A, R, T extends string> = (args: A, result: TAsyncActionResult<R, T>) => void; | ||
export declare type TAsyncActionUpdateCached<A, R, T extends string> = (args: A, updater: TUpdateFunction<R>) => void; | ||
export declare type TAsyncActionGetCached<A, R, T extends string> = (args?: A, options?: IAsyncActionGetCachedOptions) => IGetCachedResponse<R, T>; | ||
@@ -98,2 +101,4 @@ export declare type TAsyncActionDelayedRun<A> = (args?: A, options?: IAsyncActionRunOptions & { | ||
getCached: TAsyncActionGetCached<A, R, T>; | ||
setCached: TAsyncActionSetCached<A, R, T>; | ||
updateCached: TAsyncActionUpdateCached<A, R, T>; | ||
clearCache: TAsyncActionClearCache<A>; | ||
@@ -100,0 +105,0 @@ clearAllCache: TAsyncActionClearAllCache; |
@@ -5,4 +5,4 @@ import { IPullstateAllStores } from "./PullstateCore"; | ||
export declare function keyFromObject(json: any): string; | ||
export declare function successResult<R extends any = null, T extends string = string>(payload?: R, tags?: (EAsyncEndTags | T)[], message?: string): IAsyncActionResultPositive<R, T>; | ||
export declare function successResult<R, T extends string = string>(payload?: R, tags?: (EAsyncEndTags | T)[], message?: string): IAsyncActionResultPositive<R, T>; | ||
export declare function errorResult<R = any, T extends string = string>(tags?: (EAsyncEndTags | T)[], message?: string): IAsyncActionResultNegative<T>; | ||
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>; |
@@ -1,2 +0,2 @@ | ||
import React,{useState,useRef,useCallback,useEffect,useContext,useMemo}from'react';const isEqual = require("fast-deep-equal"); | ||
import React,{useState,useRef,useCallback,useEffect,useContext,useMemo}from'react';import produce$1 from'immer';const isEqual = require("fast-deep-equal"); | ||
function useStoreState(store, getSubState) { | ||
@@ -278,2 +278,3 @@ const [subState, setSubState] = useState(() => getSubState ? getSubState(store.getRawState()) : store.getRawState()); | ||
} | ||
let currentActionOrd = actionOrdUpdate(cache, key); | ||
if (!cache.actions.hasOwnProperty(key)) { | ||
@@ -284,4 +285,4 @@ if (initiate) { | ||
if (shortCircuitResponse !== false) { | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
cache.results[key] = [true, true, shortCircuitResponse, false]; | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
return cache.results[key]; | ||
@@ -291,11 +292,6 @@ } | ||
if (ssr || !onServer) { | ||
cache.actions[key] = () => action(args, stores); | ||
} | ||
let currentActionOrd = actionOrdUpdate(cache, key); | ||
if (!onServer) { | ||
cache.actions[key]() | ||
.then(resp => { | ||
cache.actions[key] = () => action(args, stores).then(resp => { | ||
if (currentActionOrd === cache.actionOrd[key]) { | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [true, true, resp, false]; | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -312,4 +308,4 @@ }) | ||
}; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -319,5 +315,10 @@ }) | ||
delete cache.actions[key]; | ||
notifyListeners(key); | ||
if (!onServer) { | ||
notifyListeners(key); | ||
} | ||
}); | ||
} | ||
if (!onServer) { | ||
cache.actions[key](); | ||
} | ||
} | ||
@@ -498,2 +499,19 @@ else { | ||
}; | ||
const setCached = (args, result) => { | ||
const key = createKey(ordinal, args); | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
cache.results[key] = [true, true, result, false]; | ||
}; | ||
const updateCached = (args, updater) => { | ||
const key = createKey(ordinal, args); | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key) && !cache.results[key][2].error) { | ||
const currentCached = cache.results[key][2].payload; | ||
cache.results[key][2].payload = produce$1(currentCached, s => updater(s, currentCached)); | ||
} | ||
}; | ||
const getCached = (args = {}, { checkCacheBreak = false } = {}) => { | ||
@@ -572,2 +590,4 @@ const key = createKey(ordinal, args); | ||
getCached, | ||
setCached, | ||
updateCached, | ||
}; | ||
@@ -642,23 +662,3 @@ }const PullstateContext = React.createContext(null); | ||
getAllUnresolvedAsyncActions() { | ||
return Object.keys(this._asyncCache.actions).map(key => this._asyncCache.actions[key]() | ||
.then(resp => { | ||
this._asyncCache.results[key] = [true, true, resp, false]; | ||
}) | ||
.catch(e => { | ||
console.error(e); | ||
this._asyncCache.results[key] = [ | ||
true, | ||
true, | ||
{ | ||
error: true, | ||
message: "Threw error on server", | ||
tags: [EAsyncEndTags.THREW_ERROR], | ||
payload: null, | ||
}, | ||
false, | ||
]; | ||
}) | ||
.then(() => { | ||
delete this._asyncCache.actions[key]; | ||
})); | ||
return Object.keys(this._asyncCache.actions).map(key => this._asyncCache.actions[key]()); | ||
} | ||
@@ -665,0 +665,0 @@ instantiateReactions() { |
@@ -1,2 +0,2 @@ | ||
'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 isEqual = require("fast-deep-equal"); | ||
'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),produce$1=_interopDefault(require('immer'));const isEqual = require("fast-deep-equal"); | ||
function useStoreState(store, getSubState) { | ||
@@ -277,2 +277,3 @@ const [subState, setSubState] = React.useState(() => getSubState ? getSubState(store.getRawState()) : store.getRawState()); | ||
} | ||
let currentActionOrd = actionOrdUpdate(cache, key); | ||
if (!cache.actions.hasOwnProperty(key)) { | ||
@@ -283,4 +284,4 @@ if (initiate) { | ||
if (shortCircuitResponse !== false) { | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
cache.results[key] = [true, true, shortCircuitResponse, false]; | ||
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT); | ||
return cache.results[key]; | ||
@@ -290,11 +291,6 @@ } | ||
if (ssr || !onServer) { | ||
cache.actions[key] = () => action(args, stores); | ||
} | ||
let currentActionOrd = actionOrdUpdate(cache, key); | ||
if (!onServer) { | ||
cache.actions[key]() | ||
.then(resp => { | ||
cache.actions[key] = () => action(args, stores).then(resp => { | ||
if (currentActionOrd === cache.actionOrd[key]) { | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [true, true, resp, false]; | ||
runPostActionHook(resp, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -311,4 +307,4 @@ }) | ||
}; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [true, true, result, false]; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
} | ||
@@ -318,5 +314,10 @@ }) | ||
delete cache.actions[key]; | ||
notifyListeners(key); | ||
if (!onServer) { | ||
notifyListeners(key); | ||
} | ||
}); | ||
} | ||
if (!onServer) { | ||
cache.actions[key](); | ||
} | ||
} | ||
@@ -497,2 +498,19 @@ else { | ||
}; | ||
const setCached = (args, result) => { | ||
const key = createKey(ordinal, args); | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
cache.results[key] = [true, true, result, false]; | ||
}; | ||
const updateCached = (args, updater) => { | ||
const key = createKey(ordinal, args); | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key) && !cache.results[key][2].error) { | ||
const currentCached = cache.results[key][2].payload; | ||
cache.results[key][2].payload = produce$1(currentCached, s => updater(s, currentCached)); | ||
} | ||
}; | ||
const getCached = (args = {}, { checkCacheBreak = false } = {}) => { | ||
@@ -571,2 +589,4 @@ const key = createKey(ordinal, args); | ||
getCached, | ||
setCached, | ||
updateCached, | ||
}; | ||
@@ -641,23 +661,3 @@ }const PullstateContext = React__default.createContext(null); | ||
getAllUnresolvedAsyncActions() { | ||
return Object.keys(this._asyncCache.actions).map(key => this._asyncCache.actions[key]() | ||
.then(resp => { | ||
this._asyncCache.results[key] = [true, true, resp, false]; | ||
}) | ||
.catch(e => { | ||
console.error(e); | ||
this._asyncCache.results[key] = [ | ||
true, | ||
true, | ||
{ | ||
error: true, | ||
message: "Threw error on server", | ||
tags: [exports.EAsyncEndTags.THREW_ERROR], | ||
payload: null, | ||
}, | ||
false, | ||
]; | ||
}) | ||
.then(() => { | ||
delete this._asyncCache.actions[key]; | ||
})); | ||
return Object.keys(this._asyncCache.actions).map(key => this._asyncCache.actions[key]()); | ||
} | ||
@@ -664,0 +664,0 @@ instantiateReactions() { |
@@ -7,3 +7,3 @@ import { Patch } from "immer"; | ||
} | ||
declare type TUpdateFunction<S> = (draft: S, original: S) => void; | ||
export declare type TUpdateFunction<S> = (draft: S, original: S) => void; | ||
declare type TReactionFunction<S, T> = (watched: T, draft: S, original: S, previousWatched: T) => void; | ||
@@ -10,0 +10,0 @@ declare type TRunReactionFunction = () => void; |
{ | ||
"name": "pullstate", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -11,2 +11,3 @@ "main": "dist/index.js", | ||
"test": "jest", | ||
"test-watch": "jest --watch", | ||
"clean": "rimraf ./dist", | ||
@@ -13,0 +14,0 @@ "build": "npm run clean && rollup -c", |
85240
1695