Comparing version 1.7.3 to 1.8.0
@@ -1,6 +0,10 @@ | ||
## 1.7.3 | ||
## 1.8.0 | ||
* Added the passable option `{ dormant: true }` to Async Function's `useBeckon()` or `useWatch()`, which will basically just make the action completely dormant - no execution or hitting of cache or anything, but will still respect the option `{ holdPrevious: true }`, returning the last completed result for this action if it exists. | ||
### 1.7.3 | ||
[TypeScript] Minor type updates for calling `useStore()` directly on one of your stores, so that the "sub-state" function gets the store's state interface correctly. | ||
## 1.7.2 | ||
### 1.7.2 | ||
@@ -15,3 +19,3 @@ Minor quality of life update, able to now set successful cached payloads directly in the cache using | ||
## 1.7.1 | ||
### 1.7.1 | ||
@@ -18,0 +22,0 @@ Slight change to how `Store.update()` runs when accepting an array of updaters. It now runs each update separately on the state, allowing for updates further down the line to act on previous updates (still triggers re-renders of your React components as if it were a single update). |
@@ -60,2 +60,3 @@ import { IPullstateAllStores } from "./PullstateCore"; | ||
holdPrevious?: boolean; | ||
dormant?: boolean; | ||
} | ||
@@ -132,2 +133,3 @@ export interface IAsyncActionWatchOptions extends IAsyncActionBeckonOptions { | ||
export interface ICreateAsyncActionOptions<A, R, T extends string, S extends IPullstateAllStores> { | ||
forceContext?: boolean; | ||
clientStores?: S; | ||
@@ -134,0 +136,0 @@ shortCircuitHook?: TPullstateAsyncShortCircuitHook<A, R, T, S>; |
@@ -7,2 +7,2 @@ import { IPullstateAllStores } from "./PullstateCore"; | ||
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, subsetKey, }?: ICreateAsyncActionOptions<A, R, T, S>): IOCreateAsyncActionOutput<A, R, T>; | ||
export declare function createAsyncAction<A = any, R = any, T extends string = string, S extends IPullstateAllStores = IPullstateAllStores>(action: TPullstateAsyncAction<A, R, T, S>, { forceContext, clientStores, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, }?: ICreateAsyncActionOptions<A, R, T, S>): IOCreateAsyncActionOutput<A, R, T>; |
@@ -401,3 +401,3 @@ import React,{useState,useRef,useEffect,useContext,useMemo}from'react';import produce$1 from'immer';const isEqual = require("fast-deep-equal"); | ||
} | ||
function createAsyncAction(action, { clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) { | ||
function createAsyncAction(action, { forceContext = false, clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) { | ||
const ordinal = asyncCreationOrdinal++; | ||
@@ -484,9 +484,3 @@ const onServer = typeof window === "undefined"; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [ | ||
true, | ||
true, | ||
result, | ||
false, | ||
Date.now(), | ||
]; | ||
cache.results[key] = [true, true, result, false, Date.now()]; | ||
} | ||
@@ -543,6 +537,6 @@ }) | ||
} | ||
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, } = {}) => { | ||
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, dormant = false, } = {}) => { | ||
const responseRef = useRef(); | ||
const prevKeyRef = useRef(); | ||
const key = _createKey(ordinal, args); | ||
const prevKeyRef = useRef("."); | ||
const key = dormant ? "." : _createKey(ordinal, args); | ||
let watchId = useRef(-1); | ||
@@ -552,14 +546,14 @@ if (watchId.current === -1) { | ||
} | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
shouldUpdate[key] = { | ||
[watchId.current]: true, | ||
}; | ||
if (!dormant) { | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
shouldUpdate[key] = { | ||
[watchId.current]: true, | ||
}; | ||
} | ||
else { | ||
shouldUpdate[key][watchId.current] = true; | ||
} | ||
} | ||
else { | ||
shouldUpdate[key][watchId.current] = true; | ||
} | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const stores = onServer ? useContext(PullstateContext).stores : clientStores; | ||
const cache = onServer ? useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
const stores = onServer || forceContext ? useContext(PullstateContext).stores : clientStores; | ||
if (!onServer) { | ||
@@ -575,14 +569,35 @@ const onAsyncStateChanged = () => { | ||
useMemo(() => { | ||
if (!cache.listeners.hasOwnProperty(key)) { | ||
cache.listeners[key] = {}; | ||
if (!dormant) { | ||
if (!cache.listeners.hasOwnProperty(key)) { | ||
cache.listeners[key] = {}; | ||
} | ||
cache.listeners[key][watchId.current] = onAsyncStateChanged; | ||
} | ||
cache.listeners[key][watchId.current] = onAsyncStateChanged; | ||
}, [key]); | ||
useEffect(() => () => { | ||
delete cache.listeners[key][watchId.current]; | ||
shouldUpdate[key][watchId.current] = false; | ||
if (!dormant) { | ||
delete cache.listeners[key][watchId.current]; | ||
shouldUpdate[key][watchId.current] = false; | ||
} | ||
}, [key]); | ||
} | ||
const [_, setWatchUpdate] = useState(0); | ||
if (prevKeyRef.current !== key) { | ||
if (dormant) { | ||
responseRef.current = | ||
holdPrevious && responseRef.current && responseRef.current[1] | ||
? responseRef.current | ||
: [ | ||
false, | ||
false, | ||
{ | ||
message: "", | ||
tags: [EAsyncEndTags.UNFINISHED], | ||
error: true, | ||
payload: null, | ||
}, | ||
false, | ||
-1, | ||
]; | ||
} | ||
else if (prevKeyRef.current !== key) { | ||
if (prevKeyRef.current !== null && shouldUpdate.hasOwnProperty(prevKeyRef.current)) { | ||
@@ -593,8 +608,8 @@ delete cache.listeners[prevKeyRef.current][watchId.current]; | ||
prevKeyRef.current = key; | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, false, postActionEnabled, cacheBreakEnabled, (holdPrevious && responseRef.current && responseRef.current[1]) ? responseRef.current : undefined); | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, false, postActionEnabled, cacheBreakEnabled, holdPrevious && responseRef.current && responseRef.current[1] ? responseRef.current : undefined); | ||
} | ||
return responseRef.current; | ||
}; | ||
const useBeckon = (args = {}, { ssr = true, postActionEnabled = true, cacheBreakEnabled = true, holdPrevious = false } = {}) => { | ||
const result = useWatch(args, { initiate: true, ssr, postActionEnabled, cacheBreakEnabled, holdPrevious }); | ||
const useBeckon = (args = {}, { ssr = true, postActionEnabled = true, cacheBreakEnabled = true, holdPrevious = false, dormant = false, } = {}) => { | ||
const result = useWatch(args, { initiate: true, ssr, postActionEnabled, cacheBreakEnabled, holdPrevious, dormant }); | ||
return [result[1], result[2], result[3]]; | ||
@@ -711,5 +726,3 @@ }; | ||
const key = _createKey(ordinal, args); | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
cache.results[key] = [true, true, result, false, Date.now()]; | ||
@@ -726,5 +739,3 @@ if (notify) { | ||
const key = _createKey(ordinal, args); | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key) && !cache.results[key][2].error) { | ||
@@ -753,5 +764,3 @@ const currentCached = cache.results[key][2].payload; | ||
let cacheBreakable = false; | ||
const cache = onServer | ||
? useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key)) { | ||
@@ -758,0 +767,0 @@ if (checkCacheBreak && cacheBreakHook !== undefined) { |
@@ -400,3 +400,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),produce$1=_interopDefault(require('immer'));const isEqual = require("fast-deep-equal"); | ||
} | ||
function createAsyncAction(action, { clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) { | ||
function createAsyncAction(action, { forceContext = false, clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) { | ||
const ordinal = asyncCreationOrdinal++; | ||
@@ -483,9 +483,3 @@ const onServer = typeof window === "undefined"; | ||
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN); | ||
cache.results[key] = [ | ||
true, | ||
true, | ||
result, | ||
false, | ||
Date.now(), | ||
]; | ||
cache.results[key] = [true, true, result, false, Date.now()]; | ||
} | ||
@@ -542,6 +536,6 @@ }) | ||
} | ||
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, } = {}) => { | ||
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, dormant = false, } = {}) => { | ||
const responseRef = React.useRef(); | ||
const prevKeyRef = React.useRef(); | ||
const key = _createKey(ordinal, args); | ||
const prevKeyRef = React.useRef("."); | ||
const key = dormant ? "." : _createKey(ordinal, args); | ||
let watchId = React.useRef(-1); | ||
@@ -551,14 +545,14 @@ if (watchId.current === -1) { | ||
} | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
shouldUpdate[key] = { | ||
[watchId.current]: true, | ||
}; | ||
if (!dormant) { | ||
if (!shouldUpdate.hasOwnProperty(key)) { | ||
shouldUpdate[key] = { | ||
[watchId.current]: true, | ||
}; | ||
} | ||
else { | ||
shouldUpdate[key][watchId.current] = true; | ||
} | ||
} | ||
else { | ||
shouldUpdate[key][watchId.current] = true; | ||
} | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const stores = onServer ? React.useContext(PullstateContext).stores : clientStores; | ||
const cache = onServer ? React.useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
const stores = onServer || forceContext ? React.useContext(PullstateContext).stores : clientStores; | ||
if (!onServer) { | ||
@@ -574,14 +568,35 @@ const onAsyncStateChanged = () => { | ||
React.useMemo(() => { | ||
if (!cache.listeners.hasOwnProperty(key)) { | ||
cache.listeners[key] = {}; | ||
if (!dormant) { | ||
if (!cache.listeners.hasOwnProperty(key)) { | ||
cache.listeners[key] = {}; | ||
} | ||
cache.listeners[key][watchId.current] = onAsyncStateChanged; | ||
} | ||
cache.listeners[key][watchId.current] = onAsyncStateChanged; | ||
}, [key]); | ||
React.useEffect(() => () => { | ||
delete cache.listeners[key][watchId.current]; | ||
shouldUpdate[key][watchId.current] = false; | ||
if (!dormant) { | ||
delete cache.listeners[key][watchId.current]; | ||
shouldUpdate[key][watchId.current] = false; | ||
} | ||
}, [key]); | ||
} | ||
const [_, setWatchUpdate] = React.useState(0); | ||
if (prevKeyRef.current !== key) { | ||
if (dormant) { | ||
responseRef.current = | ||
holdPrevious && responseRef.current && responseRef.current[1] | ||
? responseRef.current | ||
: [ | ||
false, | ||
false, | ||
{ | ||
message: "", | ||
tags: [exports.EAsyncEndTags.UNFINISHED], | ||
error: true, | ||
payload: null, | ||
}, | ||
false, | ||
-1, | ||
]; | ||
} | ||
else if (prevKeyRef.current !== key) { | ||
if (prevKeyRef.current !== null && shouldUpdate.hasOwnProperty(prevKeyRef.current)) { | ||
@@ -592,8 +607,8 @@ delete cache.listeners[prevKeyRef.current][watchId.current]; | ||
prevKeyRef.current = key; | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, false, postActionEnabled, cacheBreakEnabled, (holdPrevious && responseRef.current && responseRef.current[1]) ? responseRef.current : undefined); | ||
responseRef.current = checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, false, postActionEnabled, cacheBreakEnabled, holdPrevious && responseRef.current && responseRef.current[1] ? responseRef.current : undefined); | ||
} | ||
return responseRef.current; | ||
}; | ||
const useBeckon = (args = {}, { ssr = true, postActionEnabled = true, cacheBreakEnabled = true, holdPrevious = false } = {}) => { | ||
const result = useWatch(args, { initiate: true, ssr, postActionEnabled, cacheBreakEnabled, holdPrevious }); | ||
const useBeckon = (args = {}, { ssr = true, postActionEnabled = true, cacheBreakEnabled = true, holdPrevious = false, dormant = false, } = {}) => { | ||
const result = useWatch(args, { initiate: true, ssr, postActionEnabled, cacheBreakEnabled, holdPrevious, dormant }); | ||
return [result[1], result[2], result[3]]; | ||
@@ -710,5 +725,3 @@ }; | ||
const key = _createKey(ordinal, args); | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? React.useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
cache.results[key] = [true, true, result, false, Date.now()]; | ||
@@ -725,5 +738,3 @@ if (notify) { | ||
const key = _createKey(ordinal, args); | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? React.useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key) && !cache.results[key][2].error) { | ||
@@ -752,5 +763,3 @@ const currentCached = cache.results[key][2].payload; | ||
let cacheBreakable = false; | ||
const cache = onServer | ||
? React.useContext(PullstateContext)._asyncCache | ||
: clientAsyncCache; | ||
const cache = onServer ? React.useContext(PullstateContext)._asyncCache : clientAsyncCache; | ||
if (cache.results.hasOwnProperty(key)) { | ||
@@ -757,0 +766,0 @@ if (checkCacheBreak && cacheBreakHook !== undefined) { |
{ | ||
"name": "pullstate", | ||
"version": "1.7.3", | ||
"version": "1.8.0", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
118568
2265