@hulanbv/nest-utilities-client-state
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -11,2 +11,3 @@ "use strict"; | ||
let deleteStates = 0; | ||
let distinctStates = 0; | ||
/** | ||
@@ -27,2 +28,5 @@ * Returns a unique method id for POST and DELETE requests. | ||
} | ||
function createDistintId() { | ||
return distinctStates++; | ||
} | ||
/** | ||
@@ -39,13 +43,16 @@ * Use a crud request. | ||
// Extract request options | ||
const { appendQuery, proxyMethod, cache = false } = stateOptions; | ||
// Create a shadow variable for `cache` so its value won't change. | ||
const _cache = react_1.useRef(cache); | ||
// Create a unique id for the given method. | ||
const { appendQuery, proxyMethod, cache = false, distinct = false, } = stateOptions; | ||
// Create a static cache key | ||
const cacheKey = react_1.useRef(cache); | ||
// Create a static method id for the given method. | ||
const methodId = react_1.useRef(createUniqueMethodId(method)); | ||
// Create a static distinct id when state should be distinct. | ||
const distinctId = react_1.useRef(distinct ? createDistintId() : null); | ||
// Create a state hash string. | ||
const stateHash = react_1.useMemo(() => hash_1.hash([ | ||
service.controller, | ||
_cache.current.toString(), | ||
cacheKey.current.toString(), | ||
method, | ||
query || methodId.current, | ||
distinctId.current, | ||
] | ||
@@ -61,3 +68,3 @@ .filter(Boolean) | ||
*/ | ||
const createRequestState = react_1.useCallback(() => new RequestState_1.RequestState(service, method, query, httpOptions, proxyMethod, _cache.current, appendQuery), | ||
const createRequestState = react_1.useCallback(() => new RequestState_1.RequestState(service, method, query, httpOptions, proxyMethod, cacheKey.current, appendQuery), | ||
// We do not need httpOptions as a dependency, because we know `httpOptionsHash` will change every time http options change. | ||
@@ -64,0 +71,0 @@ // eslint-disable-next-line react-hooks/exhaustive-deps |
@@ -30,2 +30,4 @@ import { CrudService, IResponse } from 'nest-utilities-client'; | ||
export interface IStateOptions { | ||
/** Create a distinct state e.g. do *not* use matching existing state */ | ||
distinct?: boolean; | ||
/** Cache with generated or speficic key */ | ||
@@ -32,0 +34,0 @@ cache?: boolean | string; |
{ | ||
"name": "@hulanbv/nest-utilities-client-state", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "React hooks for state management when using nest-utilities-client and a nest-utilities based API.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -475,5 +475,8 @@ # nest-utilities-client-state | ||
| -------------- | ----------------------------------------------- | | ||
| distinct | \<optional> boolean | | ||
| cache | \<optional> string \| boolean | | ||
| immediateFetch | \<optional> boolean | | ||
| proxyMethod | "POST" \| "GET" \| "PUT" \| "PATCH" \| "DELETE" | | ||
| debug | \<optional> boolean | | ||
| appendQuery | \<optional> string | | ||
@@ -480,0 +483,0 @@ ### `enum FetchState` |
Sorry, the diff of this file is not supported yet
80674
50
1038
526