@@ -9,7 +9,12 @@ import {
9
9
rel ,
10
10
} from './constants' ;
11
11
import 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' ;
13
18
14
19
export const bundles : BundleImports = new Map ( ) ;
15
20
let queueDirty : boolean ;
@@ -81,7 +86,8 @@ export const trigger = () => {
81
86
Math . max ( 1 , config [ maxSimultaneousPreloadsStr ] * probability )
82
87
: // While the graph is not available, we limit to 2 preloads
83
88
2 ;
84
- if ( preloadCount < allowedPreloads ) {
89
+ // When we're 100% sure, everything needs to be queued
90
+ if ( probability === 1 || preloadCount < allowedPreloads ) {
85
91
queue . shift ( ) ;
86
92
preloadOne ( bundle ) ;
87
93
} else {
@@ -93,7 +99,7 @@ export const trigger = () => {
93
99
* for other resources, so we cycle between 4 and 10 outstanding modulepreloads.
94
100
*/
95
101
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 ) ;
97
103
const waitTime = loaded . reduce ( ( acc , b ) => acc + b . $waitedMs$ , 0 ) ;
98
104
const loadTime = loaded . reduce ( ( acc , b ) => acc + b . $loadedMs$ , 0 ) ;
99
105
log (
@@ -103,16 +109,20 @@ export const trigger = () => {
103
109
} ;
104
110
105
111
const preloadOne = ( bundle : BundleImport ) => {
106
- if ( bundle . $state$ >= BundleImportState . Preload ) {
112
+ if ( bundle . $state$ >= BundleImportState_Preload ) {
107
113
return ;
108
114
}
109
115
preloadCount ++ ;
110
116
111
117
const start = Date . now ( ) ;
112
118
bundle . $waitedMs$ = start - bundle . $createdTs$ ;
113
- bundle . $state$ = BundleImportState . Preload ;
119
+ bundle . $state$ = BundleImportState_Preload ;
114
120
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
+ ) ;
116
126
117
127
const link = doc . createElement ( 'link' ) ;
118
128
link . href = bundle . $url$ ! ;
@@ -124,7 +134,7 @@ const preloadOne = (bundle: BundleImport) => {
124
134
preloadCount -- ;
125
135
const end = Date . now ( ) ;
126
136
bundle . $loadedMs$ = end - start ;
127
- bundle . $state$ = BundleImportState . Loaded ;
137
+ bundle . $state$ = BundleImportState_Loaded ;
128
138
config . DEBUG && log ( `>> done after ${ bundle . $loadedMs$ } ms` , bundle . $name$ ) ;
129
139
// Keep the <head> clean
130
140
link . remove ( ) ;
@@ -151,11 +161,11 @@ export const adjustProbabilities = (
151
161
}
152
162
153
163
if (
154
- bundle . $state$ < BundleImportState . Preload &&
164
+ bundle . $state$ < BundleImportState_Preload &&
155
165
bundle . $inverseProbability$ < config [ maxSignificantInverseProbabilityStr ]
156
166
) {
157
- if ( bundle . $state$ === BundleImportState . None ) {
158
- bundle . $state$ = BundleImportState . Queued ;
167
+ if ( bundle . $state$ === BundleImportState_None ) {
168
+ bundle . $state$ = BundleImportState_Queued ;
159
169
queue . push ( bundle ) ;
160
170
config . DEBUG &&
161
171
log ( `queued ${ Math . round ( ( 1 - bundle . $inverseProbability$ ) * 100 ) } %` , bundle . $name$ ) ;
0 commit comments