Skip to content

Commit 9ee35a2

Browse files
committed
SDK Queuing System #7
1 parent 9c4f514 commit 9ee35a2

File tree

1 file changed

+67
-60
lines changed

1 file changed

+67
-60
lines changed

lib/src/data/data_manager.dart

+67-60
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class DataManager {
108108
final Stopwatch stopwatch = Stopwatch()..start();
109109

110110
initialized = true;
111+
queuingDone = false;
111112

112113
// Initialize all locally cached data.
113114
final cachedModel = localDataRepository.fetchPublishModel(
@@ -156,25 +157,21 @@ class DataManager {
156157
if (success) {
157158
log('[DataManager] [slug] Complete publish model from slug is downloaded in background. Emitting.');
158159

159-
loadFontsFromPublishModel();
160-
} else {
161-
log('[DataManager] [slug] Failed to download complete publish bundle for slug ${config.slug}.');
162-
}
163-
});
164-
165-
if (_publishModel == null) {
166-
await publishBundleFuture;
160+
loadFontsFromPublishModel();
161+
} else {
162+
log('Failed to download complete publish bundle for slug ${config.slug}.');
167163
}
168164

169-
_recordTime(stopwatch);
170-
} catch (e, stackTrace) {
165+
_logTime(stopwatch);
166+
return;
167+
} catch (e, stackTrace) {
171168
log('[DataManager] Error trying to download complete publish model from slug.');
172169
log('[DataManager] Since no publish model is cached, this is a complete stop to the data manager.');
173170
log('[DataManager]', level: 900, error: e, stackTrace: stackTrace);
174171
print(e);
175172
print(stackTrace);
176173

177-
_recordTime(stopwatch);
174+
_logTime(stopwatch);
178175

179176
log('[DataManager] [slug] Failed to download complete publish bundle for slug ${config.slug}.');
180177
return;
@@ -192,7 +189,7 @@ class DataManager {
192189

193190
if (authManager.authData == null) {
194191
log('[DataManager] No auth data is available. Continuing as if offline.');
195-
_recordTime(stopwatch);
192+
_logTime(stopwatch);
196193
return;
197194
}
198195

@@ -236,9 +233,7 @@ class DataManager {
236233
// If the publish model is still null, then we need to wait for the first
237234
// publish model to arrive from the server via the stream above.
238235
if (_publishModel == null) {
239-
log(
240-
'[DataManager] Publish model is still null during init. Waiting for the first publish model from the server.',
241-
);
236+
log('[DataManager] Publish model is still null during init. Waiting for the first publish model from the server.');
242237
final model = await firstPublishEvent;
243238
await onPublishModelLoaded(model);
244239
_publishModel = model;
@@ -252,58 +247,67 @@ class DataManager {
252247
'[DataManager] Publish model is still null.\n'
253248
'Is there a network problem or bad authentication?',
254249
);
255-
_recordTime(stopwatch);
250+
_logTime(stopwatch);
256251
return;
257252
}
253+
}
258254

259-
// If a [layoutID] was specified, then that layout must be prioritized and
260-
// downloaded first if it is not already cached.
261-
//
262-
// If we could not download it earlier, that would be because we did not
263-
// have a publish model available and needed to wait for one to arrive
264-
// from the server for the first time or that it failed to download for some
265-
// unknown reason.
266-
//
267-
// Perhaps the publish model was simply out of date locally,
268-
// but now that we fetched a new one, and didPrepareLayout is still false,
269-
// we can try to download the layout again with the new publish model.
270-
//
271-
// At this stage of the function, we can be sure that a publish model exists
272-
// and can safely download the desired [layoutID], because if a publish
273-
// model is still null, we cannot proceed further and this function
274-
// terminates earlier.
275-
if (!didPrepareLayout && layoutID != null) {
276-
log(
277-
'[DataManager] Publish model is definitely available. We can safely download layout [$layoutID] now.',
278-
);
279-
await getOrFetchPopulatedLayout(layoutID: layoutID);
255+
// If a [layoutID] was specified, then that layout must be prioritized and
256+
// downloaded first if it is not already cached.
257+
//
258+
// If we could not download it earlier, that would be because we did not
259+
// have a publish model available and needed to wait for one to arrive
260+
// from the server for the first time or that it failed to download for
261+
// some unknown reason.
262+
//
263+
// Perhaps the publish model was simply out of date locally,
264+
// but now that we fetched a new one, and [didPrepareLayout] is still
265+
// false, we can try to download the layout again with the new publish
266+
// model.
267+
//
268+
// At this stage of the function, we can be sure that a publish model
269+
// exists and can safely download the desired [layoutID], because if a
270+
// publish model is still null, we cannot proceed further and this
271+
// function terminates earlier.
272+
if (!didPrepareLayout && layoutID != null) {
273+
log('[DataManager] We can safely download layout [$layoutID] now.');
274+
await getOrFetchPopulatedLayout(layoutID: layoutID);
275+
log('[DataManager] Layout [$layoutID] downloaded from init successfully.');
276+
}
280277

281-
log(
282-
'[DataManager] Layout [$layoutID] during init download complete.',
283-
);
284-
}
278+
// Add all the layouts to the download queue excluding the [layoutID] if
279+
// that was specified. We don't want to download that layout twice.
280+
if (config.preload) {
281+
log('[DataManager] Config preload was specified during init, adding ${_publishModel!.updates.layouts.length - 1} the layouts to the download queue...');
282+
_downloadQueue.addAll(
283+
[..._publishModel!.updates.layouts.keys]..remove(layoutID),
284+
);
285+
log('[DataManager] All layouts during init download complete.');
286+
}
285287

286-
// If a [layoutID] is not specified, then we need to download all layouts
287-
// in the background.
288-
if (config.preload) {
289-
log('[DataManager] Config preload was specified during init, but a layoutID was not specified. Waiting for all layouts to download...');
290-
log('[DataManager] Config preload was specified during init. Downloading ${_publishModel!.updates.layouts.length} layouts as a queue...');
291-
_downloadQueue.addAll(_publishModel!.updates.layouts.keys);
292-
293-
while (_downloadQueue.isNotEmpty) {
294-
final String layoutID = _downloadQueue.removeAt(0);
295-
log('[DataManager] \tDownloading layout [$layoutID]...');
296-
await getOrFetchPopulatedLayout(layoutID: layoutID);
297-
log('[DataManager] \tLayout [$layoutID] during init download complete.');
298-
}
288+
// If a [layoutID] was specified for this initialization, then the Future
289+
// callback of this init function must complete once the layout has been
290+
// downloaded.
291+
// Otherwise we need to await for all layouts to be downloaded before
292+
// completing the Future.
293+
if (layoutID != null) {
294+
processDownloadQueue();
295+
} else {
296+
await processDownloadQueue();
297+
}
299298

300-
queuingDone = true;
299+
_logTime(stopwatch);
300+
}
301301

302-
log('[DataManager] All layouts during init download complete.');
303-
}
302+
Future<void> processDownloadQueue() async {
303+
while (_downloadQueue.isNotEmpty) {
304+
final String layoutID = _downloadQueue.removeAt(0);
305+
log('[DataManager] \tDownloading layout [$layoutID]...');
306+
await getOrFetchPopulatedLayout(layoutID: layoutID);
307+
log('[DataManager] \tLayout [$layoutID] during init download complete.');
304308
}
305309

306-
_recordTime(stopwatch);
310+
queuingDone = true;
307311
}
308312

309313
/// Called when the publish model is loaded.
@@ -318,7 +322,10 @@ class DataManager {
318322
}
319323
}
320324

321-
void _recordTime(Stopwatch stopwatch) {
325+
/// This function serves to complete any post initialization steps like
326+
/// indicating that queuing is done and logging the time it took to
327+
/// initialize.
328+
void _logTime(Stopwatch stopwatch) {
322329
stopwatch.stop();
323330
log(
324331
'[DataManager] Initialization took ${stopwatch.elapsedMilliseconds}ms or ${stopwatch.elapsed.inSeconds}s.',
@@ -845,7 +852,7 @@ class DataManager {
845852
bool prioritize = false,
846853
}) async {
847854
if (_publishModel != null && queuingDone) {
848-
log('[DataManager] [queueLayout] Download queue is empty. Downloading layout [$layoutID] immediately...');
855+
log('[DataManager] [queueLayout] No longer queuing. Downloading layout [$layoutID] immediately...');
849856
await getOrFetchPopulatedLayout(layoutID: layoutID);
850857
log('[DataManager] [queueLayout] Layout [$layoutID] download complete.');
851858
} else {

0 commit comments

Comments
 (0)