Skip to content

refactor(client): Build each service once in the initializer - #537

Merged
abelonogov-ld merged 2 commits into
v11from
andrey/ldclient-construct-services-once
Oct 9, 2026
Merged

abelonogov-ld merged 2 commits into
v11from
andrey/ldclient-construct-services-once

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

LDClient.init builds 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. onEventSyncComplete becomes a static over an OSLog captured before the first assignment, and the reporter gets it at construction.

Flag synchronizer. Its callback genuinely reports to the client, so LDFlagSynchronizing makes onSyncComplete settable and the initializer assigns it once self is 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. onFlagSyncComplete now reads back from the synchronizer it made rather than from the factory call, so it sees the callback whichever way it arrived.

Not changed

connectionInformation is still assigned twice, for the same reason. It is a larger untangling and does not block anything, so it stays for now.

Verification

  • 684 tests pass on iOS; macOS and tvOS build.
  • Two new tests pin the behaviour: each service is built exactly once, and the synchronizer is told where to report. Both fail if the second construction is put back.
  • No new SwiftLint violations. The three pre-existing LDClient.swift ones improve: initializer body 63 → 60 lines, class body 589 → 587.

Note

Overview
LDClient initialization no longer constructs the event reporter and flag synchronizer twice. The duplicate build existed because sync-complete callbacks needed self before stored properties were fully initialized.

For the event reporter, sync completion only logged, so onEventSyncComplete becomes a static logEventSyncComplete that takes an OSLog captured at construction—no client reference required.

For the flag synchronizer, LDFlagSynchronizing now exposes a settable onSyncComplete; the initializer assigns onFlagSyncComplete once self is 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, and LDClientSpec asserts each service is built exactly once and the synchronizer gets a non-nil reporter. DiagnosticReporterSpec adds an NSLock around 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.testRetries occasionally 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.

`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>
@abelonogov-ld
abelonogov-ld merged commit cadbf32 into v11 Oct 9, 2026
19 checks passed
@abelonogov-ld
abelonogov-ld deleted the andrey/ldclient-construct-services-once branch October 9, 2026 13:35
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.

2 participants