feat(lib/react/jsx-runtime): switch to objects for storage
This commit is contained in:
parent
c1f56c7e60
commit
feaf1cd194
3 changed files with 17 additions and 16 deletions
|
|
@ -3,9 +3,9 @@ import type { AfterJSXCallback, BeforeJSXCallback, InsteadJSXCallback } from '.'
|
||||||
|
|
||||||
export const jPatches = new Map<
|
export const jPatches = new Map<
|
||||||
ElementType | keyof JSX.IntrinsicElements,
|
ElementType | keyof JSX.IntrinsicElements,
|
||||||
[
|
{
|
||||||
before?: Set<BeforeJSXCallback<any>>,
|
before?: Set<BeforeJSXCallback<any>>
|
||||||
after?: Set<AfterJSXCallback<any>>,
|
after?: Set<AfterJSXCallback<any>>
|
||||||
instead?: Set<InsteadJSXCallback<any>>,
|
instead?: Set<InsteadJSXCallback<any>>
|
||||||
]
|
}
|
||||||
>()
|
>()
|
||||||
|
|
|
||||||
|
|
@ -38,11 +38,11 @@ export function afterJSX<E extends ElementType>(
|
||||||
) {
|
) {
|
||||||
let patches = jPatches.get(type)
|
let patches = jPatches.get(type)
|
||||||
if (!patches) {
|
if (!patches) {
|
||||||
patches = [undefined, new Set(), undefined]
|
patches = {}
|
||||||
jPatches.set(type, 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<E>)
|
set.add(patch as AfterJSXCallback<E>)
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -65,11 +65,11 @@ export function beforeJSX<E extends ElementType>(
|
||||||
) {
|
) {
|
||||||
let patches = jPatches.get(type)
|
let patches = jPatches.get(type)
|
||||||
if (!patches) {
|
if (!patches) {
|
||||||
patches = [new Set(), undefined, undefined]
|
patches = {}
|
||||||
jPatches.set(type, 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<E>)
|
set.add(patch as BeforeJSXCallback<E>)
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -92,11 +92,11 @@ export function insteadJSX<E extends ElementType>(
|
||||||
) {
|
) {
|
||||||
let patches = jPatches.get(type)
|
let patches = jPatches.get(type)
|
||||||
if (!patches) {
|
if (!patches) {
|
||||||
patches = [undefined, undefined, new Set()]
|
patches = {}
|
||||||
jPatches.set(type, 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<E>)
|
set.add(patch as InsteadJSXCallback<E>)
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -108,6 +108,7 @@ export function insteadJSX<E extends ElementType>(
|
||||||
|
|
||||||
function attemptCleanup(type: ElementType) {
|
function attemptCleanup(type: ElementType) {
|
||||||
const patches = jPatches.get(type)!
|
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)
|
jPatches.delete(type)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ const patch = (
|
||||||
const patches = jPatches.get(type)
|
const patches = jPatches.get(type)
|
||||||
if (!patches) return Reflect.apply(orig, ReactJSXRuntime, args)
|
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)
|
if (before) for (const cb of before) args = cb(args)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue