New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pullstate

Package Overview
Dependencies
Maintainers
1
Versions
99
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pullstate - npm Package Compare versions

Comparing version 1.7.3 to 1.8.0-dev-20200113-1809

dist/PSuspense.d.ts

10

Changelog.md

@@ -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).

@@ -45,2 +45,4 @@ import { IPullstateAllStores } from "./PullstateCore";

RUN_HIT_CACHE = "RUN_HIT_CACHE",
READ_HIT_CACHE = "READ_HIT_CACHE",
READ_RUN = "READ_RUN",
SHORT_CIRCUIT = "SHORT_CIRCUIT",

@@ -56,7 +58,10 @@ DIRECT_RUN = "DIRECT_RUN",

}) => void;
export interface IAsyncActionBeckonOptions {
ssr?: boolean;
export interface IAsyncActionReadOptions {
postActionEnabled?: boolean;
cacheBreakEnabled?: boolean;
}
export interface IAsyncActionBeckonOptions extends IAsyncActionReadOptions {
ssr?: boolean;
holdPrevious?: boolean;
dormant?: boolean;
}

@@ -101,2 +106,3 @@ export interface IAsyncActionWatchOptions extends IAsyncActionBeckonOptions {

export declare type TAsyncActionUpdateCached<A, R> = (args: A, updater: TUpdateFunction<R>, options?: IAsyncActionUpdateCachedOptions) => void;
export declare type TAsyncActionRead<A, R> = (args?: A, options?: IAsyncActionReadOptions) => R;
export declare type TAsyncActionDelayedRun<A> = (args: A, options: IAsyncActionRunOptions & {

@@ -108,2 +114,3 @@ delay: number;

export interface IOCreateAsyncActionOutput<A = any, R = any, T extends string = string> {
read: TAsyncActionRead<A, R>;
useBeckon: TAsyncActionBeckon<A, R, T>;

@@ -135,2 +142,3 @@ useWatch: TAsyncActionWatch<A, R, T>;

export interface ICreateAsyncActionOptions<A, R, T extends string, S extends IPullstateAllStores> {
forceContext?: boolean;
clientStores?: S;

@@ -137,0 +145,0 @@ shortCircuitHook?: TPullstateAsyncShortCircuitHook<A, R, T, S>;

@@ -7,2 +7,7 @@ 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 class PullstateAsyncError extends Error {
message: string;
tags: string[];
constructor(message: string, tags: string[]);
}
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>;

@@ -326,2 +326,4 @@ import React,{useState,useRef,useEffect,useContext,useMemo}from'react';import produce$1 from'immer';const isEqual = require("fast-deep-equal");

EPostActionContext["RUN_HIT_CACHE"] = "RUN_HIT_CACHE";
EPostActionContext["READ_HIT_CACHE"] = "READ_HIT_CACHE";
EPostActionContext["READ_RUN"] = "READ_RUN";
EPostActionContext["SHORT_CIRCUIT"] = "SHORT_CIRCUIT";

@@ -402,3 +404,9 @@ EPostActionContext["DIRECT_RUN"] = "DIRECT_RUN";

}
function createAsyncAction(action, { clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) {
class PullstateAsyncError extends Error {
constructor(message, tags) {
super(message);
this.tags = tags;
}
}
function createAsyncAction(action, { forceContext = false, clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) {
const ordinal = asyncCreationOrdinal++;

@@ -420,3 +428,3 @@ const onServer = typeof window === "undefined";

}
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false, postActionEnabled = true, cacheBreakEnabled = true, holdingResult = undefined) {
function getCachedResult(key, cache, args, stores, context, postActionEnabled, cacheBreakEnabled, fromListener) {
if (cache.results.hasOwnProperty(key)) {

@@ -452,3 +460,3 @@ const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2;

if (postActionEnabled && cache.results[key][1] && !fromListener) {
runPostActionHook(cache.results[key][2], args, stores, initiate ? EPostActionContext.BECKON_HIT_CACHE : EPostActionContext.WATCH_HIT_CACHE);
runPostActionHook(cache.results[key][2], args, stores, context);
}

@@ -458,2 +466,44 @@ return cache.results[key];

}
return undefined;
}
function createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, context) {
return () => action(args, stores)
.then(resp => {
if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(resp, args, stores, context);
}
cache.results[key] = [true, true, resp, false, Date.now()];
}
return resp;
})
.catch(e => {
console.error(e);
const result = {
payload: null,
error: true,
tags: [EAsyncEndTags.THREW_ERROR],
message: e.message,
};
if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(result, args, stores, context);
}
cache.results[key] = [true, true, result, false, Date.now()];
}
return result;
})
.then(resp => {
delete cache.actions[key];
if (!onServer) {
notifyListeners(key);
}
return resp;
});
}
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false, postActionEnabled = true, cacheBreakEnabled = true, holdingResult = undefined) {
const cached = getCachedResult(key, cache, args, stores, initiate ? EPostActionContext.BECKON_HIT_CACHE : EPostActionContext.WATCH_HIT_CACHE, postActionEnabled, cacheBreakEnabled, fromListener);
if (cached) {
return cached;
}
if (!cache.actions.hasOwnProperty(key)) {

@@ -471,34 +521,3 @@ const currentActionOrd = actionOrdUpdate(cache, key);

if (ssr || !onServer) {
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, Date.now()];
}
})
.catch(e => {
console.error(e);
if (currentActionOrd === cache.actionOrd[key]) {
const result = {
payload: null,
error: true,
tags: [EAsyncEndTags.THREW_ERROR],
message: e.message,
};
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN);
cache.results[key] = [
true,
true,
result,
false,
Date.now(),
];
}
})
.then(() => {
delete cache.actions[key];
if (!onServer) {
notifyListeners(key);
}
});
cache.actions[key] = createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, EPostActionContext.BECKON_RUN);
}

