Skip to content

Commit

Permalink
feat(dev-loader): retry mechanism with timeout and hmr trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 27, 2020
1 parent 6b399f5 commit 4cd850e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions packages/gatsby/cache-dir/dev-loader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import { BaseLoader, PageResourceStatus } from "./loader"
import { findPath } from "./find-path"
import * as pDefer from "p-defer"

const WAITING_FOR_HOT_UPDATE_TIMEOUT = 1000
const COMPONENT_GRABBING_RETRY_LIMIT = 5

class DevLoader extends BaseLoader {
waitingForHotUpdatePromise = pDefer()

constructor(syncRequires, matchPaths) {
const loadComponent = (chunkName, key = `components`) =>
Promise.resolve(this.syncRequires[key][chunkName])
const loadComponent = (chunkName, key = `components`, retry = 0) => {
const module = this.syncRequires[key][chunkName]

if (!module && retry < COMPONENT_GRABBING_RETRY_LIMIT) {
return new Promise(resolve => {
const checkAgain = () => loadComponent(chunkName, key, retry + 1)

let waitingForHotUpdate = setTimeout(() => {
waitingForHotUpdate = undefined
resolve(checkAgain())
}, WAITING_FOR_HOT_UPDATE_TIMEOUT)

this.waitingForHotUpdatePromise.promise.then(() => {
if (waitingForHotUpdate) {
clearTimeout(waitingForHotUpdate)
resolve(checkAgain())
}
})
})
}

return Promise.resolve(module)
}

super(loadComponent, matchPaths)

Expand Down Expand Up @@ -46,6 +73,8 @@ class DevLoader extends BaseLoader {

updateSyncRequires(syncRequires) {
this.syncRequires = syncRequires
this.waitingForHotUpdatePromise.resolve()
this.waitingForHotUpdatePromise = pDefer()
}
}

Expand Down

0 comments on commit 4cd850e

Please sign in to comment.