Skip to content

Commit 1ba2c69

Browse files
committed
chore: bot comment
1 parent c2d3927 commit 1ba2c69

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

packages/sdk/electron/__tests__/platform/ElectronStorage.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import * as fs from 'fs/promises';
22

33
import type { LDLogger } from '@launchdarkly/js-client-sdk-common';
44

5-
import ElectronStorage from '../../src/platform/ElectronStorage';
5+
import ElectronStorage, {
6+
getElectronStorage,
7+
resetElectronStorage,
8+
} from '../../src/platform/ElectronStorage';
69

710
jest.mock('fs/promises');
811
jest.mock('electron', () => ({
@@ -23,6 +26,7 @@ const logger: LDLogger = {
2326

2427
beforeEach(() => {
2528
jest.clearAllMocks();
29+
resetElectronStorage();
2630
});
2731

2832
it('handles failed initialization when clearing values', async () => {
@@ -230,3 +234,16 @@ it('handles error when setting values', async () => {
230234
'Error setting key in storage: key3, reason: write failed',
231235
);
232236
});
237+
238+
it('getElectronStorage returns the same instance on subsequent calls', () => {
239+
const a = getElectronStorage(logger);
240+
const b = getElectronStorage(logger);
241+
expect(a).toBe(b);
242+
});
243+
244+
it('resetElectronStorage causes a fresh instance on next call', () => {
245+
const a = getElectronStorage(logger);
246+
resetElectronStorage();
247+
const b = getElectronStorage(logger);
248+
expect(a).not.toBe(b);
249+
});

packages/sdk/electron/contract-tests/entity/src/ClientEntity.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ export class ClientEntity {
140140
async close() {
141141
try {
142142
await this._client.close();
143-
if (fs.existsSync(this._storagePath)) {
144-
fs.rmSync(this._storagePath, { recursive: true });
145-
}
146-
if (fs.existsSync(`${this._storagePath}.tmp`)) {
147-
fs.rmSync(`${this._storagePath}.tmp`, { recursive: true });
148-
}
149143
this._logger.info('Test ended');
150144
} catch (error) {
151145
this._logger.error(`Error closing client: ${error}`);

packages/sdk/electron/src/platform/ElectronPlatform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ElectronCrypto from './ElectronCrypto';
55
import ElectronEncoding from './ElectronEncoding';
66
import ElectronInfo from './ElectronInfo';
77
import ElectronRequests from './ElectronRequests';
8-
import ElectronStorage from './ElectronStorage';
8+
import { getElectronStorage } from './ElectronStorage';
99

1010
// NOTE: Because Electron main process runs on Node.js, this platform should be
1111
// very similar to the Node server sdk platform.
@@ -22,7 +22,7 @@ export default class ElectronPlatform implements platform.Platform {
2222
requests: platform.Requests;
2323

2424
constructor(logger: LDLogger, options: ElectronOptions) {
25-
this.storage = new ElectronStorage(logger);
25+
this.storage = getElectronStorage(logger);
2626
this.requests = new ElectronRequests(
2727
options.tlsParams,
2828
options.proxyOptions,

packages/sdk/electron/src/platform/ElectronStorage.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ export default class ElectronStorage implements Storage {
113113
}
114114
}
115115
}
116+
117+
// Storage should be a singleton to support multiple instances of the SDK. This should have
118+
// the same limitations as the browser sdk using the shared localStorage cache.
119+
let instance: ElectronStorage | undefined;
120+
121+
export function getElectronStorage(logger?: LDLogger): ElectronStorage {
122+
if (!instance) {
123+
instance = new ElectronStorage(logger);
124+
}
125+
return instance;
126+
}
127+
128+
/** @internal Visible for testing only. */
129+
export function resetElectronStorage(): void {
130+
instance = undefined;
131+
}

0 commit comments

Comments
 (0)