@@ -9,7 +9,12 @@ import {
99 rel ,
1010} from './constants' ;
1111import type { BundleImport , BundleImports } from './types' ;
12- import { BundleImportState } from './types' ;
12+ import {
13+ BundleImportState_None ,
14+ BundleImportState_Queued ,
15+ BundleImportState_Preload ,
16+ BundleImportState_Loaded ,
17+ } from './types' ;
1318
1419export const bundles : BundleImports = new Map ( ) ;
1520let queueDirty : boolean ;
@@ -81,7 +86,8 @@ export const trigger = () => {
8186 Math . max ( 1 , config [ maxSimultaneousPreloadsStr ] * probability )
8287 : // While the graph is not available, we limit to 2 preloads
8388 2 ;
84- if ( preloadCount < allowedPreloads ) {
89+ // When we're 100% sure, everything needs to be queued
90+ if ( probability === 1 || preloadCount < allowedPreloads ) {
8591 queue . shift ( ) ;
8692 preloadOne ( bundle ) ;
8793 } else {
@@ -93,7 +99,7 @@ export const trigger = () => {
9399 * for other resources, so we cycle between 4 and 10 outstanding modulepreloads.
94100 */
95101 if ( config . DEBUG && ! queue . length ) {
96- const loaded = [ ...bundles . values ( ) ] . filter ( ( b ) => b . $state$ > BundleImportState . None ) ;
102+ const loaded = [ ...bundles . values ( ) ] . filter ( ( b ) => b . $state$ > BundleImportState_None ) ;
97103 const waitTime = loaded . reduce ( ( acc , b ) => acc + b . $waitedMs$ , 0 ) ;
98104 const loadTime = loaded . reduce ( ( acc , b ) => acc + b . $loadedMs$ , 0 ) ;
99105 log (
@@ -103,16 +109,20 @@ export const trigger = () => {
103109} ;
104110
105111const preloadOne = ( bundle : BundleImport ) => {
106- if ( bundle . $state$ >= BundleImportState . Preload ) {
112+ if ( bundle . $state$ >= BundleImportState_Preload ) {
107113 return ;
108114 }
109115 preloadCount ++ ;
110116
111117 const start = Date . now ( ) ;
112118 bundle . $waitedMs$ = start - bundle . $createdTs$ ;
113- bundle . $state$ = BundleImportState . Preload ;
119+ bundle . $state$ = BundleImportState_Preload ;
114120
115- config . DEBUG && log ( `<< load after ${ `${ bundle . $waitedMs$ } ms` } ` , bundle . $name$ ) ;
121+ config . DEBUG &&
122+ log (
123+ `<< load ${ Math . round ( ( 1 - bundle . $inverseProbability$ ) * 100 ) } % after ${ `${ bundle . $waitedMs$ } ms` } ` ,
124+ bundle . $name$
125+ ) ;
116126
117127 const link = doc . createElement ( 'link' ) ;
118128 link . href = bundle . $url$ ! ;
@@ -124,7 +134,7 @@ const preloadOne = (bundle: BundleImport) => {
124134 preloadCount -- ;
125135 const end = Date . now ( ) ;
126136 bundle . $loadedMs$ = end - start ;
127- bundle . $state$ = BundleImportState . Loaded ;
137+ bundle . $state$ = BundleImportState_Loaded ;
128138 config . DEBUG && log ( `>> done after ${ bundle . $loadedMs$ } ms` , bundle . $name$ ) ;
129139 // Keep the <head> clean
130140 link . remove ( ) ;
@@ -151,11 +161,11 @@ export const adjustProbabilities = (
151161 }
152162
153163 if (
154- bundle . $state$ < BundleImportState . Preload &&
164+ bundle . $state$ < BundleImportState_Preload &&
155165 bundle . $inverseProbability$ < config [ maxSignificantInverseProbabilityStr ]
156166 ) {
157- if ( bundle . $state$ === BundleImportState . None ) {
158- bundle . $state$ = BundleImportState . Queued ;
167+ if ( bundle . $state$ === BundleImportState_None ) {
168+ bundle . $state$ = BundleImportState_Queued ;
159169 queue . push ( bundle ) ;
160170 config . DEBUG &&
161171 log ( `queued ${ Math . round ( ( 1 - bundle . $inverseProbability$ ) * 100 ) } %` , bundle . $name$ ) ;
0 commit comments