Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf/memo #3816

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-olives-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-core': patch
---

Use useMemoOnce for decorate, usePlateEditor
5 changes: 5 additions & 0 deletions .changeset/thirty-cats-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/react-utils': patch
---

Add useEffectOnce, useMemoOnce
15 changes: 7 additions & 8 deletions apps/www/content/docs/multi-select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ Unlike traditional input-based multi-selects, this component is built on top of

- Full history support (undo/redo)
- Native cursor navigation between and within tags
- Text selection across multiple tags
- Copy/paste support for tags
- Select one to many tags
- Copy/paste tags
- Drag and drop to reorder tags
- Read-only mode support
- Read-only mode
- Duplicate tags prevention
- Support creating new tags if not in the list
- Automatic search text cleanup
- Automatic whitespace trimming
- Headless components for full styling control
- Fuzzy search with keyboard navigation and [cmdk](https://github.com/pacocoursey/cmdk)
- Create new tags, case insensitive
- Search text cleanup
- Whitespace trimming
- Fuzzy search with [cmdk](https://github.com/pacocoursey/cmdk)

</PackageInfo>

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/react/components/EditorMethodsEffect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';

import { useMemoOnce } from '@udecode/react-utils';

import {
EXPOSED_STORE_KEYS,
useEditorRef,
Expand All @@ -18,7 +20,7 @@ export const EditorMethodsEffect = ({ id }: { id?: string }) => {
EXPOSED_STORE_KEYS.map((key) => [key, plateStore.set[key]()])
) as any;

const memorizedStoreSetters = React.useMemo(
const memorizedStoreSetters = useMemoOnce(
() => storeSetters,
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/react/editor/usePlateEditor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import type React from 'react';

import type { Value } from '@udecode/slate';

import { useMemoOnce } from '@udecode/react-utils';

import type { AnyPluginConfig } from '../../lib';

import {
Expand Down Expand Up @@ -34,7 +36,7 @@ export function usePlateEditor<
: TEnabled extends true | undefined
? TPlateEditor<V, P>
: TPlateEditor<V, P> | null {
return React.useMemo(
return useMemoOnce(
(): any => {
if (options.enabled === false) return null;

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/react/hooks/useEditableProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';

import type { TEditableProps } from '@udecode/slate-react';

import { useMemoOnce } from '@udecode/react-utils';
import clsx from 'clsx';
import omit from 'lodash/omit.js';
import { useDeepCompareMemo } from 'use-deep-compare';
Expand Down Expand Up @@ -31,11 +32,11 @@ export const useEditableProps = ({
const storeRenderLeaf = selectors.renderLeaf();
const storeRenderElement = selectors.renderElement();

const decorateMemo = React.useMemo(() => {
const decorateMemo = useMemoOnce(() => {
return pipeDecorate(editor, storeDecorate ?? editableProps?.decorate);
}, [editableProps?.decorate, editor, storeDecorate]);

const decorate: typeof decorateMemo = React.useMemo(() => {
const decorate: typeof decorateMemo = useMemoOnce(() => {
if (!versionDecorate || !decorateMemo) return;

return (entry) => decorateMemo(entry);
Expand Down
2 changes: 2 additions & 0 deletions packages/react-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export * from './createPrimitiveComponent';
export * from './createPrimitiveElement';
export * from './createSlotComponent';
export * from './useComposedRef';
export * from './useEffectOnce';
export * from './useIsomorphicLayoutEffect';
export * from './useMemoOnce';
export * from './useOnClickOutside';
export * from './useStableMemo';
export * from './withProviders';
Expand Down
20 changes: 20 additions & 0 deletions packages/react-utils/src/useEffectOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

export function useEffectOnce(
effect: React.EffectCallback,
deps: React.DependencyList
) {
const initialized = React.useRef(false);
const prevDepsRef = React.useRef(deps);

React.useEffect(() => {
const depsChanged = deps.some((dep, i) => dep !== prevDepsRef.current[i]);

if (!initialized.current || depsChanged) {
initialized.current = true;
prevDepsRef.current = deps;
effect();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
}
21 changes: 21 additions & 0 deletions packages/react-utils/src/useMemoOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';

export function useMemoOnce<T>(
factory: () => T,
deps: React.DependencyList
): T {
const initialized = React.useRef(false);
const prevDepsRef = React.useRef(deps);
const memoizedValueRef = React.useRef<T>();

if (
!initialized.current ||
deps.some((dep, i) => dep !== prevDepsRef.current[i])
) {
initialized.current = true;
prevDepsRef.current = deps;
memoizedValueRef.current = factory();
}

return memoizedValueRef.current!;
}