@@ -108,6 +108,7 @@ class DataManager {
108
108
final Stopwatch stopwatch = Stopwatch ()..start ();
109
109
110
110
initialized = true ;
111
+ queuingDone = false ;
111
112
112
113
// Initialize all locally cached data.
113
114
final cachedModel = localDataRepository.fetchPublishModel (
@@ -156,25 +157,21 @@ class DataManager {
156
157
if (success) {
157
158
log ('[DataManager] [slug] Complete publish model from slug is downloaded in background. Emitting.' );
158
159
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 }.' );
167
163
}
168
164
169
- _recordTime (stopwatch);
170
- } catch (e, stackTrace) {
165
+ _logTime (stopwatch);
166
+ return ;
167
+ } catch (e, stackTrace) {
171
168
log ('[DataManager] Error trying to download complete publish model from slug.' );
172
169
log ('[DataManager] Since no publish model is cached, this is a complete stop to the data manager.' );
173
170
log ('[DataManager]' , level: 900 , error: e, stackTrace: stackTrace);
174
171
print (e);
175
172
print (stackTrace);
176
173
177
- _recordTime (stopwatch);
174
+ _logTime (stopwatch);
178
175
179
176
log ('[DataManager] [slug] Failed to download complete publish bundle for slug ${config .slug }.' );
180
177
return ;
@@ -192,7 +189,7 @@ class DataManager {
192
189
193
190
if (authManager.authData == null ) {
194
191
log ('[DataManager] No auth data is available. Continuing as if offline.' );
195
- _recordTime (stopwatch);
192
+ _logTime (stopwatch);
196
193
return ;
197
194
}
198
195
@@ -236,9 +233,7 @@ class DataManager {
236
233
// If the publish model is still null, then we need to wait for the first
237
234
// publish model to arrive from the server via the stream above.
238
235
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.' );
242
237
final model = await firstPublishEvent;
243
238
await onPublishModelLoaded (model);
244
239
_publishModel = model;
@@ -252,58 +247,67 @@ class DataManager {
252
247
'[DataManager] Publish model is still null.\n '
253
248
'Is there a network problem or bad authentication?' ,
254
249
);
255
- _recordTime (stopwatch);
250
+ _logTime (stopwatch);
256
251
return ;
257
252
}
253
+ }
258
254
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
+ }
280
277
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
+ }
285
287
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] \t Downloading layout [$layoutID ]...' );
296
- await getOrFetchPopulatedLayout (layoutID: layoutID);
297
- log ('[DataManager] \t Layout [$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
+ }
299
298
300
- queuingDone = true ;
299
+ _logTime (stopwatch);
300
+ }
301
301
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] \t Downloading layout [$layoutID ]...' );
306
+ await getOrFetchPopulatedLayout (layoutID: layoutID);
307
+ log ('[DataManager] \t Layout [$layoutID ] during init download complete.' );
304
308
}
305
309
306
- _recordTime (stopwatch) ;
310
+ queuingDone = true ;
307
311
}
308
312
309
313
/// Called when the publish model is loaded.
@@ -318,7 +322,10 @@ class DataManager {
318
322
}
319
323
}
320
324
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) {
322
329
stopwatch.stop ();
323
330
log (
324
331
'[DataManager] Initialization took ${stopwatch .elapsedMilliseconds }ms or ${stopwatch .elapsed .inSeconds }s.' ,
@@ -845,7 +852,7 @@ class DataManager {
845
852
bool prioritize = false ,
846
853
}) async {
847
854
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...' );
849
856
await getOrFetchPopulatedLayout (layoutID: layoutID);
850
857
log ('[DataManager] [queueLayout] Layout [$layoutID ] download complete.' );
851
858
} else {
0 commit comments