Repository navigation
refactor(client): Build each service once in the initializer - #537
Merged
Merged
Conversation
`LDClient.init` built the event reporter and the flag synchronizer, threw both away, and built them again -- the second time with a callback that reports back to the client. The duplication is not an oversight: naming an instance method is an implicit reference to `self`, which Swift forbids until every stored property has a value, so neither callback could be given on the first pass. The pattern has been there since 2019. Each is now built once. The event reporter's callback only ever logged, so it becomes a static over an `OSLog` captured before the first assignment, with no need for a client at all. The synchronizer's callback does report to the client, so `LDFlagSynchronizing` makes it settable and the initializer assigns it once `self` is usable. The other two places that build a synchronizer are past initialization and still pass it at construction. In the mock factory, `onFlagSyncComplete` reads back from the synchronizer it made rather than from the factory call, so it sees the callback whichever way it arrived. Co-authored-by: Cursor <cursoragent@cursor.com>
…ueue `testRetries` failed now and then under load with a leftover event, even though no extra request arrived. The mock answers a publish and its retry back to back on the reporter's queue, so the second append can land while the test thread, woken by the first, is removing from the same array. An unsynchronized array mutated from two threads can lose either change. The events are now taken under a lock on both sides. Co-authored-by: Cursor <cursoragent@cursor.com>
tanderson-ld
approved these changes
Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LDClient.initbuilds the event reporter and the flag synchronizer, throws both away, and builds them again a few lines later — the second time with a callback that reports back to the client.The duplication is not an oversight. Naming an instance method is an implicit reference to
self, which Swift forbids until every stored property has a value, so neither callback could be given on the first pass. The workaround has been there since 2019. It is also the thing reviewers keep tripping over in the event-durability stack, so this lands on its own, ahead of it.What changes
Each service is built once.
Event reporter. Its callback only ever logged, so it does not need a client at all.
onEventSyncCompletebecomes a static over anOSLogcaptured before the first assignment, and the reporter gets it at construction.Flag synchronizer. Its callback genuinely reports to the client, so
LDFlagSynchronizingmakesonSyncCompletesettable and the initializer assigns it onceselfis usable. The two other places that build a synchronizer are past initialization and still pass it at construction, so that path is unchanged.Mock factory.
onFlagSyncCompletenow reads back from the synchronizer it made rather than from the factory call, so it sees the callback whichever way it arrived.Not changed
connectionInformationis still assigned twice, for the same reason. It is a larger untangling and does not block anything, so it stays for now.Verification
LDClient.swiftones improve: initializer body 63 → 60 lines, class body 589 → 587.Note
Overview
LDClientinitialization no longer constructs the event reporter and flag synchronizer twice. The duplicate build existed because sync-complete callbacks neededselfbefore stored properties were fully initialized.For the event reporter, sync completion only logged, so
onEventSyncCompletebecomes a staticlogEventSyncCompletethat takes anOSLogcaptured at construction—no client reference required.For the flag synchronizer,
LDFlagSynchronizingnow exposes a settableonSyncComplete; the initializer assignsonFlagSyncCompleteonceselfis usable. Other code paths that create a synchronizer after init can still pass the callback at construction.Tests and mocks track the new wiring: mocks gain
onSyncComplete, the test factory records the built synchronizer and reads the callback from it, andLDClientSpecasserts each service is built exactly once and the synchronizer gets a non-nil reporter.DiagnosticReporterSpecadds anNSLockaround captured diagnostic events to avoid races when retries append while the test thread is dequeuing.Reviewed by Cursor Bugbot for commit 3b109a8. Bugbot is set up for automated code reviews on this repo. Configure here.
Also: a pre-existing test flake
DiagnosticReporterSpec.testRetriesoccasionally failed under load with a leftover event. The mock answers a publish and its retry back to back on the reporter's queue, so the second append could land while the test thread was removing from the same unsynchronized array. The events are now taken under a lock. Test-only; 10 consecutive full-suite runs (8,240 tests) passed with it.