Skip to content

Commit 69a1c1d

Browse files
committed
chore: Adds LDDataSystemOptions for configuring the Data System.
1 parent 33c873e commit 69a1c1d

File tree

14 files changed

+468
-118
lines changed

14 files changed

+468
-118
lines changed

packages/shared/common/__tests__/subsystem/DataSystem/CompositeDataSource.test.ts

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ import {
77
DataSourceState,
88
DataSystemInitializer,
99
DataSystemSynchronizer,
10-
InitializerFactory,
11-
SynchronizerFactory,
10+
LDInitializerFactory,
11+
LDSynchronizerFactory,
1212
} from '../../../src/api/subsystem/DataSystem/DataSource';
1313
import { Backoff } from '../../../src/datasource/Backoff';
1414

15-
function makeInitializerFactory(internal: DataSystemInitializer): InitializerFactory {
16-
return {
17-
create: () => internal,
18-
};
15+
function makeInitializerFactory(internal: DataSystemInitializer): LDInitializerFactory {
16+
return () => internal;
1917
}
2018

21-
function makeSynchronizerFactory(internal: DataSystemSynchronizer): SynchronizerFactory {
22-
return {
23-
create: () => internal,
24-
};
19+
function makeSynchronizerFactory(internal: DataSystemSynchronizer): LDSynchronizerFactory {
20+
return () => internal;
2521
}
2622

2723
function makeTestTransitionConditions(): TransitionConditions {
@@ -58,7 +54,7 @@ function makeZeroBackoff(): Backoff {
5854

5955
it('handles initializer getting basis, switching to syncrhonizer', async () => {
6056
const mockInitializer1 = {
61-
run: jest
57+
start: jest
6258
.fn()
6359
.mockImplementation(
6460
(
@@ -73,7 +69,7 @@ it('handles initializer getting basis, switching to syncrhonizer', async () => {
7369

7470
const mockSynchronizer1Data = { key: 'sync1' };
7571
const mockSynchronizer1 = {
76-
run: jest
72+
start: jest
7773
.fn()
7874
.mockImplementation(
7975
(
@@ -101,19 +97,19 @@ it('handles initializer getting basis, switching to syncrhonizer', async () => {
10197
}
10298
});
10399

104-
underTest.run(callback, jest.fn());
100+
underTest.start(callback, jest.fn());
105101
});
106102

107-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
108-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(1);
103+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
104+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(1);
109105
expect(callback).toHaveBeenCalledTimes(2);
110106
expect(callback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
111107
expect(callback).toHaveBeenNthCalledWith(2, false, { key: 'sync1' });
112108
});
113109

114110
it('handles initializer getting basis, switches to synchronizer 1, falls back to synchronizer 2, recovers to synchronizer 1', async () => {
115111
const mockInitializer1: DataSystemInitializer = {
116-
run: jest
112+
start: jest
117113
.fn()
118114
.mockImplementation(
119115
(
@@ -129,7 +125,7 @@ it('handles initializer getting basis, switches to synchronizer 1, falls back to
129125
let sync1RunCount = 0;
130126
const mockSynchronizer1Data = { key: 'sync1' };
131127
const mockSynchronizer1 = {
132-
run: jest
128+
start: jest
133129
.fn()
134130
.mockImplementation(
135131
(
@@ -142,7 +138,7 @@ it('handles initializer getting basis, switches to synchronizer 1, falls back to
142138
message: 'I am error...man!',
143139
}); // error that will lead to fallback
144140
} else {
145-
_dataCallback(false, mockSynchronizer1Data); // second run will lead to data
141+
_dataCallback(false, mockSynchronizer1Data); // second start will lead to data
146142
}
147143
sync1RunCount += 1;
148144
},
@@ -152,7 +148,7 @@ it('handles initializer getting basis, switches to synchronizer 1, falls back to
152148

153149
const mockSynchronizer2Data = { key: 'sync2' };
154150
const mockSynchronizer2 = {
155-
run: jest
151+
start: jest
156152
.fn()
157153
.mockImplementation(
158154
(
@@ -181,12 +177,12 @@ it('handles initializer getting basis, switches to synchronizer 1, falls back to
181177
}
182178
});
183179

184-
underTest.run(callback, jest.fn());
180+
underTest.start(callback, jest.fn());
185181
});
186182

187-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
188-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(2);
189-
expect(mockSynchronizer2.run).toHaveBeenCalledTimes(1);
183+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
184+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(2);
185+
expect(mockSynchronizer2.start).toHaveBeenCalledTimes(1);
190186
expect(callback).toHaveBeenCalledTimes(3);
191187
expect(callback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
192188
expect(callback).toHaveBeenNthCalledWith(2, false, { key: 'sync2' }); // sync1 errors and fallsback
@@ -199,7 +195,7 @@ it('reports error when all initializers fail', async () => {
199195
message: 'I am initializer1 error!',
200196
};
201197
const mockInitializer1: DataSystemInitializer = {
202-
run: jest
198+
start: jest
203199
.fn()
204200
.mockImplementation(
205201
(
@@ -217,7 +213,7 @@ it('reports error when all initializers fail', async () => {
217213
message: 'I am initializer2 error!',
218214
};
219215
const mockInitializer2: DataSystemInitializer = {
220-
run: jest
216+
start: jest
221217
.fn()
222218
.mockImplementation(
223219
(
@@ -246,11 +242,11 @@ it('reports error when all initializers fail', async () => {
246242
}
247243
});
248244

249-
underTest.run(dataCallback, statusCallback);
245+
underTest.start(dataCallback, statusCallback);
250246
});
251247

252-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
253-
expect(mockInitializer2.run).toHaveBeenCalledTimes(1);
248+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
249+
expect(mockInitializer2.start).toHaveBeenCalledTimes(1);
254250
expect(dataCallback).toHaveBeenCalledTimes(0);
255251
expect(statusCallback).toHaveBeenNthCalledWith(
256252
1,
@@ -272,7 +268,7 @@ it('reports error when all initializers fail', async () => {
272268

273269
it('can be stopped when in thrashing synchronizer fallback loop', async () => {
274270
const mockInitializer1 = {
275-
run: jest
271+
start: jest
276272
.fn()
277273
.mockImplementation(
278274
(
@@ -287,7 +283,7 @@ it('can be stopped when in thrashing synchronizer fallback loop', async () => {
287283

288284
const mockSynchronizer1Error = { name: 'Error', message: 'I am error...man!' };
289285
const mockSynchronizer1 = {
290-
run: jest
286+
start: jest
291287
.fn()
292288
.mockImplementation(
293289
(
@@ -316,11 +312,11 @@ it('can be stopped when in thrashing synchronizer fallback loop', async () => {
316312
}
317313
});
318314

319-
underTest.run(dataCallback, statusCallback);
315+
underTest.start(dataCallback, statusCallback);
320316
});
321317

322-
expect(mockInitializer1.run).toHaveBeenCalled();
323-
expect(mockSynchronizer1.run).toHaveBeenCalled();
318+
expect(mockInitializer1.start).toHaveBeenCalled();
319+
expect(mockSynchronizer1.start).toHaveBeenCalled();
324320
expect(dataCallback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
325321
underTest.stop();
326322

@@ -340,7 +336,7 @@ it('can be stopped when in thrashing synchronizer fallback loop', async () => {
340336
it('can be stopped and restarted', async () => {
341337
const mockInitializer1Data = { key: 'init1' };
342338
const mockInitializer1 = {
343-
run: jest
339+
start: jest
344340
.fn()
345341
.mockImplementation(
346342
(
@@ -355,7 +351,7 @@ it('can be stopped and restarted', async () => {
355351

356352
const mockSynchronizer1Data = { key: 'sync1' };
357353
const mockSynchronizer1 = {
358-
run: jest
354+
start: jest
359355
.fn()
360356
.mockImplementation(
361357
(
@@ -383,13 +379,13 @@ it('can be stopped and restarted', async () => {
383379
resolve();
384380
}
385381
});
386-
// first run
387-
underTest.run(callback1, jest.fn());
382+
// first start
383+
underTest.start(callback1, jest.fn());
388384
});
389385

390-
// check first run triggered underlying data sources
391-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
392-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(1);
386+
// check first start triggered underlying data sources
387+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
388+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(1);
393389
expect(callback1).toHaveBeenCalledTimes(2);
394390

395391
// wait a moment for pending awaits to resolve the stop request
@@ -404,13 +400,13 @@ it('can be stopped and restarted', async () => {
404400
resolve();
405401
}
406402
});
407-
// second run
408-
underTest.run(callback2, jest.fn());
403+
// second start
404+
underTest.start(callback2, jest.fn());
409405
});
410406

411-
// check that second run triggers underlying data sources again
412-
expect(mockInitializer1.run).toHaveBeenCalledTimes(2);
413-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(2);
407+
// check that second start triggers underlying data sources again
408+
expect(mockInitializer1.start).toHaveBeenCalledTimes(2);
409+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(2);
414410
expect(callback2).toHaveBeenCalledTimes(2);
415411
});
416412

@@ -428,7 +424,7 @@ it('is well behaved with no initializers and no synchronizers configured', async
428424
resolve();
429425
});
430426

431-
underTest.run(jest.fn(), statusCallback);
427+
underTest.start(jest.fn(), statusCallback);
432428
});
433429

434430
expect(statusCallback).toHaveBeenNthCalledWith(1, DataSourceState.Closed, {
@@ -440,7 +436,7 @@ it('is well behaved with no initializers and no synchronizers configured', async
440436

441437
it('is well behaved with an initializer and no synchronizers configured', async () => {
442438
const mockInitializer1 = {
443-
run: jest
439+
start: jest
444440
.fn()
445441
.mockImplementation(
446442
(
@@ -466,7 +462,7 @@ it('is well behaved with an initializer and no synchronizers configured', async
466462
resolve();
467463
});
468464

469-
underTest.run(jest.fn(), statusCallback);
465+
underTest.start(jest.fn(), statusCallback);
470466
});
471467

472468
expect(statusCallback).toHaveBeenNthCalledWith(1, DataSourceState.Closed, {

packages/shared/common/src/api/subsystem/DataSystem/CompositeDataSource.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
Data,
77
DataSource,
88
DataSourceState,
9-
InitializerFactory,
10-
SynchronizerFactory,
9+
LDInitializerFactory,
10+
LDSynchronizerFactory,
1111
} from './DataSource';
1212

1313
// TODO: SDK-858, specify these constants when CompositeDataSource is used.
@@ -33,6 +33,7 @@ interface TransitionRequest {
3333
err?: Error;
3434
}
3535

36+
// TODO SDK-858: move this out of API directory to neighbor datasource folder
3637
/**
3738
* The {@link CompositeDataSource} can combine a number of {@link DataSystemInitializer}s and {@link DataSystemSynchronizer}s
3839
* into a single {@link DataSource}, implementing fallback and recovery logic internally to choose where data is sourced from.
@@ -54,8 +55,8 @@ export class CompositeDataSource implements DataSource {
5455
* @param _synchronizers factories to create {@link DataSystemSynchronizer}s, in priority order.
5556
*/
5657
constructor(
57-
private readonly _initializers: InitializerFactory[],
58-
private readonly _synchronizers: SynchronizerFactory[],
58+
private readonly _initializers: LDInitializerFactory[],
59+
private readonly _synchronizers: LDSynchronizerFactory[],
5960
private readonly _transitionConditions: TransitionConditions,
6061
private readonly _backoff: Backoff,
6162
private readonly _logger?: LDLogger,
@@ -67,7 +68,7 @@ export class CompositeDataSource implements DataSource {
6768
this._currentPosition = 0;
6869
}
6970

70-
async run(
71+
async start(
7172
dataCallback: (basis: boolean, data: Data) => void,
7273
statusCallback: (status: DataSourceState, err?: any) => void,
7374
): Promise<void> {
@@ -133,7 +134,7 @@ export class CompositeDataSource implements DataSource {
133134
}
134135
},
135136
);
136-
currentDS.run(
137+
currentDS.start(
137138
(basis, data) => callbackHandler.dataHandler(basis, data),
138139
(status, err) => callbackHandler.statusHandler(status, err),
139140
);
@@ -229,7 +230,7 @@ export class CompositeDataSource implements DataSource {
229230
return undefined;
230231
}
231232

232-
return this._initializers[this._currentPosition].create();
233+
return this._initializers[this._currentPosition]();
233234
}
234235
// getting here indicates we are using a synchronizer
235236

@@ -242,7 +243,7 @@ export class CompositeDataSource implements DataSource {
242243
// this is only possible if no synchronizers were provided
243244
return undefined;
244245
}
245-
return this._synchronizers[this._currentPosition].create();
246+
return this._synchronizers[this._currentPosition]();
246247
}
247248

248249
/**

packages/shared/common/src/api/subsystem/DataSystem/DataSource.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface DataSource {
1515
* @param statusCallback that will be called when data source state changes or an unrecoverable error
1616
* has been encountered.
1717
*/
18-
run(
18+
start(
1919
dataCallback: (basis: boolean, data: Data) => void,
2020
statusCallback: (status: DataSourceState, err?: any) => void,
2121
): void;
@@ -26,6 +26,11 @@ export interface DataSource {
2626
stop(): void;
2727
}
2828

29+
export type LDInitializerFactory = () => DataSystemInitializer;
30+
31+
export type LDSynchronizerFactory = () => DataSystemSynchronizer;
32+
33+
// TODO: renmae these to start with LD to help with API consumption
2934
/**
3035
* A data source that can be used to fetch the basis.
3136
*/
@@ -35,11 +40,3 @@ export interface DataSystemInitializer extends DataSource {}
3540
* A data source that can be used to fetch the basis or ongoing data changes.
3641
*/
3742
export interface DataSystemSynchronizer extends DataSource {}
38-
39-
export interface InitializerFactory {
40-
create(): DataSystemInitializer;
41-
}
42-
43-
export interface SynchronizerFactory {
44-
create(): DataSystemSynchronizer;
45-
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export {
22
DataSystemInitializer,
33
DataSystemSynchronizer,
4-
InitializerFactory,
5-
SynchronizerFactory,
4+
LDInitializerFactory as InitializerFactory,
5+
LDSynchronizerFactory as SynchronizerFactory,
66
} from './DataSource';
77
export { CompositeDataSource } from './CompositeDataSource';

packages/shared/common/src/api/subsystem/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import {
2+
DataSystemInitializer,
3+
DataSystemSynchronizer,
4+
InitializerFactory,
5+
SynchronizerFactory,
6+
} from './DataSystem';
17
import LDContextDeduplicator from './LDContextDeduplicator';
28
import LDEventProcessor from './LDEventProcessor';
39
import LDEventSender, { LDDeliveryStatus, LDEventSenderResult, LDEventType } from './LDEventSender';
410
import { LDStreamProcessor } from './LDStreamProcessor';
511

612
export {
13+
DataSystemInitializer,
14+
DataSystemSynchronizer,
15+
InitializerFactory,
16+
SynchronizerFactory,
717
LDEventProcessor,
818
LDContextDeduplicator,
919
LDEventSender,

0 commit comments

Comments
 (0)