-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathstore.js
45 lines (38 loc) · 1.4 KB
/
store.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
'use strict'
/* eslint-env browser, webextensions */
import browser from 'webextension-polyfill'
import { optionDefaults } from '../lib/options.js'
import createRuntimeChecks from '../lib/runtime-checks.js'
import { handleConsentFromState, trackView } from '../lib/telemetry.js'
// The store contains and mutates the state for the app
export default function optionStore (state, emitter) {
state.options = optionDefaults
const updateStateOptions = async () => {
const runtime = await createRuntimeChecks(browser)
state.withNodeFromBrave = runtime.brave && await runtime.brave.getIPFSEnabled()
/**
* FIXME: Why are we setting `state.options` when state is supposed to extend options?
*/
state.options = await getOptions()
emitter.emit('render')
}
emitter.on('DOMContentLoaded', async () => {
handleConsentFromState(state)
trackView('options')
updateStateOptions()
browser.storage.onChanged.addListener(updateStateOptions)
})
emitter.on('optionChange', ({ key, value }) => (
browser.storage.local.set({ [key]: value })
))
emitter.on('optionsReset', () => (
browser.storage.local.set(optionDefaults)
))
}
async function getOptions () {
const storedOpts = await browser.storage.local.get()
return Object.keys(optionDefaults).reduce((opts, key) => {
opts[key] = storedOpts[key] == null ? optionDefaults[key] : storedOpts[key]
return opts
}, {})
}