@@ -547,6 +566,48 @@ if (!onServer) {

}
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, } = {}) => {
const read = (args = {}, { cacheBreakEnabled = true, postActionEnabled = true } = {}) => {
const key = _createKey(ordinal, args);
const cache = onServer ? useContext(PullstateContext)._asyncCache : clientAsyncCache;
const stores = onServer || forceContext ? useContext(PullstateContext).stores : clientStores;
const cached = getCachedResult(key, cache, args, stores, EPostActionContext.READ_HIT_CACHE, postActionEnabled, cacheBreakEnabled, false);
if (cached) {
if (!cached[2].error) {
return cached[2].payload;
}
else {
throw new PullstateAsyncError(cached[2].message, cached[2].tags);
}
}
if (!cache.actions.hasOwnProperty(key)) {
if (shortCircuitHook !== undefined) {
const shortCircuitResponse = shortCircuitHook({ args, stores });
if (shortCircuitResponse !== false) {
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT);
cache.results[key] = [true, true, shortCircuitResponse, false, Date.now()];
if (!shortCircuitResponse.error) {
return shortCircuitResponse.payload;
}
else {
throw new PullstateAsyncError(shortCircuitResponse.message, shortCircuitResponse.tags);
}
}
}
const currentActionOrd = actionOrdUpdate(cache, key);
cache.actions[key] = createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, EPostActionContext.READ_RUN);
if (onServer) {
throw new Error(`Pullstate Async Action: action.read() : Resolve all async state for Suspense actions before Server-side render ( make use of instance.runAsyncAction() )`);
}
throw cache.actions[key]();
}
if (onServer) {
throw new Error(`Pullstate Async Action: action.read() : Resolve all async state for Suspense actions before Server-side render ( make use of instance.runAsyncAction() )`);
}
const watchOrd = watchIdOrd++;
throw new Promise(resolve => {
cache.listeners[key][watchOrd] = resolve;
});
};
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);

@@ -556,14 +617,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) {

@@ -579,14 +640,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)) {

@@ -597,8 +679,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]];

@@ -608,21 +690,7 @@ };

const key = _createKey(ordinal, args);
if (_asyncCache.results.hasOwnProperty(key) && respectCache) {
if (cacheBreakHook !== undefined &&
cacheBreakHook({
args,
result: _asyncCache.results[key][2],
stores: _stores,
timeCached: _asyncCache.results[key][4],
})) {
delete _asyncCache.results[key];
if (respectCache) {
const cached = getCachedResult(key, _asyncCache, args, _stores, EPostActionContext.RUN_HIT_CACHE, true, true, false);
if (cached) {
return cached[2];
}
else {
if (_asyncCache.results[key][1]) {
runPostActionHook(_asyncCache.results[key][2], args, _stores, EPostActionContext.RUN_HIT_CACHE);
return _asyncCache.results[key][2];
}
else {
return _asyncCache.results[key][2];
}
}
}

@@ -669,26 +737,4 @@ if (!ignoreShortCircuit && shortCircuitHook !== undefined) {

let currentActionOrd = actionOrdUpdate(_asyncCache, key);
try {
const result = await action(args, _stores);
if (currentActionOrd === _asyncCache.actionOrd[key]) {
_asyncCache.results[key] = [true, true, result, false, Date.now()];
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN);
notifyListeners(key);
}
return result;
}
catch (e) {
console.error(e);
const result = {
error: true,
message: e.message,
tags: [EAsyncEndTags.THREW_ERROR],
payload: null,
};
if (currentActionOrd === _asyncCache.actionOrd[key]) {
_asyncCache.results[key] = [true, true, result, false, Date.now()];
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN);
notifyListeners(key);
}
return result;
}
_asyncCache.actions[key] = createInternalAction(key, _asyncCache, args, _stores, currentActionOrd, true, EPostActionContext.DIRECT_RUN);
return _asyncCache.actions[key]();
};

