diff --git a/benchmark/react/cases/004-various-update/index.tsx b/benchmark/react/cases/004-various-update/index.tsx index e828593dc5..3d48867849 100644 --- a/benchmark/react/cases/004-various-update/index.tsx +++ b/benchmark/react/cases/004-various-update/index.tsx @@ -3,12 +3,15 @@ // LICENSE file in the root directory of this source tree. import { root, useEffect, useState } from '@lynx-js/react'; -import { SnapshotInstance } from '@lynx-js/react/internal'; +import { + BackgroundSnapshotInstance, + SnapshotInstance, +} from '@lynx-js/react/internal'; import type { CSSProperties, MainThread, NodesRef } from '@lynx-js/types'; import { hook, isMainThread } from '../../src/hook.js'; -if (__MAIN_THREAD__) { +if (typeof Codspeed !== 'undefined' && __MAIN_THREAD__) { hook( SnapshotInstance.prototype, 'setAttribute', @@ -84,6 +87,31 @@ if (__MAIN_THREAD__) { ); } +if (typeof Codspeed !== 'undefined' && __BACKGROUND__) { + hook( + BackgroundSnapshotInstance.prototype, + 'setAttribute', + function(this: BackgroundSnapshotInstance, old, key, value) { + const values = value as unknown[]; + if ( + key === 'values' && values[values.length - 1] === 'stop-benchmark-true' + ) { + // we only care about the update that stops the benchmark + + Codspeed.startBenchmark(); + const ret = old!.call(this, key, value); + Codspeed.stopBenchmark(); + Codspeed.setExecutedBenchmark( + `${__REPO_FILEPATH__}::${__webpack_chunkname__}-setAttribute__BatchedValues`, + ); + return ret; + } + + return old!.call(this, key, value); + }, + ); +} + function F() { const [stopBenchmark, setStopBenchmark] = useState(false); const [values, setValues] = useState( diff --git a/benchmark/react/src/hook.ts b/benchmark/react/src/hook.ts index 5254c17d3e..e674936fa3 100644 --- a/benchmark/react/src/hook.ts +++ b/benchmark/react/src/hook.ts @@ -17,6 +17,4 @@ export function hook( export const PREFIX = __REPO_FILEPATH__.split('/').slice(0, -2).join('/'); -export const isMainThread = - // @ts-expect-error safely check - typeof __CreatePage === 'function'; +export const isMainThread = typeof __CreatePage === 'function'; diff --git a/packages/react/runtime/lazy/internal.js b/packages/react/runtime/lazy/internal.js index f26c184d94..613ccf4863 100644 --- a/packages/react/runtime/lazy/internal.js +++ b/packages/react/runtime/lazy/internal.js @@ -5,6 +5,7 @@ import { sExportsReactInternal, target } from './target.js'; export const { + BackgroundSnapshotInstance, Component, __ComponentIsPolyfill, __DynamicPartChildren, diff --git a/packages/react/runtime/src/internal.ts b/packages/react/runtime/src/internal.ts index c0fb3f875c..5f3140f461 100644 --- a/packages/react/runtime/src/internal.ts +++ b/packages/react/runtime/src/internal.ts @@ -7,6 +7,7 @@ import type { FC } from 'react'; import './lynx.js'; +import { BackgroundSnapshotInstance } from './backgroundSnapshot.js'; import { factory as factory2 } from './compat/componentIs.js'; import { useMemo } from './hooks/react.js'; import { loadLazyBundle } from './lynx/lazy-bundle.js'; @@ -17,7 +18,7 @@ import { SnapshotInstance, __page, __pageId, createSnapshot, snapshotManager } f export { __page, __pageId, __root }; -export { SnapshotInstance, snapshotCreateList, createSnapshot, snapshotManager }; +export { BackgroundSnapshotInstance, SnapshotInstance, snapshotCreateList, createSnapshot, snapshotManager }; export const __DynamicPartSlot: DynamicPartType = DynamicPartType.Slot; export const __DynamicPartMultiChildren: DynamicPartType = DynamicPartType.MultiChildren;