Skip to content

Commit a8efb22

Browse files
committed
chore: Adds LDDataSystemOptions for configuring the Data System.
1 parent 6d0a08b commit a8efb22

File tree

14 files changed

+468
-119
lines changed

14 files changed

+468
-119
lines changed

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

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import {
22
CompositeDataSource,
3-
Transition,
43
TransitionConditions,
54
} from '../../../src/api/subsystem/DataSystem/CompositeDataSource';
65
import {
76
Data,
87
DataSourceState,
98
DataSystemInitializer,
109
DataSystemSynchronizer,
11-
InitializerFactory,
12-
SynchronizerFactory,
10+
LDInitializerFactory,
11+
LDSynchronizerFactory,
1312
} from '../../../src/api/subsystem/DataSystem/DataSource';
1413
import { Backoff } from '../../../src/datasource/Backoff';
1514

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

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

2823
function makeTestTransitionConditions(): TransitionConditions {
@@ -59,7 +54,7 @@ function makeZeroBackoff(): Backoff {
5954

6055
it('initializer gets basis, switch to syncrhonizer', async () => {
6156
const mockInitializer1 = {
62-
run: jest
57+
start: jest
6358
.fn()
6459
.mockImplementation(
6560
(
@@ -74,7 +69,7 @@ it('initializer gets basis, switch to syncrhonizer', async () => {
7469

7570
const mockSynchronizer1Data = { key: 'sync1' };
7671
const mockSynchronizer1 = {
77-
run: jest
72+
start: jest
7873
.fn()
7974
.mockImplementation(
8075
(
@@ -102,19 +97,19 @@ it('initializer gets basis, switch to syncrhonizer', async () => {
10297
}
10398
});
10499

105-
underTest.run(callback, jest.fn());
100+
underTest.start(callback, jest.fn());
106101
});
107102

108-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
109-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(1);
103+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
104+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(1);
110105
expect(callback).toHaveBeenCalledTimes(2);
111106
expect(callback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
112107
expect(callback).toHaveBeenNthCalledWith(2, false, { key: 'sync1' });
113108
});
114109

115110
it('initializer gets basis, switch to synchronizer 1, fallback to synchronizer 2, recover to synchronizer 1', async () => {
116111
const mockInitializer1: DataSystemInitializer = {
117-
run: jest
112+
start: jest
118113
.fn()
119114
.mockImplementation(
120115
(
@@ -130,7 +125,7 @@ it('initializer gets basis, switch to synchronizer 1, fallback to synchronizer 2
130125
let sync1RunCount = 0;
131126
const mockSynchronizer1Data = { key: 'sync1' };
132127
const mockSynchronizer1 = {
133-
run: jest
128+
start: jest
134129
.fn()
135130
.mockImplementation(
136131
(
@@ -143,7 +138,7 @@ it('initializer gets basis, switch to synchronizer 1, fallback to synchronizer 2
143138
message: 'I am error...man!',
144139
}); // error that will lead to fallback
145140
} else {
146-
_dataCallback(false, mockSynchronizer1Data); // second run will lead to data
141+
_dataCallback(false, mockSynchronizer1Data); // second start will lead to data
147142
}
148143
sync1RunCount += 1;
149144
},
@@ -153,7 +148,7 @@ it('initializer gets basis, switch to synchronizer 1, fallback to synchronizer 2
153148

154149
const mockSynchronizer2Data = { key: 'sync2' };
155150
const mockSynchronizer2 = {
156-
run: jest
151+
start: jest
157152
.fn()
158153
.mockImplementation(
159154
(
@@ -182,12 +177,12 @@ it('initializer gets basis, switch to synchronizer 1, fallback to synchronizer 2
182177
}
183178
});
184179

185-
underTest.run(callback, jest.fn());
180+
underTest.start(callback, jest.fn());
186181
});
187182

188-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
189-
expect(mockSynchronizer1.run).toHaveBeenCalledTimes(2);
190-
expect(mockSynchronizer2.run).toHaveBeenCalledTimes(1);
183+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
184+
expect(mockSynchronizer1.start).toHaveBeenCalledTimes(2);
185+
expect(mockSynchronizer2.start).toHaveBeenCalledTimes(1);
191186
expect(callback).toHaveBeenCalledTimes(3);
192187
expect(callback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
193188
expect(callback).toHaveBeenNthCalledWith(2, false, { key: 'sync2' }); // sync1 errors and fallsback
@@ -200,7 +195,7 @@ it('it reports error when all initializers fail', async () => {
200195
message: 'I am initializer1 error!',
201196
};
202197
const mockInitializer1: DataSystemInitializer = {
203-
run: jest
198+
start: jest
204199
.fn()
205200
.mockImplementation(
206201
(
@@ -218,7 +213,7 @@ it('it reports error when all initializers fail', async () => {
218213
message: 'I am initializer2 error!',
219214
};
220215
const mockInitializer2: DataSystemInitializer = {
221-
run: jest
216+
start: jest
222217
.fn()
223218
.mockImplementation(
224219
(
@@ -247,11 +242,11 @@ it('it reports error when all initializers fail', async () => {
247242
}
248243
});
249244

250-
underTest.run(dataCallback, statusCallback);
245+
underTest.start(dataCallback, statusCallback);
251246
});
252247

253-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
254-
expect(mockInitializer2.run).toHaveBeenCalledTimes(1);
248+
expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
249+
expect(mockInitializer2.start).toHaveBeenCalledTimes(1);
255250
expect(dataCallback).toHaveBeenCalledTimes(0);
256251
expect(statusCallback).toHaveBeenNthCalledWith(
257252
1,
@@ -273,7 +268,7 @@ it('it reports error when all initializers fail', async () => {
273268

274269
it('it can be stopped when in thrashing synchronizer fallback loop', async () => {
275270
const mockInitializer1 = {
276-
run: jest
271+
start: jest
277272
.fn()
278273
.mockImplementation(
279274
(
@@ -288,7 +283,7 @@ it('it can be stopped when in thrashing synchronizer fallback loop', async () =>
288283

289284
const mockSynchronizer1Error = { name: 'Error', message: 'I am error...man!' };
290285
const mockSynchronizer1 = {
291-
run: jest
286+
start: jest
292287
.fn()
293288
.mockImplementation(
294289
(
@@ -317,11 +312,11 @@ it('it can be stopped when in thrashing synchronizer fallback loop', async () =>
317312
}
318313
});
319314

320-
underTest.run(dataCallback, statusCallback);
315+
underTest.start(dataCallback, statusCallback);
321316
});
322317

323-
expect(mockInitializer1.run).toHaveBeenCalled();
324-
expect(mockSynchronizer1.run).toHaveBeenCalled();
318+
expect(mockInitializer1.start).toHaveBeenCalled();
319+
expect(mockSynchronizer1.start).toHaveBeenCalled();
325320
expect(dataCallback).toHaveBeenNthCalledWith(1, true, { key: 'init1' });
326321
underTest.stop();
327322

@@ -341,7 +336,7 @@ it('it can be stopped when in thrashing synchronizer fallback loop', async () =>
341336
it('it can be stopped and restarted', async () => {
342337
const mockInitializer1Data = { key: 'init1' };
343338
const mockInitializer1 = {
344-
run: jest
339+
start: jest
345340
.fn()
346341
.mockImplementation(
347342
(
@@ -356,7 +351,7 @@ it('it can be stopped and restarted', async () => {
356351

357352
const mockSynchronizer1Data = { key: 'sync1' };
358353
const mockSynchronizer1 = {
359-
run: jest
354+
start: jest
360355
.fn()
361356
.mockImplementation(
362357
(
@@ -384,13 +379,13 @@ it('it can be stopped and restarted', async () => {
384379
resolve();
385380
}
386381
});
387-
// first run
388-
underTest.run(callback1, jest.fn());
382+
// first start
383+
underTest.start(callback1, jest.fn());
389384
});
390385

391-
// check first run triggered underlying data sources
392-
expect(mockInitializer1.run).toHaveBeenCalledTimes(1);
393-
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);
394389
expect(callback1).toHaveBeenCalledTimes(2);
395390

396391
// wait a moment for pending awaits to resolve the stop request
@@ -405,13 +400,13 @@ it('it can be stopped and restarted', async () => {
405400
resolve();
406401
}
407402
});
408-
// second run
409-
underTest.run(callback2, jest.fn());
403+
// second start
404+
underTest.start(callback2, jest.fn());
410405
});
411406

412-
// check that second run triggers underlying data sources again
413-
expect(mockInitializer1.run).toHaveBeenCalledTimes(2);
414-
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);
415410
expect(callback2).toHaveBeenCalledTimes(2);
416411
});
417412

@@ -429,7 +424,7 @@ it('it is well behaved with no initializers and no synchronizers configured', as
429424
resolve();
430425
});
431426

432-
underTest.run(jest.fn(), statusCallback);
427+
underTest.start(jest.fn(), statusCallback);
433428
});
434429

435430
expect(statusCallback).toHaveBeenNthCalledWith(1, DataSourceState.Closed, {
@@ -441,7 +436,7 @@ it('it is well behaved with no initializers and no synchronizers configured', as
441436

442437
it('it is well behaved with an initializer and no synchronizers configured', async () => {
443438
const mockInitializer1 = {
444-
run: jest
439+
start: jest
445440
.fn()
446441
.mockImplementation(
447442
(
@@ -467,7 +462,7 @@ it('it is well behaved with an initializer and no synchronizers configured', asy
467462
resolve();
468463
});
469464

470-
underTest.run(jest.fn(), statusCallback);
465+
underTest.start(jest.fn(), statusCallback);
471466
});
472467

473468
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
@@ -5,8 +5,8 @@ import {
55
Data,
66
DataSource,
77
DataSourceState,
8-
InitializerFactory,
9-
SynchronizerFactory,
8+
LDInitializerFactory,
9+
LDSynchronizerFactory,
1010
} from './DataSource';
1111

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

35+
// TODO SDK-858: move this out of API directory to neighbor datasource folder
3536
/**
3637
* The {@link CompositeDataSource} can combine a number of {@link DataSystemInitializer}s and {@link DataSystemSynchronizer}s
3738
* into a single {@link DataSource}, implementing fallback and recovery logic internally to choose where data is sourced from.
@@ -52,8 +53,8 @@ export class CompositeDataSource implements DataSource {
5253
* @param _synchronizers factories to create {@link DataSystemSynchronizer}s, in priority order.
5354
*/
5455
constructor(
55-
private readonly _initializers: InitializerFactory[],
56-
private readonly _synchronizers: SynchronizerFactory[],
56+
private readonly _initializers: LDInitializerFactory[],
57+
private readonly _synchronizers: LDSynchronizerFactory[],
5758
private readonly _transitionConditions: TransitionConditions,
5859
private readonly _backoff: Backoff,
5960
) {
@@ -64,7 +65,7 @@ export class CompositeDataSource implements DataSource {
6465
this._currentPosition = 0;
6566
}
6667

67-
async run(
68+
async start(
6869
dataCallback: (basis: boolean, data: Data) => void,
6970
statusCallback: (status: DataSourceState, err?: any) => void,
7071
): Promise<void> {
@@ -128,7 +129,7 @@ export class CompositeDataSource implements DataSource {
128129
}
129130
},
130131
);
131-
currentDS.run(
132+
currentDS.start(
132133
(basis, data) => callbackHandler.dataHanlder(basis, data),
133134
(status, err) => callbackHandler.statusHandler(status, err),
134135
);
@@ -218,7 +219,7 @@ export class CompositeDataSource implements DataSource {
218219
return undefined;
219220
}
220221

221-
return this._initializers[this._currentPosition].create();
222+
return this._initializers[this._currentPosition]();
222223
}
223224
// getting here indicates we are using a synchronizer
224225

@@ -231,7 +232,7 @@ export class CompositeDataSource implements DataSource {
231232
// this is only possible if no synchronizers were provided
232233
return undefined;
233234
}
234-
return this._synchronizers[this._currentPosition].create();
235+
return this._synchronizers[this._currentPosition]();
235236
}
236237

237238
/**

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)