Skip to content

[lexical] Feature: add onWarn editor hook and route cascade guard through it - #8653

Closed
potatowagon wants to merge 2 commits into
mainfrom
add-editor-onwarn-hook
Closed

potatowagon wants to merge 2 commits into
mainfrom
add-editor-onwarn-hook

Conversation

@potatowagon

Copy link
Copy Markdown
Contributor

Description

Add an optional onWarn hook to CreateEditorArgs that mirrors onError but at warning severity. The hook defaults to console.warn, preserving existing OSS behavior.

The update-recursion guard in $triggerEnqueuedUpdates (cascade count > 99) now routes through editor._onWarn in production instead of a raw console.warn via devInvariant. In development (__DEV__), it still throws loudly via devInvariant for immediate developer feedback.

This allows embedders to wire onWarn to their own telemetry systems (e.g. structured logging) without the warning being lost to the browser console. It complements #8631 which downgraded this from an error to a warning — this PR ensures the warning remains capturable.

Why a separate hook rather than reusing onError

#8631 deliberately moved this off _onError because routing a recovered condition through the error handler (typically console.error) reported it as an uncaught error, and prior history (#8596) showed routing through onError could freeze the editor. A dedicated onWarn keeps the severity separation clean.

Changes

  1. LexicalEditor.ts: Add onWarn?: ErrorHandler to CreateEditorArgs; add _onWarn: ErrorHandler field on LexicalEditor class, defaulting to console.warn.
  2. LexicalUpdates.ts: In the _cascadeCount > 99 branch of $triggerEnqueuedUpdates, call editor._onWarn(new Error(...)) in production; retain devInvariant throw in __DEV__.

Closes: N/A (feature request from internal sync)

Test plan

Automated

  • TypeScript compiles cleanly (tsc --noEmit passes)
  • Existing test suite unaffected (hook is additive, default behavior unchanged)

Manual verification

  • Without onWarn: behavior identical to before (cascade guard logs to console.warn)
  • With onWarn: (err) => myLogger.warn(err): embedder receives the Error object for structured telemetry

Add an optional `onWarn` hook to CreateEditorArgs that mirrors `onError`
but at warning severity. The hook defaults to `console.warn`, preserving
existing OSS behavior.

The update-recursion guard in `$triggerEnqueuedUpdates` (cascade count > 99)
now routes through `editor._onWarn` in production instead of a raw
`console.warn` via `devInvariant`. In development (__DEV__), it still
throws loudly via `devInvariant` for immediate developer feedback.

This allows embedders to wire `onWarn` to their own telemetry systems
(e.g. structured logging) without the warning being lost to the browser
console. It complements #8631 which downgraded this from an error to a
warning — this PR ensures the warning remains capturable.

Why a separate hook rather than reusing `onError`: #8631 deliberately
moved this off `_onError` because routing a recovered condition through
the error handler (typically console.error) reported it as an uncaught
error and prior history (#8596) showed it could freeze the editor.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 8, 2026
@vercel

vercel Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Jun 8, 2026 2:23pm
lexical-playground Ready Ready Preview, Comment Jun 8, 2026 2:23pm

Request Review

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not #8644?

@potatowagon

Copy link
Copy Markdown
Contributor Author

You're right — #8644 already covers this. Closing as duplicate. Sorry for the noise!

@potatowagon potatowagon closed this Jun 8, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — cfbc6c30 Deployed Jun 8, 2026 by vercel[bot]
Preview – lexical-playground — cfbc6c30 Deployed Jun 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants