Skip to content

Commit

Permalink
fix(svrnm#131): reinitialise the store in options
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonfray committed Mar 21, 2023
1 parent 875c039 commit 502d21a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 79 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 79 additions & 77 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,86 +92,88 @@ document.getElementById('reload-now-button').onclick = () => {
window.chrome.runtime.reload()
}

const store = new Store({
portName: 'DEMO_MONKEY_STORE' // communication port name
})

let commitHash = ''
fetch(window.chrome.runtime.getURL('COMMITHASH'))
.then((r) => {
return r.text()
})
.then((r) => {
commitHash = r
const initPopup = () => {
const store = new Store({
portName: 'DEMO_MONKEY_STORE' // communication port name
})
.catch((e) => {
// Could not fetch COMMITHASH, that should never happen, but we have a safeguard here.
console.log(e)
})
.finally(() => {
console.log('Loading redux store')
store
.ready()
.then(() => {
console.log('Store loaded.')
document.getElementById('backup-message').remove()
const rootElement = document.getElementById('app')

window.store = store

const app = rootElement.getAttribute('data-app')

const root = createRoot(rootElement)

if (store.getState().settings.optionalFeatures.writeLogs) {
connectLogger(store, { source: 'monkey.js' })
}

// updateCurrentPage()

const manifest = new Manifest(window.chrome, commitHash)

logger('debug', `DemoMonkey ${manifest.version()}`).write()

const protocolHandler = new ProtocolHandler('web+mnky:')
protocolHandler
.handle(window.location.search)
.catch((error) => {
logger('error', error).write()
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.hash
)
})
.then((configuration) => {
if (configuration) {
const configurations = store.getState().configurations
store.dispatch({ type: 'ADD_CONFIGURATION', configuration }).then(() => {
const latest = configurations[configurations.length - 1]
store.dispatch({
type: 'SET_CURRENT_VIEW',
view: `configuration/${latest.id}`
})
})
let commitHash = ''
fetch(window.chrome.runtime.getURL('COMMITHASH'))
.then((r) => {
return r.text()
})
.then((r) => {
commitHash = r
})
.catch((e) => {
// Could not fetch COMMITHASH, that should never happen, but we have a safeguard here.
console.log(e)
})
.finally(() => {
console.log('Loading redux store')
store
.ready()
.then(() => {
console.log('Store loaded.')
document.getElementById('backup-message').remove()
const rootElement = document.getElementById('app')
window.store = store
const app = rootElement.getAttribute('data-app')
const root = createRoot(rootElement)
if (store.getState().settings.optionalFeatures.writeLogs) {
connectLogger(store, { source: 'monkey.js' })
}
// updateCurrentPage()
const manifest = new Manifest(window.chrome, commitHash)
logger('debug', `DemoMonkey ${manifest.version()}`).write()
const protocolHandler = new ProtocolHandler('web+mnky:')
protocolHandler
.handle(window.location.search)
.catch((error) => {
logger('error', error).write()
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.hash
)
}
})
.finally(() => {
switch (app) {
case 'OptionsPageApp':
renderOptionsPageApp(root, store)
break
default:
renderPopupPageApp(root, store, manifest)
}
})
})
.catch((e) => {
console.log(e)
})
})
})
.then((configuration) => {
if (configuration) {
const configurations = store.getState().configurations
store.dispatch({ type: 'ADD_CONFIGURATION', configuration }).then(() => {
const latest = configurations[configurations.length - 1]
store.dispatch({
type: 'SET_CURRENT_VIEW',
view: `configuration/${latest.id}`
})
})
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.hash
)
}
})
.finally(() => {
switch (app) {
case 'OptionsPageApp':
renderOptionsPageApp(root, store)
break
default:
renderPopupPageApp(root, store, manifest)
}
})
})
.catch((e) => {
console.log(e)
})
})
}

initPopup()

window.chrome.runtime.onMessage.addListener((req) => {
if (req.type === 'STORE_INITIALIZED') {
// Initializes the popup and options logic
initPopup()
}
})
1 change: 1 addition & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ try {
run(state)
isInitialized = true
}
scope.chrome.runtime.sendMessage({ type: 'STORE_INITIALIZED' })
})
}
init()
Expand Down

0 comments on commit 502d21a

Please sign in to comment.