Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f8522d

Browse files
author
Alejandro CR
committed
refactor: rename createFactory to functionFactory and improve memoization logic
1 parent 0bff40e commit 9f8522d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

examples/browsers/libraries/reactjs/maker-fn.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ const { createElement: ce, useState } = React;
55

66
ReactDOMClient.createRoot(document.getElementById('root')).render(ce(App));
77

8-
function createFactory(fn) {
8+
/**
9+
* @param {Function} fn - The function to be memoized.
10+
* @returns {Function} - A memoized version of the function.
11+
*/
12+
function functionFactory(fn) {
913
let fnCache = null;
1014
let depsCache = [];
1115

1216
return (...args) => {
1317
if (
1418
fnCache === null ||
1519
depsCache.length !== args.length ||
16-
args.some((dep, i) => dep !== depsCache[i])
20+
!args.every((dep, i) => Object.is(dep, depsCache[i]))
1721
) {
1822
fnCache = fn.apply(undefined, args);
1923
depsCache = args;
@@ -23,14 +27,14 @@ function createFactory(fn) {
2327
};
2428
}
2529

26-
const OnClickFactory = createFactory(setCount => _event => setCount(prev => prev + 1));
30+
const onClickFactory = functionFactory(setCount => _event => setCount(prev => prev + 1));
2731

2832
const stack = [];
2933

3034
function App() {
3135
const [count, setCount] = useState(0);
3236

33-
const onClick = OnClickFactory(setCount);
37+
const onClick = onClickFactory(setCount);
3438

3539
stack.push(onClick);
3640

0 commit comments

Comments
 (0)