♻️ Refactor startSessionManager to return a Promise instead of using a callback#4438
♻️ Refactor startSessionManager to return a Promise instead of using a callback#4438thomas-lebeau wants to merge 3 commits intov7from
Conversation
… callback - Replace `onReady` callback with async/await pattern, making the API more idiomatic and easier to compose - startSessionManager now returns Promise<SessionManager | undefined> - startSessionManagerStub returns Promise<SessionManager> - Update preStartLogs, preStartRum, and all related tests accordingly
Bundles Sizes Evolution
🚀 CPU Performance
🧠 Memory Performance
|
|
✅ Tests 🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: b5201b8 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e39b36d0a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| expire() | ||
| return | ||
| } | ||
| const initialState = await resolveInitialState() |
There was a problem hiding this comment.
Catch session init rejection before resolving manager
startSessionManager now awaits resolveInitialState() directly, so any rejection from strategy.setSessionState (for example storage write failures or lock acquisition failures) rejects the returned promise. The current callers in preStartLogs/preStartRum only attach .then(...), so this turns into an unhandled promise rejection and prevents SDK startup in those error paths; previously this path was guarded by monitorError instead of surfacing globally.
Useful? React with 👍 / 👎.
- Catch `resolveInitialState()` rejection so `startSessionManager` resolves with `undefined` instead of propagating the error - Add test for storage failure scenario
There was a problem hiding this comment.
suggestion: replace ?. with !. or maybe const sessionManager = (await ...)!.
All those ?. are making tests less precise, because in cases like this:
expect(sessionManager?.findSession()!.anonymousId).toBeUndefined()the assertion can pass when sessionManager is undefined, but it shouldn't.
- Tighten startSessionManagerWithDefaults return type to Promise<SessionManager> - Remove ?. operators on sessionManager since it's now guaranteed non-undefined
Motivation
The
startSessionManagerfunction used a callback pattern (onReady) to signal when the session manager was initialized. This is unnecessary complexity since the function already operates asynchronously -- a Promise-based API is more idiomatic and easier to compose.Changes
startSessionManagerto returnPromise<SessionManager | undefined>instead of accepting anonReadycallbackundefinedwhen no session store strategy is configured (instead of never calling the callback)preStartLogsandpreStartRumtoawaitthe session manager instead of using callbacksTest instructions
yarn test:unit --spec packages/core/src/domain/session/sessionManager.spec.tsyarn test:unit --spec packages/logs/src/boot/preStartLogs.spec.tsyarn test:unit --spec packages/rum-core/src/boot/preStartRum.spec.tsChecklist