Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(server/ssr): wait for stats file before reading it using chokidar
Browse files Browse the repository at this point in the history
feat(server/ssr): wait for stats file before reading it using chokidar
  • Loading branch information
Metnew committed Nov 6, 2017
1 parent ef32b45 commit a968954
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/server/ssr/stats.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// @flow
import fs from 'fs'
import chokidar from 'chokidar'

let cache = {}

async function getFile (path) {
// Cache file forever
if (cache[path]) {
console.log('from cache')
return Promise.resolve(cache[path])
}

return new Promise((resolve, reject) => {
// Give Webpack some time on first stats generation
setTimeout(() => {
// This `readFile` func is looking like it escaped from procedure programming
const readFile = () => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
throw err
Expand All @@ -21,7 +21,17 @@ async function getFile (path) {
cache[path] = json
resolve(json)
})
}, 4000)
}
// does file exist?
fs.access(path, fs.constants.R_OK, err => {
if (err) {
// No. Watch for changes, resolve on `add`.
chokidar.watch(path).on('add', readFile)
} else {
// Yes. resolve!
readFile()
}
})
})
}

Expand Down

0 comments on commit a968954

Please sign in to comment.