@@ -32,28 +32,25 @@ module.exports = function() {
3232 fn [ name ] = $require$ [ name ] ;
3333 }
3434 }
35- fn . e = function ( chunkId , callback ) {
35+ fn . e = function ( chunkId ) {
3636 if ( hotStatus === "ready" )
3737 hotSetStatus ( "prepare" ) ;
3838 hotChunksLoading ++ ;
39- $require$ . e ( chunkId , function ( ) {
40- try {
41- callback . call ( null , fn ) ;
42- } finally {
43- finishChunkLoading ( ) ;
44- }
45- function finishChunkLoading ( ) {
46- hotChunksLoading -- ;
47- if ( hotStatus === "prepare" ) {
48- if ( ! hotWaitingFilesMap [ chunkId ] ) {
49- hotEnsureUpdateChunk ( chunkId ) ;
50- }
51- if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
52- hotUpdateDownloaded ( ) ;
53- }
39+ return $require$ . e ( chunkId ) . then ( finishChunkLoading , function ( err ) {
40+ finishChunkLoading ( ) ;
41+ throw err ;
42+ } ) ;
43+ function finishChunkLoading ( ) {
44+ hotChunksLoading -- ;
45+ if ( hotStatus === "prepare" ) {
46+ if ( ! hotWaitingFilesMap [ chunkId ] ) {
47+ hotEnsureUpdateChunk ( chunkId ) ;
48+ }
49+ if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
50+ hotUpdateDownloaded ( ) ;
5451 }
5552 }
56- } ) ;
53+ }
5754 } ;
5855 return fn ;
5956 }
@@ -134,27 +131,19 @@ module.exports = function() {
134131 var hotWaitingFilesMap = { } ;
135132 var hotRequestedFilesMap = { } ;
136133 var hotAvailibleFilesMap = { } ;
137- var hotCallback ;
134+ var hotDeferred ;
138135
139136 // The update info
140137 var hotUpdate , hotUpdateNewHash ;
141138
142- function hotCheck ( apply , callback ) {
139+ function hotCheck ( apply ) {
143140 if ( hotStatus !== "idle" ) throw new Error ( "check() is only allowed in idle status" ) ;
144- if ( typeof apply === "function" ) {
145- hotApplyOnUpdate = false ;
146- callback = apply ;
147- } else {
148- hotApplyOnUpdate = apply ;
149- callback = callback || function ( err ) { if ( err ) throw err ; } ;
150- }
141+ hotApplyOnUpdate = apply ;
151142 hotSetStatus ( "check" ) ;
152- hotDownloadManifest ( function ( err , update ) {
153- if ( err ) return callback ( err ) ;
143+ return hotDownloadManifest ( ) . then ( function ( update ) {
154144 if ( ! update ) {
155145 hotSetStatus ( "idle" ) ;
156- callback ( null , null ) ;
157- return ;
146+ return null ;
158147 }
159148
160149 hotRequestedFilesMap = { } ;
@@ -165,7 +154,12 @@ module.exports = function() {
165154 hotUpdateNewHash = update . h ;
166155
167156 hotSetStatus ( "prepare" ) ;
168- hotCallback = callback ;
157+ var promise = new Promise ( function ( resolve , reject ) {
158+ hotDeferred = {
159+ resolve : resolve ,
160+ reject : reject
161+ } ;
162+ } ) ;
169163 hotUpdate = { } ;
170164 /*foreachInstalledChunks*/ { // eslint-disable-line no-lone-blocks
171165 /*globals chunkId */
@@ -174,6 +168,7 @@ module.exports = function() {
174168 if ( hotStatus === "prepare" && hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
175169 hotUpdateDownloaded ( ) ;
176170 }
171+ return promise ;
177172 } ) ;
178173 }
179174
@@ -203,33 +198,29 @@ module.exports = function() {
203198
204199 function hotUpdateDownloaded ( ) {
205200 hotSetStatus ( "ready" ) ;
206- var callback = hotCallback ;
207- hotCallback = null ;
208- if ( ! callback ) return ;
201+ var deferred = hotDeferred ;
202+ hotDeferred = null ;
203+ if ( ! deferred ) return ;
209204 if ( hotApplyOnUpdate ) {
210- hotApply ( hotApplyOnUpdate , callback ) ;
205+ hotApply ( hotApplyOnUpdate ) . then ( function ( result ) {
206+ deferred . resolve ( result ) ;
207+ } , function ( err ) {
208+ deferred . reject ( err ) ;
209+ } ) ;
211210 } else {
212211 var outdatedModules = [ ] ;
213212 for ( var id in hotUpdate ) {
214213 if ( Object . prototype . hasOwnProperty . call ( hotUpdate , id ) ) {
215214 outdatedModules . push ( + id ) ;
216215 }
217216 }
218- callback ( null , outdatedModules ) ;
217+ deferred . resolve ( outdatedModules ) ;
219218 }
220219 }
221220
222- function hotApply ( options , callback ) {
221+ function hotApply ( options ) {
223222 if ( hotStatus !== "ready" ) throw new Error ( "apply() is only allowed in ready status" ) ;
224- if ( typeof options === "function" ) {
225- callback = options ;
226- options = { } ;
227- } else if ( options && typeof options === "object" ) {
228- callback = callback || function ( err ) { if ( err ) throw err ; } ;
229- } else {
230- options = { } ;
231- callback = callback || function ( err ) { if ( err ) throw err ; } ;
232- }
223+ options = options || { } ;
233224
234225 function getAffectedStuff ( module ) {
235226 var outdatedModules = [ module ] ;
@@ -289,11 +280,11 @@ module.exports = function() {
289280 if ( options . ignoreUnaccepted )
290281 continue ;
291282 hotSetStatus ( "abort" ) ;
292- return callback ( new Error ( "Aborted because " + moduleId + " is not accepted" ) ) ;
283+ return Promise . reject ( new Error ( "Aborted because " + moduleId + " is not accepted" ) ) ;
293284 }
294285 if ( result instanceof Error ) {
295286 hotSetStatus ( "abort" ) ;
296- return callback ( result ) ;
287+ return Promise . reject ( result ) ;
297288 }
298289 appliedUpdate [ moduleId ] = hotUpdate [ moduleId ] ;
299290 addAllToSet ( outdatedModules , result [ 0 ] ) ;
@@ -426,10 +417,10 @@ module.exports = function() {
426417 // handle errors in accept handlers and self accepted module load
427418 if ( error ) {
428419 hotSetStatus ( "fail" ) ;
429- return callback ( error ) ;
420+ return Promise . reject ( error ) ;
430421 }
431422
432423 hotSetStatus ( "idle" ) ;
433- callback ( null , outdatedModules ) ;
424+ Promise . resolve ( outdatedModules ) ;
434425 }
435- } ;
426+ } ;
0 commit comments