Skip to content

Commit

Permalink
Made tab reload optional - sometimes we have our own code to reinject…
Browse files Browse the repository at this point in the history
… content script necessities, or we don't have content script, and sometimes we don't want the page to refresh
  • Loading branch information
mikob committed May 26, 2020
1 parent 0c18322 commit 1909970
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions hot-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,43 @@ const timestampForFilesInDirectory = dir =>
filesInDirectory (dir).then (files =>
files.map (f => f.name + f.lastModifiedDate).join ())

const reload = () => {
const reload = (reloadTab) => {

chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5
if (reloadTab) {
chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5

if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }
if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }

chrome.runtime.reload ()
})
} else {
chrome.runtime.reload ()
})
}
}

const watchChanges = (dir, lastTimestamp) => {
const watchChanges = (dir, reloadTab, lastTimestamp) => {

timestampForFilesInDirectory (dir).then (timestamp => {

if (!lastTimestamp || (lastTimestamp === timestamp)) {

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

} else {

reload ()
reload (reloadTab)
}
})

}

chrome.management.getSelf (self => {

if (self.installType === 'development') {

chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
if (typeof module === 'object') {
exports.default = (opts) => {
chrome.management.getSelf (self => {
if (self.installType === 'development') {
chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir, opts.reloadTab))
}
})
}
})
}

0 comments on commit 1909970

Please sign in to comment.