From 14e7c61f398316202773081a517dece4cd7aaf64 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Sun, 7 Jul 2024 22:37:43 +0800 Subject: [PATCH] chore: add es-toolkit --- package.json | 1 + pnpm-lock.yaml | 7 +++++++ scripts/build.mjs | 2 +- src/lib/utils/throttle.ts | 29 ----------------------------- src/metro/caches.ts | 6 +++--- 5 files changed, 12 insertions(+), 33 deletions(-) delete mode 100644 src/lib/utils/throttle.ts diff --git a/package.json b/package.json index 89ab1d6..f586bcb 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "yargs-parser": "^21.1.1" }, "dependencies": { + "es-toolkit": "^1.8.0", "fuzzysort": "^3.0.1", "spitroast": "^1.4.4" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ff9efa..310b834 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + es-toolkit: + specifier: ^1.8.0 + version: 1.8.0 fuzzysort: specifier: ^3.0.1 version: 3.0.1 @@ -1165,6 +1168,10 @@ packages: is-symbol: 1.0.4 dev: true + /es-toolkit@1.8.0: + resolution: {integrity: sha512-ldETiVy9nu3uCHKsALW4pGitzASWivUq79DdMQqdLa2Nx0swhULpvXCqSoJrWYVI69bsgaNRqnFRwpHie8hY+A==} + dev: false + /esbuild@0.20.2: resolution: {integrity: sha512-WdOOppmUNU+IbZ0PaDiTst80zjnrOkyJNHoKupIcVyU8Lvla3Ugx94VzkQ32Ijqd7UhHJy75gNWDMUekcrSJ6g==} engines: {node: '>=12'} diff --git a/scripts/build.mjs b/scripts/build.mjs index 1342e95..0bbfc96 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -66,7 +66,7 @@ const config = { { name: "swc", setup(build) { - build.onLoad({ filter: /\.[jt]sx?$/ }, async args => { + build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async args => { const result = await swc.transformFile(args.path, { jsc: { externalHelpers: true, diff --git a/src/lib/utils/throttle.ts b/src/lib/utils/throttle.ts deleted file mode 100644 index f0fc242..0000000 --- a/src/lib/utils/throttle.ts +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Returns a function that ensures that only one call - * executes every given milliseconds. - * @param funct Function to throttle - * @param time Time to wait before allowing the next call - * @returns Throttled function with the same signature as `funct` - */ -export function throttle any>( - funct: T, - time: number = 200 -): T { - let active = false; - let calledWhileActive = false; - return function (this: ReturnType, ...args: Parameters) { - if (!active) { - active = true; - funct.apply(this, args); - setTimeout(() => { - active = false; - if (calledWhileActive) { - calledWhileActive = false; - funct.apply(this, args); - } - }, time); - } else { - calledWhileActive = true; - } - } as T; -} diff --git a/src/metro/caches.ts b/src/metro/caches.ts index 88a3330..2b55e33 100644 --- a/src/metro/caches.ts +++ b/src/metro/caches.ts @@ -1,5 +1,5 @@ import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules"; -import { throttle } from "@lib/utils/throttle"; +import { debounce } from "es-toolkit"; const CACHE_VERSION = 34; const BUNNY_METRO_CACHE_KEY = "__bunny_metro_cache_key__"; @@ -65,9 +65,9 @@ export async function initMetroCache() { } } -const saveCache = throttle(() => { +const saveCache = debounce(() => { MMKVManager.setItem(BUNNY_METRO_CACHE_KEY, JSON.stringify(_metroCache)); -}); +}, 1000); function extractExportsFlags(moduleExports: any) { if (!moduleExports) return 0;