-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
page-hot-reloader.js
59 lines (50 loc) · 1.52 KB
/
page-hot-reloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { emitter, store } = require(`../redux`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const { boundActionCreators } = require(`../redux/actions`)
const { deletePage, deleteComponentsDependencies } = boundActionCreators
let pagesDirty = false
let graphql
emitter.on(`CREATE_NODE`, action => {
if (action.payload.internal.type !== `SitePage`) {
pagesDirty = true
}
})
emitter.on(`DELETE_NODE`, action => {
if (action.payload.internal.type !== `SitePage`) {
pagesDirty = true
// Make a fake API call to trigger `API_RUNNING_QUEUE_EMPTY` being called.
// We don't want to call runCreatePages here as there might be work in
// progress. So this is a safe way to make sure runCreatePages gets called
// at a safe time.
apiRunnerNode(`FAKE_API_CALL`)
}
})
emitter.on(`API_RUNNING_QUEUE_EMPTY`, () => {
if (pagesDirty) {
runCreatePages()
}
})
const runCreatePages = async () => {
pagesDirty = false
const timestamp = Date.now()
await apiRunnerNode(`createPages`, {
graphql,
traceId: `createPages`,
waitForCascadingActions: true,
})
// Delete pages that weren't updated when running createPages.
Array.from(store.getState().pages.values()).forEach(page => {
if (
!page.isCreatedByStatefulCreatePages &&
page.updatedAt < timestamp &&
page.path !== `/404.html`
) {
deleteComponentsDependencies([page.path])
deletePage(page)
}
})
emitter.emit(`CREATE_PAGE_END`)
}
module.exports = graphqlRunner => {
graphql = graphqlRunner
}