Skip to content

refactor(runtime): share initData commit context#2677

Merged
Yradex merged 2 commits into
mainfrom
wt/lynx-stack-20260520-1406
May 20, 2026
Merged

refactor(runtime): share initData commit context#2677
Yradex merged 2 commits into
mainfrom
wt/lynx-stack-20260520-1406

Conversation

@Yradex
Copy link
Copy Markdown
Collaborator

@Yradex Yradex commented May 20, 2026

Summary by CodeRabbit

  • New Features

    • InitData APIs: added InitData provider, consumer, and hooks (useInitData, useInitDataChanged) plus withInitDataInState for class components.
    • useLynxGlobalEventListener made available from the hooks entry.
  • Behavioral Improvements

    • Data-change flushes now trigger update notifications even when only flush options are present.
    • Patch application now skips native patch work unless there are actual ops to apply.
  • Tests

    • Added tests covering commit context and comprehensive init-data/data-change scenarios.

Review Change Stack

Overview

Element Template needs initData APIs without depending on Snapshot-private runtime state. This PR moves the initData factory and shared commit context into core, then wires Snapshot and Element Template through that shared state so data-change flush options can be produced consistently.

Key Points

  • Moves initData support from snapshot/compat into core and re-exports the APIs from both the existing React runtime entry and the Element Template entry.
  • Shares globalCommitContext through core, while keeping ET-only teardown state out of the cross-thread payload.
  • Allows ET to dispatch option-only updates for initData changes, using payloads such as { ops: [], flushOptions: { triggerDataUpdated: true } }.
  • Uses the public @lynx-js/react/hooks entry for ET hooks; existing layer/alias rules resolve it to background or main-thread hook implementations without an ET-only hook facade.

Runtime Contract

  • Background ET commits may send an update event with empty ops when only flushOptions need to reach the main thread.
  • The main-thread ET patch listener skips patch-command application and patch profiling when ops is missing or empty, but still calls __FlushElementTree(__page, flushOptions).
  • Snapshot timing flush options and initData triggerDataUpdated now share the same core commit-context storage, with each runtime consuming/resetting it at its own commit boundary.

Checklist

  • Tests updated.
  • Documentation updated (not required; behavior is covered by runtime tests and existing public docs remain applicable).
  • Changeset added, and when a BREAKING CHANGE occurs, it needs to be clearly marked (empty changeset: no release-facing defaults changed).

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 18662afd-2656-444e-8ba1-e09746fdc6cc

📥 Commits

Reviewing files that changed from the base of the PR and between 290b868 and 0f3c6ce.

📒 Files selected for processing (8)
  • packages/react/runtime/__test__/element-template/runtime/background/commit-hook.test.ts
  • packages/react/runtime/__test__/element-template/vitest.config.ts
  • packages/react/runtime/src/core/hooks/useLynxGlobalEventListener.ts
  • packages/react/runtime/src/element-template/hooks/react.ts
  • packages/react/runtime/src/element-template/index.ts
  • packages/react/runtime/src/element-template/native/index.ts
  • packages/react/runtime/tsconfig.json
  • packages/react/runtime/vitest.config.ts
💤 Files with no reviewable changes (1)
  • packages/react/runtime/src/element-template/hooks/react.ts

📝 Walkthrough

Walkthrough

Consolidates runtime flush-option management into a shared global commit context, migrates initData wiring to core, exposes InitData provider/consumer/hooks from the element-template entrypoint, tightens op handling, updates types for a triggerDataUpdated flush flag, removes test shims, and adds tests and Vitest/TS aliases.

Changes

Element Template InitData Public API with Shared Commit Context

