use-memo-one
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -24,18 +24,12 @@ 'use strict'; | ||
})[0]; | ||
var uncommitted = react.useRef(initial); | ||
var committed = react.useRef(initial); | ||
react.useEffect(function () { | ||
committed.current = uncommitted.current; | ||
}); | ||
var isInputMatch = Boolean(inputs && committed.current.inputs && areInputsEqual(inputs, committed.current.inputs)); | ||
if (isInputMatch) { | ||
return committed.current.result; | ||
} | ||
uncommitted.current = { | ||
var cache = isInputMatch ? committed.current : { | ||
inputs: inputs, | ||
result: getResult() | ||
}; | ||
return uncommitted.current.result; | ||
react.useEffect(function () { | ||
committed.current = cache; | ||
}, [cache]); | ||
return cache.result; | ||
} | ||
@@ -42,0 +36,0 @@ function useCallbackOne(callback, inputs) { |
@@ -20,18 +20,12 @@ import { useState, useRef, useEffect } from 'react'; | ||
})[0]; | ||
var uncommitted = useRef(initial); | ||
var committed = useRef(initial); | ||
useEffect(function () { | ||
committed.current = uncommitted.current; | ||
}); | ||
var isInputMatch = Boolean(inputs && committed.current.inputs && areInputsEqual(inputs, committed.current.inputs)); | ||
if (isInputMatch) { | ||
return committed.current.result; | ||
} | ||
uncommitted.current = { | ||
var cache = isInputMatch ? committed.current : { | ||
inputs: inputs, | ||
result: getResult() | ||
}; | ||
return uncommitted.current.result; | ||
useEffect(function () { | ||
committed.current = cache; | ||
}, [cache]); | ||
return cache.result; | ||
} | ||
@@ -38,0 +32,0 @@ function useCallbackOne(callback, inputs) { |
@@ -24,18 +24,12 @@ (function (global, factory) { | ||
})[0]; | ||
var uncommitted = react.useRef(initial); | ||
var committed = react.useRef(initial); | ||
react.useEffect(function () { | ||
committed.current = uncommitted.current; | ||
}); | ||
var isInputMatch = Boolean(inputs && committed.current.inputs && areInputsEqual(inputs, committed.current.inputs)); | ||
if (isInputMatch) { | ||
return committed.current.result; | ||
} | ||
uncommitted.current = { | ||
var cache = isInputMatch ? committed.current : { | ||
inputs: inputs, | ||
result: getResult() | ||
}; | ||
return uncommitted.current.result; | ||
react.useEffect(function () { | ||
committed.current = cache; | ||
}, [cache]); | ||
return cache.result; | ||
} | ||
@@ -42,0 +36,0 @@ function useCallbackOne(callback, inputs) { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).useMemoOne={},e.React)}(this,function(e,c){"use strict";var o=function(e,t){return e===t};function n(e,t){var n=c.useState(function(){return{inputs:t,result:e()}})[0],u=c.useRef(n),r=c.useRef(n);return c.useEffect(function(){r.current=u.current}),Boolean(t&&r.current.inputs&&function(e,n){return e.length===n.length&&e.every(function(e,t){return o(e,n[t])})}(t,r.current.inputs))?r.current.result:(u.current={inputs:t,result:e()},u.current.result)}e.useCallbackOne=function(e,t){return n(function(){return e},t)},e.useMemoOne=n,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e=e||self).useMemoOne={},e.React)}(this,function(e,o){"use strict";var c=function(e,n){return e===n};function t(e,n){var t=o.useState(function(){return{inputs:n,result:e()}})[0],u=o.useRef(t),r=Boolean(n&&u.current.inputs&&function(e,t){return e.length===t.length&&e.every(function(e,n){return c(e,t[n])})}(n,u.current.inputs))?u.current:{inputs:n,result:e()};return o.useEffect(function(){u.current=r},[r]),r.result}e.useCallbackOne=function(e,n){return t(function(){return e},n)},e.useMemoOne=t,Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "use-memo-one", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "useMemo and useCallback, but without cache invalidation", | ||
@@ -5,0 +5,0 @@ "main": "dist/use-memo-one.cjs.js", |
@@ -17,3 +17,3 @@ # useMemoOne | ||
`useMemoOne` and `useCallbackOne` are `concurrent mode` friendly alternatives to `useMemo` and `useCallback` **that do provide semantic guarantee**. What this means is that you will always get the same reference for a memoized so long as there is no input change. | ||
`useMemoOne` and `useCallbackOne` are `concurrent mode` safe alternatives to `useMemo` and `useCallback` **that do provide semantic guarantee**. What this means is that you will always get the same reference for a memoized so long as there is no input change. | ||
@@ -41,2 +41,6 @@ ## Install | ||
} | ||
``` | ||
``` | ||
## API | ||
See [`useMemo`](https://reactjs.org/docs/hooks-reference.html#usememo) and [`useCallback`](https://reactjs.org/docs/hooks-reference.html#usecallback) |
@@ -5,3 +5,3 @@ // @flow | ||
type Result<T> = {| | ||
type Cache<T> = {| | ||
inputs: ?(mixed[]), | ||
@@ -18,3 +18,3 @@ result: T, | ||
// using useState to generate initial value as it is lazy | ||
const initial: Result<T> = useState(() => ({ | ||
const initial: Cache<T> = useState(() => ({ | ||
inputs, | ||
@@ -24,9 +24,5 @@ result: getResult(), | ||
const uncommitted = useRef<Result<T>>(initial); | ||
const committed = useRef<Result<T>>(initial); | ||
const committed = useRef<Cache<T>>(initial); | ||
// persist any uncommitted changes after they have been committed | ||
useEffect(() => { | ||
committed.current = uncommitted.current; | ||
}); | ||
@@ -39,12 +35,16 @@ const isInputMatch: boolean = Boolean( | ||
if (isInputMatch) { | ||
return committed.current.result; | ||
} | ||
// create a new cache if required | ||
const cache: Cache<T> = isInputMatch | ||
? committed.current | ||
: { | ||
inputs, | ||
result: getResult(), | ||
}; | ||
uncommitted.current = { | ||
inputs, | ||
result: getResult(), | ||
}; | ||
// commit the cache | ||
useEffect(() => { | ||
committed.current = cache; | ||
}, [cache]); | ||
return uncommitted.current.result; | ||
return cache.result; | ||
} | ||
@@ -51,0 +51,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44
11592
173