Skip to content

Commit 2365346

Browse files
authored
Merge pull request #4 from nosnibor89/feat/svelte-sdk-client-improvements
refactor: update SvelteLDClient to use compat SDK and improve initial…
2 parents 13ba1b8 + cf49571 commit 2365346

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ 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';
5+
import { initialize, LDClient } from '@launchdarkly/js-client-sdk/compat';
66

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

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

1111
const clientSideID = 'test-client-side-id';
1212
const rawFlags = { 'test-flag': true, 'another-test-flag': 'flag-value' };
13+
const mockContext = { key: 'user1' };
1314

1415
// used to mock ready and change events on the LDClient
1516
const mockLDEventEmitter = new EventEmitter();
@@ -62,7 +63,7 @@ describe('launchDarkly', () => {
6263

6364
it('should set the loading status to false when the client is ready', async () => {
6465
const { initializing } = ld;
65-
ld.initialize(clientSideID);
66+
ld.initialize(clientSideID, mockContext);
6667

6768
expect(get(initializing)).toBe(true); // should be true before the ready event is emitted
6869
mockLDEventEmitter.emit('ready');
@@ -71,16 +72,16 @@ describe('launchDarkly', () => {
7172
});
7273

7374
it('should initialize the LaunchDarkly SDK instance', () => {
74-
ld.initialize(clientSideID);
75+
ld.initialize(clientSideID, mockContext);
7576

76-
expect(initialize).toHaveBeenCalledWith('test-client-side-id');
77+
expect(initialize).toHaveBeenCalledWith('test-client-side-id', mockContext);
7778
});
7879

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

83-
ld.initialize(clientSideID);
84+
ld.initialize(clientSideID, mockContext);
8485
mockLDEventEmitter.emit('ready');
8586

8687
expect(allFlagsSpy).toHaveBeenCalledOnce();
@@ -91,7 +92,7 @@ describe('launchDarkly', () => {
9192
const changedFlags = { ...rawFlags, 'changed-flag': true };
9293
const allFlagsSpy = vi.spyOn(mockLDClient, 'allFlags').mockReturnValue(changedFlags);
9394

94-
ld.initialize(clientSideID);
95+
ld.initialize(clientSideID, mockContext);
9596
mockLDEventEmitter.emit('change');
9697

9798
expect(allFlagsSpy).toHaveBeenCalledOnce();
@@ -116,7 +117,7 @@ describe('launchDarkly', () => {
116117

117118
it('should return a derived store that reflects the value of the specified flag', () => {
118119
const flagKey = 'test-flag';
119-
ld.initialize(clientSideID);
120+
ld.initialize(clientSideID, mockContext);
120121

121122
const flagStore = ld.watch(flagKey);
122123

@@ -126,7 +127,7 @@ describe('launchDarkly', () => {
126127
it('should update the flag store when the flag value changes', () => {
127128
const booleanFlagKey = 'test-flag';
128129
const stringFlagKey = 'another-test-flag';
129-
ld.initialize(clientSideID);
130+
ld.initialize(clientSideID, mockContext);
130131
const flagStore = ld.watch(booleanFlagKey);
131132
const flagStore2 = ld.watch(stringFlagKey);
132133

@@ -153,7 +154,7 @@ describe('launchDarkly', () => {
153154

154155
it('should return undefined if the flag is not found', () => {
155156
const flagKey = 'non-existent-flag';
156-
ld.initialize(clientSideID);
157+
ld.initialize(clientSideID, mockContext);
157158

158159
const flagStore = ld.watch(flagKey);
159160

packages/sdk/svelte/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
},
4545
"peerDependencies": {
4646
"@launchdarkly/js-client-sdk": "workspace:^",
47-
"@launchdarkly/node-server-sdk": "^9.4.6",
4847
"svelte": "^4.0.0"
4948
},
5049
"dependencies": {
@@ -56,12 +55,12 @@
5655
"@sveltejs/adapter-auto": "^3.0.0",
5756
"@sveltejs/kit": "^2.0.0",
5857
"@sveltejs/package": "^2.0.0",
59-
"@sveltejs/vite-plugin-svelte": "^3.0.0",
58+
"@sveltejs/vite-plugin-svelte": "^5.0.1",
6059
"@testing-library/svelte": "^5.2.0",
6160
"@types/jest": "^29.5.11",
6261
"@typescript-eslint/eslint-plugin": "^6.20.0",
6362
"@typescript-eslint/parser": "^6.20.0",
64-
"@vitest/ui": "^1.6.0",
63+
"@vitest/ui": "^2.1.8",
6564
"eslint": "^8.45.0",
6665
"eslint-config-airbnb-base": "^15.0.0",
6766
"eslint-config-airbnb-typescript": "^17.1.0",
@@ -76,13 +75,13 @@
7675
"prettier-plugin-svelte": "^3.1.2",
7776
"publint": "^0.1.9",
7877
"rimraf": "^5.0.5",
79-
"svelte": "^4.2.7",
78+
"svelte": "^5.4.0",
8079
"svelte-check": "^3.6.0",
8180
"ts-jest": "^29.1.1",
8281
"ts-node": "^10.9.2",
8382
"typedoc": "0.25.0",
8483
"typescript": "5.1.6",
85-
"vite": "^5.2.6",
86-
"vitest": "^2.1.4"
84+
"vite": "^6.0.2",
85+
"vitest": "^2.1.8"
8786
}
8887
}

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

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

3+
import type { LDFlagSet } from '@launchdarkly/js-client-sdk';
34
import {
45
initialize,
56
type LDClient,
67
type LDContext,
7-
type LDFlagSet,
8-
} from '@launchdarkly/js-client-sdk';
8+
type LDFlagValue,
9+
} from '@launchdarkly/js-client-sdk/compat';
10+
11+
export type { LDContext };
912

1013
/** Client ID for LaunchDarkly */
1114
export type LDClientID = string;
1215

1316
/** Flags for LaunchDarkly */
1417
export type LDFlags = LDFlagSet;
1518

16-
/** Value of LaunchDarkly flags */
17-
export type LDFlagsValue = LDFlagSet[string];
18-
1919
/**
2020
* Checks if the LaunchDarkly client is initialized.
2121
* @param {LDClient | undefined} client - The LaunchDarkly client.
@@ -70,21 +70,21 @@ function createLD() {
7070
/**
7171
* Initializes the LaunchDarkly client.
7272
* @param {LDClientID} clientId - The client ID.
73-
* @returns {Writable<boolean>} An object with the initialization status store.
73+
* @param {LDContext} context - The user context.
74+
* @returns {Object} An object with the initialization status store.
7475
*/
75-
76-
function LDInitialize(clientId: LDClientID) {
77-
coreLdClient = initialize(clientId);
76+
function LDInitialize(clientId: LDClientID, context: LDContext) {
77+
coreLdClient = initialize(clientId, context);
7878
coreLdClient!.on('ready', () => {
7979
loading.set(false);
8080
const rawFlags = coreLdClient!.allFlags();
81-
const allFlags = toFlagsProxy(coreLdClient, rawFlags);
81+
const allFlags = toFlagsProxy(coreLdClient!, rawFlags);
8282
flagsWritable.set(allFlags);
8383
});
8484

8585
coreLdClient!.on('change', () => {
8686
const rawFlags = coreLdClient!.allFlags();
87-
const allFlags = toFlagsProxy(coreLdClient, rawFlags);
87+
const allFlags = toFlagsProxy(coreLdClient!, rawFlags);
8888
flagsWritable.set(allFlags);
8989
});
9090

@@ -108,8 +108,8 @@ function createLD() {
108108
* @param {string} flagKey - The key of the flag to watch.
109109
* @returns {Readable<LDFlagsValue>} A readable store of the flag value.
110110
*/
111-
const watch = (flagKey: string): Readable<LDFlagsValue> =>
112-
derived<Writable<LDFlags>, LDFlagsValue>(flagsWritable, ($flags) => $flags[flagKey]);
111+
const watch = (flagKey: string): Readable<LDFlagValue> =>
112+
derived<Writable<LDFlags>, LDFlagValue>(flagsWritable, ($flags) => $flags[flagKey]);
113113

114114
/**
115115
* Checks if a flag is on.

0 commit comments

Comments
 (0)