Layer / File(s) Summary
All changes (single checkpoint)
packages/react/runtime/src/core/commit-context.ts, packages/react/runtime/src/core/initData.ts, packages/react/runtime/src/element-template/background/*, packages/react/runtime/src/snapshot/*, packages/react/runtime/src/element-template/index.ts, packages/react/runtime/src/element-template/internal.ts, packages/react/runtime/src/internal.ts, packages/react/runtime/src/lynx-api.ts, packages/react/runtime/src/element-template/protocol/types.ts, packages/react/runtime/src/element-template/native/*, packages/react/runtime/src/core/hooks/*, packages/react/runtime/vitest.config.ts, packages/react/runtime/tsconfig.json, tests/*`
Adds a core globalCommitContext singleton with helpers to take/reset flush options; migrates initData, snapshot, and element-template background modules to use the shared context; exposes InitData Provider/Consumer and hooks from the element-template entry; tightens patch-listener hasOps logic; activates triggerDataUpdated?: boolean in protocol types; removes layered debug shims from test surface via Vitest alias changes; and adds tests and a data-change harness covering flush-only and data-change flows.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes


Possibly related PRs

  • lynx-family/lynx-stack#2441: Related to changes introducing/altering main-thread hook implementations and exports that this PR now imports (@lynx-js/react/hooks) from.
  • lynx-family/lynx-stack#2579: Related to removed-subtree cleanup and background commit-hook behavior that interact with the shared globalCommitContext.nonPayload handling.

Suggested reviewers

  • hzy
  • HuJean
  • colinaaa
  • luhc228

"🐰 I hop through lines and bytes so neat,
I stitch flush options where modules meet,
InitData whispers, events ring true,
Tests clap once, the shared context grew."

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main refactoring: moving initData support and shared commit context from snapshot/compat into core, which is the primary objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch wt/lynx-stack-20260520-1406

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 20, 2026

🦋 Changeset detected

Latest commit: 0f3c6ce

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Yradex Yradex marked this pull request as ready for review May 20, 2026 07:26
@Yradex Yradex requested review from HuJean and hzy as code owners May 20, 2026 07:26
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
5105 1 5104 93
View the top 1 failed test(s) by shortest run time
packages/web-platform/web-rsbuild-server-middleware/test/basic.spec.ts > test/basic.spec.ts
Stack Traces | 10s run time
Error: Hook timed out in 10000ms.
If this is a long-running hook, pass a timeout value as the last argument or configure it globally with "hookTimeout".
 ❯ test/basic.spec.ts:8:1

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/react/runtime/src/element-template/hooks/react.ts`:
- Around line 63-79: The code currently performs GlobalEventEmitter subscription
side effects inside useMemo (previousArgsRef, eventName, listener,
lynx.getJSModule('GlobalEventEmitter')), which is unsafe; move the
addListener/removeListener logic into a useEffect: remove the useMemo block
entirely, create a useEffect that adds the listener
(lynx.getJSModule('GlobalEventEmitter').addListener(eventName, listener)) when
[eventName, listener] change, store the args in previousArgsRef, and return a
cleanup that removes the previous listener via removeListener using
previousArgsRef; keep the existing teardown-only useEffect (or merge it) so no
subscriptions occur during render and all listeners are removed on unmount or
dependency change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 034ebfa3-1e19-4833-a142-fa514585d186

📥 Commits

Reviewing files that changed from the base of the PR and between 3901c96 and 290b868.

📒 Files selected for processing (22)
  • .changeset/empty-et-init-data-core.md
  • packages/react/runtime/__test__/core/commit-context.test.ts
  • packages/react/runtime/__test__/element-template/imports/init-data-api.test.ts
  • packages/react/runtime/__test__/element-template/runtime/background/commit-hook.test.ts
  • packages/react/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx
  • packages/react/runtime/__test__/element-template/test-utils/debug/layeredHooks.ts
  • packages/react/runtime/__test__/element-template/test-utils/debug/layeredReact.ts
  • packages/react/runtime/__test__/element-template/vitest.config.ts
  • packages/react/runtime/__test__/snapshot/commit-context.test.ts
  • packages/react/runtime/src/core/commit-context.ts
  • packages/react/runtime/src/core/initData.ts
  • packages/react/runtime/src/element-template/background/commit-context.ts
  • packages/react/runtime/src/element-template/background/commit-hook.ts
  • packages/react/runtime/src/element-template/hooks/react.ts
  • packages/react/runtime/src/element-template/index.ts
  • packages/react/runtime/src/element-template/internal.ts
  • packages/react/runtime/src/element-template/native/patch-listener.ts
  • packages/react/runtime/src/element-template/protocol/types.ts
  • packages/react/runtime/src/internal.ts
  • packages/react/runtime/src/lynx-api.ts
  • packages/react/runtime/src/snapshot/lifecycle/patch/commit.ts
  • packages/react/runtime/src/snapshot/lynx/component.ts
💤 Files with no reviewable changes (2)
  • packages/react/runtime/test/element-template/test-utils/debug/layeredReact.ts
  • packages/react/runtime/test/element-template/test-utils/debug/layeredHooks.ts

Comment thread packages/react/runtime/src/element-template/hooks/react.ts Outdated
@Yradex Yradex marked this pull request as draft May 20, 2026 07:31
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 20, 2026

Merging this PR will improve performance by 12.03%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 80 untouched benchmarks
⏩ 26 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
transform 1000 view elements 44.8 ms 40 ms +12.03%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wt/lynx-stack-20260520-1406 (0f3c6ce) with main (b42a7dc)

Open in CodSpeed

Footnotes

  1. 26 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@relativeci
Copy link
Copy Markdown

relativeci Bot commented May 20, 2026

React MTF Example

#1632 Bundle Size — 208.74KiB (+0.03%).

0f3c6ce(current) vs b42a7dc main#1627(baseline)

Bundle metrics  Change 4 changes Improvement 1 improvement
                 Current
#1632
     Baseline
#1627
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 46.7% 0%
No change  Chunks 0 0
No change  Assets 3 3
Change  Modules 194(-0.51%) 195
Improvement  Duplicate Modules 77(-1.28%) 78
Change  Duplicate Code 44.19%(-0.16%) 44.26%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#1632
     Baseline
#1627
No change  IMG 111.23KiB 111.23KiB
Regression  Other 97.51KiB (+0.05%) 97.46KiB

Bundle analysis reportBranch wt/lynx-stack-20260520-1406Project dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented May 20, 2026

React Example with Element Template

#768 Bundle Size — 201.17KiB (-1.64%).

0f3c6ce(current) vs b42a7dc main#763(baseline)

Bundle metrics  Change 3 changes Improvement 1 improvement
                 Current
#768
     Baseline
#763
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 28.74% 0%
No change  Chunks 0 0
No change  Assets 4 4
No change  Modules 97 97
Improvement  Duplicate Modules 30(-3.23%) 31
Change  Duplicate Code 39.41%(-0.5%) 39.61%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Improvement 1 improvement
                 Current
#768
     Baseline
#763
No change  IMG 145.76KiB 145.76KiB
Improvement  Other 55.42KiB (-5.72%) 58.78KiB

Bundle analysis reportBranch wt/lynx-stack-20260520-1406Project dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented May 20, 2026

React External

#1614 Bundle Size — 697.99KiB (+0.01%).

0f3c6ce(current) vs b42a7dc main#1609(baseline)

Bundle metrics  Change 1 change
                 Current
#1614
     Baseline
#1609
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 41.22% 0%
No change  Chunks 0 0
No change  Assets 3 3
No change  Modules 17 17
No change  Duplicate Modules 5 5
No change  Duplicate Code 8.59% 8.59%
No change  Packages 0 0
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#1614
     Baseline
#1609
Regression  Other 697.99KiB (+0.01%) 697.9KiB

Bundle analysis reportBranch wt/lynx-stack-20260520-1406Project dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented May 20, 2026

Web Explorer

#10073 Bundle Size — 903.53KiB (0%).

0f3c6ce(current) vs b42a7dc main#10068(baseline)

Bundle metrics  no changes
                 Current
#10073
     Baseline
#10068
No change  Initial JS 45.06KiB 45.06KiB
No change  Initial CSS 2.22KiB 2.22KiB
No change  Cache Invalidation 0% 0%
No change  Chunks 9 9
No change  Assets 11 11
No change  Modules 231 231
No change  Duplicate Modules 11 11
No change  Duplicate Code 27.12% 27.12%
No change  Packages 10 10
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#10073
     Baseline
#10068
No change  JS 499.15KiB 499.15KiB
No change  Other 402.16KiB 402.16KiB
No change  CSS 2.22KiB 2.22KiB

Bundle analysis reportBranch wt/lynx-stack-20260520-1406Project dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented May 20, 2026

React Example

#8499 Bundle Size — 237.79KiB (+0.02%).

0f3c6ce(current) vs b42a7dc main#8494(baseline)

Bundle metrics  Change 4 changes Improvement 1 improvement
                 Current
#8499
     Baseline
#8494
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 38.69% 0%
No change  Chunks 0 0
No change  Assets 4 4
Change  Modules 199(-0.5%) 200
Improvement  Duplicate Modules 80(-1.23%) 81
Change  Duplicate Code 44.69%(-0.16%) 44.76%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#8499
     Baseline
#8494
No change  IMG 145.76KiB 145.76KiB
Regression  Other 92.04KiB (+0.06%) 91.98KiB

Bundle analysis reportBranch wt/lynx-stack-20260520-1406Project dashboard


Generated by RelativeCIDocumentationReport issue

@Yradex Yradex marked this pull request as ready for review May 20, 2026 08:16
@Yradex Yradex merged commit e794bbf into main May 20, 2026
141 of 148 checks passed
@Yradex Yradex deleted the wt/lynx-stack-20260520-1406 branch May 20, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants