Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include only some files #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions hot-reload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const includes = []

const filesInDirectory = dir => new Promise (resolve =>

dir.createReader ().readEntries (entries =>
Expand All @@ -13,9 +15,15 @@ const filesInDirectory = dir => new Promise (resolve =>
)
)

const timestampForFilesInDirectory = dir =>
filesInDirectory (dir).then (files =>
files.map (f => f.name + f.lastModifiedDate).join ())
const timestampForFilesInDirectory = dir => {
return filesInDirectory (dir).then (files => {
if (includes.length && !includes.every(i => files.some(f => i === f.name))) return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please clarify what that line does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xpl It checks if the user has specified any required includes, and if so, checks if not all of the files specified for inclusion actually exist in this timeout cycle - if any of the specified includes don't exist, it short-circuits so that we will wait for the next cycle to check again, hoping all the required files will exist then.


return files
.filter(f => includes.length ? includes.includes(f.name) : true)
.map (f => f.name + f.lastModifiedDate).join ()
})
}

const reload = () => {

Expand All @@ -31,7 +39,7 @@ const watchChanges = (dir, lastTimestamp) => {

timestampForFilesInDirectory (dir).then (timestamp => {

if (!lastTimestamp || (lastTimestamp === timestamp)) {
if (!timestamp || !lastTimestamp || (lastTimestamp === timestamp)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases timestamp is nonexistent?

Copy link
Author

@AndersDJohnson AndersDJohnson May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xpl Since we generate timestamp from the set of files filtered by includes, if none of the included files exist, it may end up an empty string ('' from joining []), which is falsy. In this case, since none of the required files exist, we want to execute the timeout again, to wait for them to exist before reloading.


setTimeout (() => watchChanges (dir, timestamp), 1000) // retry after 1s

Expand All @@ -50,3 +58,10 @@ chrome.management.getSelf (self => {
chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
}
})

if (typeof module === 'object') {
exports.include = (files) => {
includes.push(...files)
}
}