|
1 |
| -import { ISyncManager, ISyncManagerCS } from '../types'; |
| 1 | +import { ISyncManagerCS } from '../types'; |
2 | 2 | import { fromObjectSyncTaskFactory } from './syncTasks/fromObjectSyncTask';
|
3 | 3 | import { objectAssign } from '../../utils/lang/objectAssign';
|
4 | 4 | import { ISplitsParser } from './splitsParser/types';
|
@@ -29,26 +29,34 @@ export function syncManagerOfflineFactory(
|
29 | 29 | storage,
|
30 | 30 | }: ISdkFactoryContextSync): ISyncManagerCS {
|
31 | 31 |
|
| 32 | + const mainSyncManager = fromObjectSyncTaskFactory(splitsParserFactory(), storage, readiness, settings); |
| 33 | + const mainStart = mainSyncManager.start; |
| 34 | + const sharedStarts: Array<() => void> = []; |
| 35 | + |
32 | 36 | return objectAssign(
|
33 |
| - fromObjectSyncTaskFactory(splitsParserFactory(), storage, readiness, settings), |
| 37 | + mainSyncManager, |
34 | 38 | {
|
| 39 | + start() { |
| 40 | + mainStart(); |
| 41 | + sharedStarts.forEach(cb => cb()); |
| 42 | + sharedStarts.length = 0; |
| 43 | + }, |
35 | 44 | // fake flush, that resolves immediately
|
36 | 45 | flush,
|
37 | 46 |
|
38 | 47 | // [Only used for client-side]
|
39 |
| - shared(matchingKey: string, readinessManager: IReadinessManager): ISyncManager { |
| 48 | + shared(matchingKey: string, readinessManager: IReadinessManager) { |
| 49 | + // In LOCALHOST mode, shared clients are ready in the next event-loop cycle than created |
| 50 | + // SDK_READY cannot be emitted directly because this will not update the readiness status |
| 51 | + function emitSdkReady() { |
| 52 | + readinessManager.segments.emit(SDK_SEGMENTS_ARRIVED); // SDK_SPLITS_ARRIVED emitted by main SyncManager |
| 53 | + } |
| 54 | + |
| 55 | + if (mainSyncManager.isRunning()) setTimeout(emitSdkReady); |
| 56 | + else sharedStarts.push(emitSdkReady); |
| 57 | + |
40 | 58 | return {
|
41 |
| - start() { |
42 |
| - // In LOCALHOST mode, shared clients are ready in the next event-loop cycle than created |
43 |
| - // SDK_READY cannot be emitted directly because this will not update the readiness status |
44 |
| - setTimeout(() => { |
45 |
| - readinessManager.segments.emit(SDK_SEGMENTS_ARRIVED); // SDK_SPLITS_ARRIVED emitted by main SyncManager |
46 |
| - }, 0); |
47 |
| - }, |
48 | 59 | stop() { },
|
49 |
| - isRunning() { |
50 |
| - return true; |
51 |
| - }, |
52 | 60 | flush,
|
53 | 61 | };
|
54 | 62 | }
|
|
0 commit comments