Skip to content

refactor(client): replace useReactiveValue with useSyncExternalStore in providers#40446

Merged
tassoevan merged 2 commits into
refactor/auth-provider-login-token-helperfrom
refactor/sdk-status-loggingin-providers
May 13, 2026
Merged

refactor(client): replace useReactiveValue with useSyncExternalStore in providers#40446
tassoevan merged 2 commits into
refactor/auth-provider-login-token-helperfrom
refactor/sdk-status-loggingin-providers

Conversation

@ggazzo
Copy link
Copy Markdown
Member

@ggazzo ggazzo commented May 7, 2026

Stacked on top of #40445 (still in draft).

Summary

`useReactiveValue` had only 2 consumers — `ServerProvider` and `AuthenticationProvider` — both wired through `useReactiveValue → createReactiveSubscriptionFactory → Tracker.autorun`. Both now use `useSyncExternalStore` directly with bespoke subscribe/getSnapshot pairs, eliminating Tracker from these paths.

  • `ServerProvider`: subscribe directly on `sdk.connection.on('connection', …)`. Works in both transport modes since the previous PR's bridge made the Meteor pass-through emit `'connection'` from `Meteor.connection._stream` events. Snapshot is cached with a shallow-equality check so `useSyncExternalStore` only triggers re-renders when `status`/`connected`/`retryCount`/`retryTime` actually change. The standalone `Tracker.Dependency` (`ddpSdkStatusDep`) is gone — the SDK's own emitter is the single source of transition events now.
  • `AuthenticationProvider`: subscribe via a one-time monkey-patch of `Accounts._setLoggingIn` (Meteor's internal flip, same private API already used by `killMeteorStream.ts:49`) to fan out logging-in transitions without entering a Tracker context.
  • `useReactiveValue`: deleted. No remaining consumers.

`createReactiveSubscriptionFactory` stays — `AuthorizationProvider` (permission queries) and `UserProvider` (`queryPreference`) still rely on its Tracker bridge for reading `hasPermission` / `hasRole` / `getUserPreference` reactively. Migrating those is a follow-up that requires changing the underlying read APIs to expose explicit subscribers.

Net Tracker imports

Before this PR: 7 client files. After: 6 (`userAndUsers.ts`, `stubMeteorStream.ts`, `Cursor.ts`, `ObserveHandle.ts`, `watch.ts`, `createReactiveSubscriptionFactory.ts`).

Test plan

  • `tsc --noEmit` clean
  • grep confirms only the 6 expected files still import `meteor/tracker`
  • Manual: `ConnectionStatusBar` reflects connection drops/reconnects (DevTools → Network → Offline) in flag-OFF and flag-ON; reconnect countdown still works (uses internal `setInterval`, not reactive on retryTime)
  • Manual: page reload while logged in shows correct `isLoggingIn` lifecycle (initial `true` during auto-resume, `false` after) — verifying `useStoreCookiesOnLogin` writes `rc_token` and `useUpdateVideoConfUser` updates after resume
  • Manual: explicit login form submit → `isLoggingIn` flips true while in flight, false on completion

Task: ARCH-2138

Summary by CodeRabbit

  • Refactor
    • Updated internal state management patterns in core authentication and connection providers to improve code maintainability and alignment with modern React standards.

@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented May 7, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 7, 2026

⚠️ No Changeset found

Latest commit: 805c1a5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

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

Click here to learn what changesets are, and how to add one.

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Walkthrough

The PR replaces the useReactiveValue hook (Tracker-based) with useSyncExternalStore in ServerProvider and AuthenticationProvider. ServerProvider now subscribes directly to SDK connection events; AuthenticationProvider uses a monkey-patched Accounts._setLoggingIn listener. The useReactiveValue hook is removed as it has no remaining consumers.

Changes

Reactive Store Subscription Modernization

Layer / File(s) Summary
Hook removal and changeset documentation
apps/meteor/client/hooks/useReactiveValue.ts, .changeset/sdk-status-loggingin-providers.md
useReactiveValue hook is deleted. Changeset documents the migration from Tracker-based to useSyncExternalStore pattern across both providers and removal of the ddpSdkStatusDep dependency.
AuthenticationProvider Accounts bridge
apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx
A new internal "logging in" bridge maintains a listener set and installs a one-time wrapper around Accounts._setLoggingIn to notify on flag transitions. isLoggingIn now uses useSyncExternalStore with Accounts.loggingIn() as the snapshot getter.
ServerProvider status subscription and computation
apps/meteor/client/providers/ServerProvider.tsx
ServerProvider replaces Tracker.Dependency-based status bridging with a cached computeStatus() function and explicit subscription management. Direct sdk.connection.on('connection') listeners update observers only when status fields materially change; connected/status/retryCount/retryTime now derive from useSyncExternalStore.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

type: chore

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: refactoring providers to replace useReactiveValue with useSyncExternalStore.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • ARCH-2138: Request failed with status code 401

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

❌ Patch coverage is 82.92683% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.66%. Comparing base (eda14b6) to head (805c1a5).

Additional details and impacted files

Impacted file tree graph

@@                              Coverage Diff                              @@
##           refactor/auth-provider-login-token-helper   #40446      +/-   ##
=============================================================================
- Coverage                                      69.66%   69.66%   -0.01%     
=============================================================================
  Files                                           3318     3317       -1     
  Lines                                         122002   122030      +28     
  Branches                                       21810    21803       -7     
=============================================================================
+ Hits                                           84994    85013      +19     
- Misses                                         33677    33690      +13     
+ Partials                                        3331     3327       -4     
Flag Coverage Δ
unit 70.40% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ggazzo ggazzo force-pushed the refactor/sdk-tracker-easy-autoruns branch from 77c465c to 6bc7cc2 Compare May 7, 2026 18:32
@ggazzo ggazzo force-pushed the refactor/sdk-status-loggingin-providers branch from a8afa77 to 7ad8ed8 Compare May 7, 2026 18:33
ggazzo added a commit that referenced this pull request May 7, 2026
Meteor's socket-stream-client only accepts 'message' / 'reset' / 'disconnect' as event types — `_stream.on('connected', ...)` throws `Error: unknown event type: connected`. The throw propagated up through `connection.on(...)` to `CachedStore.performInitialization`, aborting it before `setupListener()` could open the stream subscription. The error was caught by `init().catch(console.error)` so the cache silently appeared ready while real-time updates from public-settings, subscriptions, etc. never reached the client.

Use 'reset' (fired when a new DDP session is established, effectively the post-reconnect signal) plus 'disconnect' instead. The bridge's `safeMeteorStatus()` readback covers determining the actual state for emitting `connected`/`disconnected`/`connection`.

Caught by deterministic E2E failures across PR #40445 / #40446: homepage custom-body, omnichannel department removal toggle, system-messages, account-security 2FA — all tests that change a setting via REST and expect the client UI to reflect it through the public-settings stream.
@ggazzo ggazzo force-pushed the refactor/sdk-status-loggingin-providers branch from 7ad8ed8 to 7a07715 Compare May 7, 2026 21:18
ggazzo added a commit that referenced this pull request May 8, 2026
Meteor's socket-stream-client only accepts 'message' / 'reset' / 'disconnect' as event types — `_stream.on('connected', ...)` throws `Error: unknown event type: connected`. The throw propagated up through `connection.on(...)` to `CachedStore.performInitialization`, aborting it before `setupListener()` could open the stream subscription. The error was caught by `init().catch(console.error)` so the cache silently appeared ready while real-time updates from public-settings, subscriptions, etc. never reached the client.

Use 'reset' (fired when a new DDP session is established, effectively the post-reconnect signal) plus 'disconnect' instead. The bridge's `safeMeteorStatus()` readback covers determining the actual state for emitting `connected`/`disconnected`/`connection`.

Caught by deterministic E2E failures across PR #40445 / #40446: homepage custom-body, omnichannel department removal toggle, system-messages, account-security 2FA — all tests that change a setting via REST and expect the client UI to reflect it through the public-settings stream.
@ggazzo ggazzo force-pushed the refactor/sdk-tracker-easy-autoruns branch from f8d46b0 to 8833762 Compare May 8, 2026 22:30
@ggazzo ggazzo force-pushed the refactor/sdk-status-loggingin-providers branch from 7a07715 to b93009a Compare May 8, 2026 22:30
@ggazzo ggazzo added this to the 8.5.0 milestone May 11, 2026
@ggazzo
Copy link
Copy Markdown
Member Author

ggazzo commented May 11, 2026

/jira ARCH-2116

ggazzo added a commit that referenced this pull request May 11, 2026
Meteor's socket-stream-client only accepts 'message' / 'reset' / 'disconnect' as event types — `_stream.on('connected', ...)` throws `Error: unknown event type: connected`. The throw propagated up through `connection.on(...)` to `CachedStore.performInitialization`, aborting it before `setupListener()` could open the stream subscription. The error was caught by `init().catch(console.error)` so the cache silently appeared ready while real-time updates from public-settings, subscriptions, etc. never reached the client.

Use 'reset' (fired when a new DDP session is established, effectively the post-reconnect signal) plus 'disconnect' instead. The bridge's `safeMeteorStatus()` readback covers determining the actual state for emitting `connected`/`disconnected`/`connection`.

Caught by deterministic E2E failures across PR #40445 / #40446: homepage custom-body, omnichannel department removal toggle, system-messages, account-security 2FA — all tests that change a setting via REST and expect the client UI to reflect it through the public-settings stream.
@ggazzo ggazzo force-pushed the refactor/sdk-tracker-easy-autoruns branch from b0b9796 to a8d5dc3 Compare May 11, 2026 22:53
…in providers

ServerProvider and AuthenticationProvider were the only callers of useReactiveValue. Both now wire their context values through useSyncExternalStore + an explicit subscribe/getSnapshot pair, dropping the Tracker.autorun that lived inside createReactiveSubscriptionFactory:

- ServerProvider listens on sdk.connection.on('connection', …) — works in both transport modes since the prior PR bridged Meteor's _stream events into that emitter. The standalone Tracker.Dependency (ddpSdkStatusDep) is gone; the SDK's own emitter is now the single source of transition events. Snapshot caching with shallow-compare keeps useSyncExternalStore identity-stable.

- AuthenticationProvider monkey-patches Accounts._setLoggingIn (Meteor's internal flip, also used in killMeteorStream.ts) to fan out logging-in transitions without entering a Tracker computation. The patch is installed on first subscribe.

useReactiveValue had no remaining consumers and is removed. createReactiveSubscriptionFactory stays — AuthorizationProvider's permission queries and UserProvider's queryPreference still rely on its Tracker bridge for reading hasPermission/hasRole/getUserPreference reactively.
@ggazzo ggazzo force-pushed the refactor/sdk-status-loggingin-providers branch from b93009a to e02d28c Compare May 11, 2026 22:58
@ggazzo ggazzo changed the base branch from refactor/sdk-tracker-easy-autoruns to refactor/auth-provider-login-token-helper May 11, 2026 22:58
@ggazzo ggazzo marked this pull request as ready for review May 12, 2026 14:03
@ggazzo ggazzo requested a review from a team as a code owner May 12, 2026 14:03
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 `@apps/meteor/client/providers/ServerProvider.tsx`:
- Around line 136-157: The cachedStatus is computed only at module load and can
be stale until the first subscriber installs the bridge; modify
ensureStatusBridge so after setting statusBridgeStarted = true and before
returning it refreshes cachedStatus by calling computeStatus() (and if you want
to preserve existing behavior also compare with the old cachedStatus and notify
statusListeners if it changed) — update the function ensureStatusBridge to
recompute cachedStatus immediately (using computeStatus) when installing the
getDdpSdk().connection.on('connection', ...) bridge so getStatusSnapshot/readers
see the current state.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 452c3498-d91b-448f-b04b-e0858d03432c

📥 Commits

Reviewing files that changed from the base of the PR and between eda14b6 and e02d28c.

📒 Files selected for processing (4)
  • .changeset/sdk-status-loggingin-providers.md
  • apps/meteor/client/hooks/useReactiveValue.ts
  • apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx
  • apps/meteor/client/providers/ServerProvider.tsx
💤 Files with no reviewable changes (1)
  • apps/meteor/client/hooks/useReactiveValue.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx
  • apps/meteor/client/providers/ServerProvider.tsx
🧠 Learnings (3)
📚 Learning: 2026-03-27T14:52:56.865Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 39892
File: apps/meteor/client/views/room/contextualBar/Threads/Thread.tsx:150-155
Timestamp: 2026-03-27T14:52:56.865Z
Learning: In Rocket.Chat, there are two different `ModalBackdrop` components with different prop APIs. During review, confirm the import source: (1) `rocket.chat/fuselage` `ModalBackdrop` uses `ModalBackdropProps` based on `BoxProps` (so it supports `onClick` and other Box/DOM props) and does not have an `onDismiss` prop; (2) `rocket.chat/ui-client` `ModalBackdrop` uses a narrower props interface like `{ children?: ReactNode; onDismiss?: () => void }` and handles Escape keypress and outside mouse-up, and it does not forward arbitrary DOM props such as `onClick`. Flag mismatched props (e.g., `onDismiss` passed to the fuselage component or `onClick` passed to the ui-client component) and ensure the usage matches the correct component being imported.

Applied to files:

  • apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx
  • apps/meteor/client/providers/ServerProvider.tsx
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx
  • apps/meteor/client/providers/ServerProvider.tsx
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.

Applied to files:

  • .changeset/sdk-status-loggingin-providers.md
🔇 Additional comments (3)
apps/meteor/client/providers/AuthenticationProvider/AuthenticationProvider.tsx (1)

7-7: LGTM!

Also applies to: 31-58, 68-68

apps/meteor/client/providers/ServerProvider.tsx (1)

15-15: LGTM!

Also applies to: 164-164

.changeset/sdk-status-loggingin-providers.md (1)

1-5: LGTM!

Comment on lines +136 to +157
let cachedStatus: CombinedStatus = computeStatus();
const statusListeners = new Set<() => void>();
let statusBridgeStarted = false;

const ensureStatusBridge = (): void => {
if (statusBridgeStarted) return;
statusBridgeStarted = true;
getDdpSdk().connection.on('connection', () => {
const next = computeStatus();
if (isStatusEqual(cachedStatus, next)) return;
cachedStatus = next;
statusListeners.forEach((cb) => cb());
});
};

const subscribeStatus = (cb: () => void): (() => void) => {
ensureStatusBridge();
statusListeners.add(cb);
return () => {
statusListeners.delete(cb);
};
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Stale cachedStatus between module load and first subscribe can leave the status bar wedged.

cachedStatus is computed once at module-evaluation time (Line 136), and the SDK 'connection' listener is only installed when the first component subscribes (ensureStatusBridge, Lines 140-149). Any 'connection' transition that fires in the window between module load and the first subscribe is dropped — no listener yet, and cachedStatus is never refreshed. getStatusSnapshot then keeps handing React the stale snapshot, and because useSyncExternalStore's post-subscribe re-check also calls getStatusSnapshot (still stale), no re-render is scheduled. On a stable connection that produces no further 'connection' emits, the ConnectionStatusBar can stay stuck on the initial value (e.g. connecting). The previous Tracker.autorun path re-read on every invalidation, so this is a new regression.

Refresh the cache when the bridge is installed so the first subscriber sees current state:

🛠️ Proposed fix
 const ensureStatusBridge = (): void => {
 	if (statusBridgeStarted) return;
 	statusBridgeStarted = true;
 	getDdpSdk().connection.on('connection', () => {
 		const next = computeStatus();
 		if (isStatusEqual(cachedStatus, next)) return;
 		cachedStatus = next;
 		statusListeners.forEach((cb) => cb());
 	});
+	cachedStatus = computeStatus();
 };
🤖 Prompt for 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.

In `@apps/meteor/client/providers/ServerProvider.tsx` around lines 136 - 157, The
cachedStatus is computed only at module load and can be stale until the first
subscriber installs the bridge; modify ensureStatusBridge so after setting
statusBridgeStarted = true and before returning it refreshes cachedStatus by
calling computeStatus() (and if you want to preserve existing behavior also
compare with the old cachedStatus and notify statusListeners if it changed) —
update the function ensureStatusBridge to recompute cachedStatus immediately
(using computeStatus) when installing the
getDdpSdk().connection.on('connection', ...) bridge so getStatusSnapshot/readers
see the current state.

@tassoevan tassoevan merged commit addc58e into refactor/auth-provider-login-token-helper May 13, 2026
25 checks passed
@tassoevan tassoevan deleted the refactor/sdk-status-loggingin-providers branch May 13, 2026 19:11
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