@@ -717,5 +763,3 @@ const clearCache = (args = {}) => {

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()];

@@ -732,5 +776,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) {

@@ -759,5 +801,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)) {

@@ -826,2 +866,3 @@ if (checkCacheBreak && cacheBreakHook !== undefined) {

return {
read,
useBeckon,

@@ -828,0 +869,0 @@ useWatch,

@@ -325,2 +325,4 @@ '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");

EPostActionContext["RUN_HIT_CACHE"] = "RUN_HIT_CACHE";
EPostActionContext["READ_HIT_CACHE"] = "READ_HIT_CACHE";
EPostActionContext["READ_RUN"] = "READ_RUN";
EPostActionContext["SHORT_CIRCUIT"] = "SHORT_CIRCUIT";

@@ -401,3 +403,9 @@ EPostActionContext["DIRECT_RUN"] = "DIRECT_RUN";

}
function createAsyncAction(action, { clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) {
class PullstateAsyncError extends Error {
constructor(message, tags) {
super(message);
this.tags = tags;
}
}
function createAsyncAction(action, { forceContext = false, clientStores = {}, shortCircuitHook, cacheBreakHook, postActionHook, subsetKey, } = {}) {
const ordinal = asyncCreationOrdinal++;

@@ -419,3 +427,3 @@ const onServer = typeof window === "undefined";

}
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false, postActionEnabled = true, cacheBreakEnabled = true, holdingResult = undefined) {
function getCachedResult(key, cache, args, stores, context, postActionEnabled, cacheBreakEnabled, fromListener) {
if (cache.results.hasOwnProperty(key)) {

@@ -451,3 +459,3 @@ const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2;

if (postActionEnabled && cache.results[key][1] && !fromListener) {
runPostActionHook(cache.results[key][2], args, stores, initiate ? EPostActionContext.BECKON_HIT_CACHE : EPostActionContext.WATCH_HIT_CACHE);
runPostActionHook(cache.results[key][2], args, stores, context);
}

@@ -457,2 +465,44 @@ return cache.results[key];

}
return undefined;
}
function createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, context) {
return () => action(args, stores)
.then(resp => {
if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(resp, args, stores, context);
}
cache.results[key] = [true, true, resp, false, Date.now()];
}
return resp;
})
.catch(e => {
console.error(e);
const result = {
payload: null,
error: true,
tags: [exports.EAsyncEndTags.THREW_ERROR],
message: e.message,
};
if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(result, args, stores, context);
}
cache.results[key] = [true, true, result, false, Date.now()];
}
return result;
})
.then(resp => {
delete cache.actions[key];
if (!onServer) {
notifyListeners(key);
}
return resp;
});
}
function checkKeyAndReturnResponse(key, cache, initiate, ssr, args, stores, fromListener = false, postActionEnabled = true, cacheBreakEnabled = true, holdingResult = undefined) {
const cached = getCachedResult(key, cache, args, stores, initiate ? EPostActionContext.BECKON_HIT_CACHE : EPostActionContext.WATCH_HIT_CACHE, postActionEnabled, cacheBreakEnabled, fromListener);
if (cached) {
return cached;
}
if (!cache.actions.hasOwnProperty(key)) {

@@ -470,34 +520,3 @@ const currentActionOrd = actionOrdUpdate(cache, key);

if (ssr || !onServer) {
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, Date.now()];
}
})
.catch(e => {
console.error(e);
if (currentActionOrd === cache.actionOrd[key]) {
const result = {
payload: null,
error: true,
tags: [exports.EAsyncEndTags.THREW_ERROR],
message: e.message,
};
runPostActionHook(result, args, stores, EPostActionContext.BECKON_RUN);
cache.results[key] = [
true,
true,
result,
false,
Date.now(),
];
}
})
.then(() => {
delete cache.actions[key];
if (!onServer) {
notifyListeners(key);
}
});
cache.actions[key] = createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, EPostActionContext.BECKON_RUN);
}

