From feaf1cd19473949b7bfbdd2a4e59469f13d7b2d4 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Fri, 18 Jul 2025 23:41:04 +0700 Subject: [PATCH] feat(lib/react/jsx-runtime): switch to objects for storage --- lib/react/src/jsx-runtime/_internal.ts | 10 +++++----- lib/react/src/jsx-runtime/index.ts | 21 +++++++++++---------- lib/react/src/jsx-runtime/patches.ts | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/react/src/jsx-runtime/_internal.ts b/lib/react/src/jsx-runtime/_internal.ts index e3f7f9f..71c9835 100644 --- a/lib/react/src/jsx-runtime/_internal.ts +++ b/lib/react/src/jsx-runtime/_internal.ts @@ -3,9 +3,9 @@ import type { AfterJSXCallback, BeforeJSXCallback, InsteadJSXCallback } from '.' export const jPatches = new Map< ElementType | keyof JSX.IntrinsicElements, - [ - before?: Set>, - after?: Set>, - instead?: Set>, - ] + { + before?: Set> + after?: Set> + instead?: Set> + } >() diff --git a/lib/react/src/jsx-runtime/index.ts b/lib/react/src/jsx-runtime/index.ts index de3f874..0cb7dd2 100644 --- a/lib/react/src/jsx-runtime/index.ts +++ b/lib/react/src/jsx-runtime/index.ts @@ -38,11 +38,11 @@ export function afterJSX( ) { let patches = jPatches.get(type) if (!patches) { - patches = [undefined, new Set(), undefined] + patches = {} jPatches.set(type, patches) - } else if (!patches[1]) patches[1] = new Set() + } - const set = patches[1]! + const set = (patches.after ??= new Set()) set.add(patch as AfterJSXCallback) return () => { @@ -65,11 +65,11 @@ export function beforeJSX( ) { let patches = jPatches.get(type) if (!patches) { - patches = [new Set(), undefined, undefined] + patches = {} jPatches.set(type, patches) - } else if (!patches[0]) patches[0] = new Set() + } - const set = patches[0]! + const set = (patches.before ??= new Set()) set.add(patch as BeforeJSXCallback) return () => { @@ -92,11 +92,11 @@ export function insteadJSX( ) { let patches = jPatches.get(type) if (!patches) { - patches = [undefined, undefined, new Set()] + patches = {} jPatches.set(type, patches) - } else if (!patches[2]) patches[2] = new Set() + } - const set = patches[2]! + const set = (patches.instead ??= new Set()) set.add(patch as InsteadJSXCallback) return () => { @@ -108,6 +108,7 @@ export function insteadJSX( function attemptCleanup(type: ElementType) { const patches = jPatches.get(type)! - if (patches[0]?.size || patches[1]?.size || patches[2]?.size) return + if (patches.before?.size || patches.after?.size || patches.instead?.size) + return jPatches.delete(type) } diff --git a/lib/react/src/jsx-runtime/patches.ts b/lib/react/src/jsx-runtime/patches.ts index 9d3f639..190d4e6 100644 --- a/lib/react/src/jsx-runtime/patches.ts +++ b/lib/react/src/jsx-runtime/patches.ts @@ -14,7 +14,7 @@ const patch = ( const patches = jPatches.get(type) if (!patches) return Reflect.apply(orig, ReactJSXRuntime, args) - const [before, after, instead] = patches + const { before, after, instead } = patches if (before) for (const cb of before) args = cb(args)