Created
November 5, 2025 08:33
-
-
Save aidenybai/6548f095f1a6a0c5a02d32b2e2e83ce0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { detectReactBuildType, getRDTHook, type ReactRenderer } from "bippy"; | |
| const devRenderers: ReactRenderer[] = []; | |
| const originalScheduleRefreshFns: ReactRenderer["scheduleRefresh"][] = []; | |
| const originalScheduleUpdateFns: ReactRenderer["scheduleUpdate"][] = []; | |
| const originalScheduleRootFns: ReactRenderer["scheduleRoot"][] = []; | |
| let isHMRDisabled = false; | |
| interface QueuedUpdate { | |
| args: any[]; | |
| functionName: "scheduleRefresh" | "scheduleUpdate" | "scheduleRoot"; | |
| rendererIndex: number; | |
| } | |
| const queuedUpdates: QueuedUpdate[] = []; | |
| export const disableHMR = () => { | |
| const rdtHook = getRDTHook(); | |
| for (const renderer of rdtHook.renderers.values()) { | |
| if (detectReactBuildType(renderer) === "development") { | |
| devRenderers.push(renderer); | |
| } | |
| } | |
| if (devRenderers.length === 0) return; | |
| if (isHMRDisabled) return; | |
| for (const renderer of devRenderers) { | |
| originalScheduleRefreshFns.push(renderer.scheduleRefresh); | |
| originalScheduleUpdateFns.push(renderer.scheduleUpdate); | |
| originalScheduleRootFns.push(renderer.scheduleRoot); | |
| } | |
| for (let i = 0; i < devRenderers.length; i++) { | |
| const renderer = devRenderers[i]; | |
| const createQueuedUpdateFn = | |
| (functionName: "scheduleRefresh" | "scheduleUpdate" | "scheduleRoot") => | |
| (...args: any[]) => { | |
| queuedUpdates.push({ args, functionName, rendererIndex: i }); | |
| }; | |
| renderer.scheduleRefresh = createQueuedUpdateFn("scheduleRefresh"); | |
| renderer.scheduleUpdate = createQueuedUpdateFn("scheduleUpdate"); | |
| renderer.scheduleRoot = createQueuedUpdateFn("scheduleRoot"); | |
| } | |
| isHMRDisabled = true; | |
| }; | |
| export const enableHMR = () => { | |
| if (devRenderers.length === 0) return; | |
| if (!isHMRDisabled) return; | |
| for (let i = 0; i < devRenderers.length; i++) { | |
| const renderer = devRenderers[i]; | |
| renderer.scheduleRefresh = originalScheduleRefreshFns[i]; | |
| renderer.scheduleUpdate = originalScheduleUpdateFns[i]; | |
| renderer.scheduleRoot = originalScheduleRootFns[i]; | |
| } | |
| isHMRDisabled = false; | |
| flushQueuedUpdates(); | |
| }; | |
| export const flushQueuedUpdates = () => { | |
| for (const { args, functionName, rendererIndex } of queuedUpdates) { | |
| if (functionName === "scheduleRefresh") { | |
| originalScheduleRefreshFns[rendererIndex]?.apply( | |
| devRenderers[rendererIndex], | |
| args as any, | |
| ); | |
| } | |
| if (functionName === "scheduleUpdate") { | |
| originalScheduleUpdateFns[rendererIndex]?.apply( | |
| devRenderers[rendererIndex], | |
| args as any, | |
| ); | |
| } | |
| if (functionName === "scheduleRoot") { | |
| originalScheduleRootFns[rendererIndex]?.apply( | |
| devRenderers[rendererIndex], | |
| args as any, | |
| ); | |
| } | |
| } | |
| queuedUpdates.length = 0; | |
| }; | |
| export const getQueuedUpdates = () => { | |
| return queuedUpdates; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment