Skip to content

Commit 54743ba

Browse files
committed
refactor(dependency): remove use-memo-one
Remove use-memo-one from the package.json and include parts of its source code directly in our source code.
1 parent 8e289cd commit 54743ba

File tree

24 files changed

+352
-41
lines changed

24 files changed

+352
-41
lines changed

.eslintrc.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,7 @@ module.exports = {
115115
name: 'react',
116116
importNames: ['useMemo', 'useCallback'],
117117
message:
118-
'`useMemo` and `useCallback` are subject to cache busting. Please use `useMemoOne`',
119-
},
120-
// Forcing use aliased imports from useMemoOne
121-
{
122-
name: 'use-memo-one',
123-
importNames: ['useMemoOne', 'useCallbackOne'],
124-
message:
125-
'use-memo-one exports `useMemo` and `useCallback` which work nicer with `eslint-plugin-react-hooks`',
118+
'`useMemo` and `useCallback` are subject to cache busting. Please use methods from `src/use-memo-one.ts`',
126119
},
127120
// Disabling using of useLayoutEffect from react
128121
{

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@
7575
"css-box-model": "^1.2.1",
7676
"raf-schd": "^4.0.3",
7777
"react-redux": "^9.1.2",
78-
"redux": "^5.0.1",
79-
"use-memo-one": "^1.1.3"
78+
"redux": "^5.0.1"
8079
},
8180
"devDependencies": {
8281
"@atlaskit/css-reset": "6.11.2",

pnpm-lock.yaml

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/state/registry/use-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect } from 'react';
2-
import { useMemo } from 'use-memo-one';
2+
import { useMemo } from '../../use-memo-one';
33
import type { Registry } from './registry-types';
44
import createRegistry from './create-registry';
55

src/use-memo-one.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Original author: Alex Reardon
3+
* License: MIT
4+
* Repo: https://github.com/alexreardon/use-memo-one
5+
* Description: useMemo and useCallback but with a stable cache.
6+
*/
7+
8+
import { useRef, useState, useEffect } from 'react';
9+
import areInputsEqual from './are-inputs-equal';
10+
11+
interface Cache<T> {
12+
inputs?: unknown[];
13+
result: T;
14+
}
15+
16+
export function useMemo<T>(
17+
// getResult changes on every call,
18+
getResult: () => T,
19+
// the inputs array changes on every call
20+
inputs?: unknown[],
21+
): T {
22+
// using useState to generate initial value as it is lazy
23+
const initial: Cache<T> = useState(() => ({
24+
inputs,
25+
result: getResult(),
26+
}))[0];
27+
const isFirstRun = useRef<boolean>(true);
28+
const committed = useRef<Cache<T>>(initial);
29+
30+
// persist any uncommitted changes after they have been committed
31+
const useCache: boolean =
32+
isFirstRun.current ||
33+
Boolean(
34+
inputs &&
35+
committed.current.inputs &&
36+
areInputsEqual(inputs, committed.current.inputs),
37+
);
38+
39+
// create a new cache if required
40+
// eslint-disable-next-line react-hooks/exhaustive-deps
41+
const cache: Cache<T> = useCache
42+
? committed.current
43+
: {
44+
inputs,
45+
result: getResult(),
46+
};
47+
48+
// commit the cache
49+
useEffect(() => {
50+
isFirstRun.current = false;
51+
committed.current = cache;
52+
}, [cache]);
53+
54+
return cache.result;
55+
}
56+
57+
// eslint-disable-next-line @typescript-eslint/ban-types
58+
export function useCallback<T extends Function>(
59+
// getResult changes on every call,
60+
callback: T,
61+
// the inputs array changes on every call
62+
inputs?: unknown[],
63+
): T {
64+
// eslint-disable-next-line react-hooks/exhaustive-deps
65+
return useMemo(() => callback, inputs);
66+
}

src/view/drag-drop-context/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { flushSync } from 'react-dom';
33
import type { ReactNode, MutableRefObject } from 'react';
44
import { bindActionCreators, Dispatch } from 'redux';
55
import { Provider } from 'react-redux';
6-
import { useMemo, useCallback } from 'use-memo-one';
6+
import { useMemo, useCallback } from '../../use-memo-one';
77
import { invariant } from '../../invariant';
88
import createStore from '../../state/create-store';
99
import createDimensionMarshal from '../../state/dimension-marshal/dimension-marshal';

src/view/draggable/draggable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useRef, DragEvent, TransitionEvent } from 'react';
22
import { flushSync } from 'react-dom';
3-
import { useMemo, useCallback } from 'use-memo-one';
3+
import { useMemo, useCallback } from '../../use-memo-one';
44
import type { DraggableRubric, DraggableDescriptor } from '../../types';
55
import getStyle from './get-style';
66
import useDraggablePublisher from '../use-draggable-publisher/use-draggable-publisher';

src/view/droppable/droppable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ReactDOM from 'react-dom';
2-
import { useMemo, useCallback } from 'use-memo-one';
32
import React, { useRef, useContext, FunctionComponent } from 'react';
43
import type { ReactNode } from 'react';
4+
import { useMemo, useCallback } from '../../use-memo-one';
55
import { invariant } from '../../invariant';
66
import type { DraggableId } from '../../types';
77
import type { Props, DroppableProvided } from './droppable-types';

src/view/placeholder/placeholder.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useRef, useEffect, FunctionComponent } from 'react';
2-
import { useCallback } from 'use-memo-one';
32
import type { Spacing } from 'css-box-model';
3+
import { useCallback } from '../../use-memo-one';
44
import type {
55
Placeholder as PlaceholderType,
66
InOutAnimationMode,

src/view/use-announcer/use-announcer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useRef, useEffect } from 'react';
2-
import { useMemo, useCallback } from 'use-memo-one';
2+
import { useMemo, useCallback } from '../../use-memo-one';
33
import type { Announce, ContextId } from '../../types';
44
import { warning } from '../../dev-warning';
55
import getBodyElement from '../get-body-element';

0 commit comments

Comments
 (0)