@@ -546,6 +565,48 @@ if (!onServer) {

}
const useWatch = (args = {}, { initiate = false, ssr = true, postActionEnabled = false, cacheBreakEnabled = false, holdPrevious = false, } = {}) => {
const read = (args = {}, { cacheBreakEnabled = true, postActionEnabled = true } = {}) => {
const key = _createKey(ordinal, args);
const cache = onServer ? React.useContext(PullstateContext)._asyncCache : clientAsyncCache;
const stores = onServer || forceContext ? React.useContext(PullstateContext).stores : clientStores;
const cached = getCachedResult(key, cache, args, stores, EPostActionContext.READ_HIT_CACHE, postActionEnabled, cacheBreakEnabled, false);
if (cached) {
if (!cached[2].error) {
return cached[2].payload;
}
else {
throw new PullstateAsyncError(cached[2].message, cached[2].tags);
}
}
if (!cache.actions.hasOwnProperty(key)) {
if (shortCircuitHook !== undefined) {
const shortCircuitResponse = shortCircuitHook({ args, stores });
if (shortCircuitResponse !== false) {
runPostActionHook(shortCircuitResponse, args, stores, EPostActionContext.SHORT_CIRCUIT);
cache.results[key] = [true, true, shortCircuitResponse, false, Date.now()];
if (!shortCircuitResponse.error) {
return shortCircuitResponse.payload;
}
else {
throw new PullstateAsyncError(shortCircuitResponse.message, shortCircuitResponse.tags);
}
}
}
const currentActionOrd = actionOrdUpdate(cache, key);
cache.actions[key] = createInternalAction(key, cache, args, stores, currentActionOrd, postActionEnabled, EPostActionContext.READ_RUN);
if (onServer) {
throw new Error(`Pullstate Async Action: action.read() : Resolve all async state for Suspense actions before Server-side render ( make use of instance.runAsyncAction() )`);
}
throw cache.actions[key]();
}
if (onServer) {
throw new Error(`Pullstate Async Action: action.read() : Resolve all async state for Suspense actions before Server-side render ( make use of instance.runAsyncAction() )`);
}
const watchOrd = watchIdOrd++;
throw new Promise(resolve => {
cache.listeners[key][watchOrd] = resolve;
});
};
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);

@@ -555,14 +616,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) {

@@ -578,14 +639,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)) {

@@ -596,8 +678,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]];

@@ -607,21 +689,7 @@ };

const key = _createKey(ordinal, args);
if (_asyncCache.results.hasOwnProperty(key) && respectCache) {
if (cacheBreakHook !== undefined &&
cacheBreakHook({
args,
result: _asyncCache.results[key][2],
stores: _stores,
timeCached: _asyncCache.results[key][4],
})) {
delete _asyncCache.results[key];
if (respectCache) {
const cached = getCachedResult(key, _asyncCache, args, _stores, EPostActionContext.RUN_HIT_CACHE, true, true, false);
if (cached) {
return cached[2];
}
else {
if (_asyncCache.results[key][1]) {
runPostActionHook(_asyncCache.results[key][2], args, _stores, EPostActionContext.RUN_HIT_CACHE);
return _asyncCache.results[key][2];
}
else {
return _asyncCache.results[key][2];
}
}
}

@@ -668,26 +736,4 @@ if (!ignoreShortCircuit && shortCircuitHook !== undefined) {

let currentActionOrd = actionOrdUpdate(_asyncCache, key);
try {
const result = await action(args, _stores);
if (currentActionOrd === _asyncCache.actionOrd[key]) {
_asyncCache.results[key] = [true, true, result, false, Date.now()];
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN);
notifyListeners(key);
}
return result;
}
catch (e) {
console.error(e);
const result = {
error: true,
message: e.message,
tags: [exports.EAsyncEndTags.THREW_ERROR],
payload: null,
};
if (currentActionOrd === _asyncCache.actionOrd[key]) {
_asyncCache.results[key] = [true, true, result, false, Date.now()];
runPostActionHook(result, args, _stores, EPostActionContext.DIRECT_RUN);
notifyListeners(key);
}
return result;
}
_asyncCache.actions[key] = createInternalAction(key, _asyncCache, args, _stores, currentActionOrd, true, EPostActionContext.DIRECT_RUN);
return _asyncCache.actions[key]();
};

@@ -716,5 +762,3 @@ const clearCache = (args = {}) => {

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()];

@@ -731,5 +775,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) {

@@ -758,5 +800,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)) {

@@ -825,2 +865,3 @@ if (checkCacheBreak && cacheBreakHook !== undefined) {

return {
read,
useBeckon,

@@ -827,0 +868,0 @@ useWatch,

{
"name": "pullstate",
"version": "1.7.3",
"version": "1.8.0-dev-20200113-1809",
"description": "Simple state stores using immer and React hooks",

@@ -44,4 +44,4 @@ "main": "dist/index.js",

"@types/lodash": "^4.14.121",
"@types/react": "^16.8.4",
"@types/react-dom": "^16.8.2",
"@types/react": "^16.9.17",
"@types/react-dom": "^16.9.4",
"benchmark": "^2.1.4",

@@ -71,4 +71,4 @@ "cross-env": "^5.2.0",

"immer": "^3.2.0",
"react": "^16.8.2"
"react": "^16.12.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc