Skip to content

Commit bcde547

Browse files
committed
chore: update svelte sdk to use browser 4.x
- removes the browser compat dependency on svelte sdk
1 parent d023a33 commit bcde547

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

packages/sdk/svelte/__tests__/lib/client/SvelteLDClient.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { EventEmitter } from 'node:events';
22
import { get } from 'svelte/store';
33
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
44

5-
import { initialize, LDClient } from '@launchdarkly/js-client-sdk/compat';
5+
import { createClient, LDClient } from '@launchdarkly/js-client-sdk';
66

77
import { LD } from '../../../src/lib/client/SvelteLDClient';
88

9-
vi.mock('@launchdarkly/js-client-sdk/compat', { spy: true });
9+
vi.mock('@launchdarkly/js-client-sdk', { spy: true });
1010

1111
const clientSideID = 'test-client-side-id';
1212
const rawFlags = { 'test-flag': true, 'another-test-flag': 'flag-value' };
@@ -21,6 +21,7 @@ const mockLDClient = {
2121
allFlags: vi.fn().mockReturnValue(rawFlags),
2222
variation: vi.fn((_, defaultValue) => defaultValue),
2323
identify: vi.fn(),
24+
start: vi.fn(),
2425
};
2526

2627
describe('launchDarkly', () => {
@@ -40,8 +41,7 @@ describe('launchDarkly', () => {
4041
const ld = LD;
4142

4243
beforeEach(() => {
43-
// mocks the initialize function to return the mockLDClient
44-
(initialize as Mock<typeof initialize>).mockReturnValue(
44+
(createClient as Mock<typeof createClient>).mockReturnValue(
4545
mockLDClient as unknown as LDClient,
4646
);
4747
});
@@ -66,23 +66,23 @@ describe('launchDarkly', () => {
6666
ld.initialize(clientSideID, mockContext);
6767

6868
expect(get(initializing)).toBe(true); // should be true before the ready event is emitted
69-
mockLDEventEmitter.emit('ready');
69+
mockLDEventEmitter.emit('initialized');
7070

7171
expect(get(initializing)).toBe(false);
7272
});
7373

7474
it('should initialize the LaunchDarkly SDK instance', () => {
7575
ld.initialize(clientSideID, mockContext);
7676

77-
expect(initialize).toHaveBeenCalledWith('test-client-side-id', mockContext);
77+
expect(createClient).toHaveBeenCalledWith('test-client-side-id', mockContext, undefined);
7878
});
7979

8080
it('should register function that gets flag values when client is ready', () => {
8181
const newFlags = { ...rawFlags, 'new-flag': true };
8282
const allFlagsSpy = vi.spyOn(mockLDClient, 'allFlags').mockReturnValue(newFlags);
8383

8484
ld.initialize(clientSideID, mockContext);
85-
mockLDEventEmitter.emit('ready');
85+
mockLDEventEmitter.emit('initialized');
8686

8787
expect(allFlagsSpy).toHaveBeenCalledOnce();
8888
expect(allFlagsSpy).toHaveReturnedWith(newFlags);
@@ -104,8 +104,7 @@ describe('launchDarkly', () => {
104104
const ld = LD;
105105

106106
beforeEach(() => {
107-
// mocks the initialize function to return the mockLDClient
108-
(initialize as Mock<typeof initialize>).mockReturnValue(
107+
(createClient as Mock<typeof createClient>).mockReturnValue(
109108
mockLDClient as unknown as LDClient,
110109
);
111110
});
@@ -166,8 +165,7 @@ describe('launchDarkly', () => {
166165
const ld = LD;
167166

168167
beforeEach(() => {
169-
// mocks the initialize function to return the mockLDClient
170-
(initialize as Mock<typeof initialize>).mockReturnValue(
168+
(createClient as Mock<typeof createClient>).mockReturnValue(
171169
mockLDClient as unknown as LDClient,
172170
);
173171
});
@@ -191,8 +189,7 @@ describe('launchDarkly', () => {
191189
const ld = LD;
192190

193191
beforeEach(() => {
194-
// mocks the initialize function to return the mockLDClient
195-
(initialize as Mock<typeof initialize>).mockReturnValue(
192+
(createClient as Mock<typeof createClient>).mockReturnValue(
196193
mockLDClient as unknown as LDClient,
197194
);
198195
});

packages/sdk/svelte/src/lib/client/SvelteLDClient.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { derived, type Readable, readonly, writable, type Writable } from 'svelte/store';
22

3-
import type { LDFlagSet } from '@launchdarkly/js-client-sdk';
43
import {
5-
initialize,
4+
createClient as createClientSdk,
65
type LDClient,
76
type LDContext,
7+
type LDFlagSet,
88
type LDFlagValue,
9-
} from '@launchdarkly/js-client-sdk/compat';
9+
type LDOptions,
10+
} from '@launchdarkly/js-client-sdk';
1011

1112
export type { LDContext, LDFlagValue };
1213

@@ -62,7 +63,7 @@ function toFlagsProxy(client: LDClient, flags: LDFlags): LDFlags {
6263
* Creates a LaunchDarkly instance.
6364
* @returns {Object} The LaunchDarkly instance object.
6465
*/
65-
function createLD() {
66+
function init() {
6667
let coreLdClient: LDClient | undefined;
6768
const loading = writable(true);
6869
const flagsWritable = writable<LDFlags>({});
@@ -73,21 +74,23 @@ function createLD() {
7374
* @param {LDContext} context - The user context.
7475
* @returns {Object} An object with the initialization status store.
7576
*/
76-
function LDInitialize(clientId: LDClientID, context: LDContext) {
77-
coreLdClient = initialize(clientId, context);
78-
coreLdClient!.on('ready', () => {
77+
function initialize(clientId: LDClientID, context: LDContext, options?: LDOptions) {
78+
coreLdClient = createClientSdk(clientId, context, options);
79+
coreLdClient.on('initialized', () => {
7980
loading.set(false);
8081
const rawFlags = coreLdClient!.allFlags();
8182
const allFlags = toFlagsProxy(coreLdClient!, rawFlags);
8283
flagsWritable.set(allFlags);
8384
});
8485

85-
coreLdClient!.on('change', () => {
86+
coreLdClient.on('change', () => {
8687
const rawFlags = coreLdClient!.allFlags();
8788
const allFlags = toFlagsProxy(coreLdClient!, rawFlags);
8889
flagsWritable.set(allFlags);
8990
});
9091

92+
coreLdClient.start();
93+
9194
return {
9295
initializing: loading,
9396
};
@@ -125,12 +128,12 @@ function createLD() {
125128
return {
126129
identify,
127130
flags: readonly(flagsWritable),
128-
initialize: LDInitialize,
131+
initialize,
129132
initializing: readonly(loading),
130133
watch,
131134
useFlag,
132135
};
133136
}
134137

135138
/** The LaunchDarkly instance */
136-
export const LD = createLD();
139+
export const LD = init();

0 commit comments

Comments
 (0)