Skip to content

Commit b894ac2

Browse files
authored
fix(js-client-sdk): switching context does not update streaming connection (#1153)
**Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Describe the solution you've provided** Steaming connection will reset if the context key is different. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/launchdarkly/js-core/pull/1153" target="_blank"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1"> <img src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1" alt="Open with Devin"> </picture> </a> <!-- devin-review-badge-end --> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes streaming lifecycle behavior during `identify`, which can impact connection churn and update delivery when applications frequently switch contexts. > > **Overview** > Fixes context switching while streaming by **stopping the active streaming data source on every `identify`** and then re-evaluating streaming state, ensuring a new EventSource is created for the new context. > > Adds a regression test asserting that calling `identify` twice with different contexts results in the stream being created twice (i.e., restarted). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit fe84093. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent da3f72e commit b894ac2

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

packages/sdk/browser/__tests__/BrowserDataManager.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,24 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
460460
);
461461
});
462462

463+
it('restarts the stream when identify is called with a new context', async () => {
464+
const context1 = Context.fromLDContext({ kind: 'user', key: 'user-1' });
465+
const context2 = Context.fromLDContext({ kind: 'user', key: 'user-2' });
466+
const identifyOptions: BrowserIdentifyOptions = {};
467+
const identifyResolve = jest.fn();
468+
const identifyReject = jest.fn();
469+
470+
flagManager.loadCached.mockResolvedValue(false);
471+
472+
dataManager.setAutomaticStreamingState(true);
473+
474+
await dataManager.identify(identifyResolve, identifyReject, context1, identifyOptions);
475+
expect(platform.requests.createEventSource).toHaveBeenCalledTimes(1);
476+
477+
await dataManager.identify(identifyResolve, identifyReject, context2, identifyOptions);
478+
expect(platform.requests.createEventSource).toHaveBeenCalledTimes(2);
479+
});
480+
463481
it('does not start a stream if identify has not been called', async () => {
464482
expect(platform.requests.createEventSource).not.toHaveBeenCalled();
465483
dataManager.setForcedStreaming(true);

packages/sdk/browser/src/BrowserDataManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default class BrowserDataManager extends BaseDataManager {
102102

103103
await this._finishIdentifyFromPoll(context, identifyResolve, identifyReject);
104104
}
105+
this._stopDataSource();
105106
this._updateStreamingState();
106107
}
107108

0 commit comments

Comments
 (0)