From 3cae2eca2cb528e008938c521010fe3cef8daef0 Mon Sep 17 00:00:00 2001 From: imnyang Date: Wed, 14 May 2025 10:18:30 +0900 Subject: [PATCH] performance --- bun.lock | 3 +++ package.json | 1 + src/App.tsx | 48 ++++++++++++++++++++----------------- src/components/CopyLink.tsx | 3 ++- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/bun.lock b/bun.lock index 12d8b7e..314d74c 100644 --- a/bun.lock +++ b/bun.lock @@ -15,6 +15,7 @@ "base65536": "^5.0.0", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "lodash.debounce": "^4.0.8", "lucide-react": "^0.510.0", "pako": "^2.1.0", "react": "^19.1.0", @@ -705,6 +706,8 @@ "locate-path": ["locate-path@6.0.0", "", { "dependencies": { "p-locate": "^5.0.0" } }, "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw=="], + "lodash.debounce": ["lodash.debounce@4.0.8", "", {}, "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow=="], + "lodash.merge": ["lodash.merge@4.6.2", "", {}, "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="], "longest-streak": ["longest-streak@3.1.0", "", {}, "sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g=="], diff --git a/package.json b/package.json index 71eb72d..7b5057e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "base65536": "^5.0.0", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "lodash.debounce": "^4.0.8", "lucide-react": "^0.510.0", "pako": "^2.1.0", "react": "^19.1.0", diff --git a/src/App.tsx b/src/App.tsx index 8c6b8ad..7999206 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,6 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect, useCallback } from "react"; import { encode, decode } from "base65536"; +import debounce from "lodash.debounce"; import pako from "pako"; import "@blocknote/core/fonts/inter.css"; @@ -47,30 +48,33 @@ export default function App() { loadContentFromURL(); }, [editor]); - const handleChange = async () => { - if (!editor) return; + // useCallback을 사용해 debounce 유지 + const debouncedHandleChange = useCallback( + debounce(async () => { + if (!editor) return; - const newContentMarkdown = await editor.blocksToMarkdownLossy( - editor.topLevelBlocks - ); - setContentForURL(newContentMarkdown); + const newContentMarkdown = await editor.blocksToMarkdownLossy( + editor.topLevelBlocks + ); + setContentForURL(newContentMarkdown); - const encoder = new TextEncoder(); - let encodedString: string; + const encoder = new TextEncoder(); + let encodedString: string; - if (newContentMarkdown.length >= 40) { - const encodedBytes = encoder.encode(newContentMarkdown); - const compressed = pako.gzip(encodedBytes); - encodedString = encode(compressed); // 압축 + 인코딩 - } else { - const plainBytes = encoder.encode(newContentMarkdown); - encodedString = encode(plainBytes); // 압축 없이 인코딩 - } + if (newContentMarkdown.length >= 40) { + const encodedBytes = encoder.encode(newContentMarkdown); + const compressed = pako.gzip(encodedBytes); + encodedString = encode(compressed); + } else { + const plainBytes = encoder.encode(newContentMarkdown); + encodedString = encode(plainBytes); + } + + const newUrl = `${window.location.pathname}?c=${encodedString}`; + window.history.replaceState({}, "", newUrl); + }, 250), [editor] // 500ms 대기 + ); - const newUrl = `${window.location.pathname}?c=${encodedString}`; - window.history.replaceState({}, "", newUrl); - window.dispatchEvent(new Event("url-changed")); - }; return (
@@ -86,7 +90,7 @@ export default function App() {
diff --git a/src/components/CopyLink.tsx b/src/components/CopyLink.tsx index 55bfe8e..fe96af9 100644 --- a/src/components/CopyLink.tsx +++ b/src/components/CopyLink.tsx @@ -45,10 +45,11 @@ export default function CopyLink() { return ( - + Share link