Skip to content

feat: Expose when the flag cache lookup finishes - #2251

Open
seanogdev wants to merge 16 commits into
launchdarkly:mainfrom
seanogdev:feat/cache-loaded-signal
Open

seanogdev wants to merge 16 commits into
launchdarkly:mainfrom
seanogdev:feat/cache-loaded-signal

Conversation

@seanogdev

@seanogdev seanogdev commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Closes #2250. A cache hit fires change, a miss fires nothing. Now both report:

client.on('cacheLoaded', (context, { hit }) => { ... });

const { hit } = await client.waitForCache(); // resolves even when called late
 LDClientImpl.identify
+  new lookup id, clear last result
   dataManager.identify
     FDv1: BaseDataManager.loadCached
       flagManager.loadCached
+      emit('cacheLoaded', context, { hit })
     FDv2: FDv2DataSource.runInitializers
       applyChangeSet -> dataCallback      # cached flags applied
+      onCacheLoaded(hit)                   # cache initializer, once per identify
+        emit('cacheLoaded', context, { hit })
+  identify queue after step (every outcome)
+    settle { hit: false } if no lookup ran (bootstrap, invalid context, failed before step)

waitForCache belongs to the latest identify call and never rejects. An identify called before start returns before it takes a lookup id, so it does not settle waiters; they wait for the first real identify. Exposed on browser, node-client, Electron (main and renderer) and the React no-op client. Svelte's restricted proxy has no on, so it is unchanged.

Evidence

  • Before: LDClientImpl.cacheloaded.test.ts
    no cached flags; identify(context)
      -> no event, no API to ask
    identify(A, cached); identify(B, uncached); waitForCache()
      -> { hit: true }               # stale result from A
    identify({ kind: 'car' }); waitForCache()
      -> pending; timed out at 5000 ms
    
    After:
    no cached flags; identify(context)
      -> cacheLoaded(context, { hit: false }); waitForCache() -> { hit: false }
    identify(A, cached); identify(B, uncached); waitForCache()
      -> { hit: false }
    identify({ kind: 'car' }); waitForCache()
      -> { hit: false }
    
    All tests pass in every dependent package (1916 total): sdk-client 903, browser 160, electron 216, node-client 196, react 212, react-native 89, vue 84, svelte 11, jest tooling 9, client-testing-plugin 36.

Merge Danger

Door: one-way once released

waitForCache and cacheLoaded become public API.

Blast Radius: additive

No existing behavior changes. TypeScript code that implements LDClient by hand (custom mocks, wrappers) must add waitForCache.


Note

Overview
Adds waitForCache() and a cacheLoaded event so apps know when an identify finishes reading persisted flags, including cache misses (previously only hits surfaced indirectly via flag changes).

LDCacheLookupResult ({ hit: boolean }) is wired through shared LDClientImpl: each identify gets a lookup id, cacheLoaded settles waiters, and identifies that skip the cache (bootstrap, validation failures, etc.) resolve with { hit: false } without hanging. DataManager.loadCached centralizes cache reads and emits cacheLoaded; the FDv2 path reports via onCacheLoaded on cache initializers (once per identify, including mode switches).

waitForCache is forwarded on browser, node-client, Electron (main IPC + renderer bridge), and React no-op/mocks; exports and jest tooling mocks updated. Extensive tests cover ordering, concurrent identifies, and bootstrap-skipped cache.

Reviewed by Cursor Bugbot for commit 67eb152. Bugbot is set up for automated code reviews on this repo. Configure here.

@seanogdev
seanogdev marked this pull request as ready for review November 25, 2026 12:20
@seanogdev
seanogdev requested a review from a team as a code owner November 25, 2026 12:20

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 898a851. Configure here.

@joker23

joker23 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution @seanogdev - per my comment in #2250 I'll hold off on doing a deep review until the SDK team aligns on what direction we want to take this.

also thanks for providing this PR, it definitely clarifies the ask!

@seanogdev

Copy link
Copy Markdown
Author

No worries at all ! Thanks for being so responsive

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose when the flag cache lookup finishes, including on a miss

2 participants