Skip to content
Permalink
Browse files

fix(db): resolve Dexie error on app startup

Initialise Dexie in the app preload script. This ensures that it is
initialised as early as possible and resolves an error that was caused
by trying to include the library from the app index file. It's also
cleaner this way as it consolidates our app initialisation code into a
single place.
  • Loading branch information...
mrfelton committed Mar 22, 2019
1 parent e9d6768 commit 4dbeccc46672dc30eec02b183beb31d3bc79b6a4
Showing with 85 additions and 28 deletions.
  1. +0 −8 app/index.js
  2. +3 −7 app/lib/utils/db.js
  3. +8 −2 app/main.js
  4. +25 −10 app/preload.js
  5. +2 −1 package.json
  6. +47 −0 webpack/webpack.config.preload.dev.js
@@ -5,21 +5,13 @@ import jstz from 'jstimezonedetect'
import { configureStore, history } from './store/configureStore'
import { getDefaultLocale } from './lib/i18n'
import Root from './containers/Root'
import { getDb } from './store/db'
import { getDbName } from './lib/utils/db'

// Register supported locales.
import './lib/i18n/locale'

// Get translations.
import translations from './lib/i18n/translation'

// Make the db globally accessible.
window.db = getDb(getDbName(CONFIG))

// Initialise the database.
window.db.open()

// Default the locale to English.
const defaultLocale = getDefaultLocale()

@@ -1,8 +1,4 @@
export const getDbName = config => {
const {
db: { namespace, domain },
} = config
const env = process.env.NODE_ENV || 'development'

return [namespace, domain, env].filter(item => Boolean(item)).join('.')
export const getDbName = options => {
const { namespace, domain, environment = 'development' } = options
return [namespace, domain, environment].filter(item => Boolean(item)).join('.')
}
@@ -143,7 +143,13 @@ const fetchSettings = () => {
// Once we have fetched (or failed to fetch) the user settings, destroy the window.
win.on('load-settings-done', () => process.nextTick(() => win.destroy()))

const dbName = getDbName(global.CONFIG)
const { namespace, domain } = global.CONFIG.db
const { NODE_ENV: environment } = process.env
const dbName = getDbName({
namespace,
domain,
environment,
})
mainLog.debug(`Fetching user settings from indexedDb (using database "%s")`, dbName)

return win.webContents
@@ -246,7 +252,7 @@ app.on('ready', async () => {
webPreferences: {
nodeIntegration: false,
preload: process.env.HOT
? path.resolve(__dirname, 'dist', 'preload.prod.js')
? path.resolve(__dirname, 'dist', 'preload.dev.js')
: path.resolve(__dirname, 'preload.prod.js'),
},
})
@@ -13,6 +13,8 @@ import rimraf from 'rimraf'
import isSubDir from './lib/utils/isSubDir'
import { getAllLocalWallets } from './lib/utils/localWallets'
import { validateHost } from './lib/utils/validateHost'
import { getDb } from './store/db'
import { getDbName } from './lib/utils/db'

const fsReadFile = promisify(fs.readFile)
const fsRimraf = promisify(rimraf)
@@ -128,6 +130,28 @@ function getUserDataDir() {
return remote.app.getPath('userData')
}

// Expose global config.
window.CONFIG = CONFIG

// Provide access to whitelisted environment variables.
window.env = Object.keys(process.env)
.filter(key => WHITELISTED_ENV_VARS.includes(key))
.reduce((obj, key) => {
obj[key] = process.env[key]
return obj
}, {})

// Initialise the database and make it globally accessible.
const { namespace, domain } = CONFIG.db
const { NODE_ENV: environment } = process.env
const dbName = getDbName({
namespace,
domain,
environment,
})
window.db = getDb(dbName)
window.db.open()

// Expose a bridging API to by setting an global on `window`.
//
// !CAREFUL! do not expose any functionality or APIs that could compromise the
@@ -147,15 +171,6 @@ window.Zap = {

// Provide access to ipcRenderer.
window.ipcRenderer = ipcRenderer

//Provide access to electron remote
window.showOpenDialog = remote.dialog.showOpenDialog

// Provide access to whitelisted environment variables.
window.env = Object.keys(process.env)
.filter(key => WHITELISTED_ENV_VARS.includes(key))
.reduce((obj, key) => {
obj[key] = process.env[key]
return obj
}, {})

window.CONFIG = CONFIG
@@ -9,12 +9,13 @@
"build-dll": "webpack -r @babel/register --config webpack/webpack.config.renderer.dev.dll.js",
"build-main": "webpack -r @babel/register --config webpack/webpack.config.main.prod.js",
"build-preload": "webpack -r @babel/register --config webpack/webpack.config.preload.prod.js",
"build-preload-dev": "webpack -r @babel/register --config webpack/webpack.config.preload.dev.js",
"build-renderer": "webpack -r @babel/register --config webpack/webpack.config.renderer.prod.js",
"build-renderer-e2e": "webpack -r @babel/register --config webpack/webpack.config.renderer.e2e.js",
"clean": "rimraf node_modules app/node_modules dll app/dist coverage .eslintcache",
"coverage": "open coverage/index.html",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"predev": "npm run build-preload",
"predev": "npm run build-preload-dev",
"dev": "cross-env HOT=1 npm run start-renderer-dev",
"extract-messages": "extract-messages -l=af-ZA,ca-ES,de-DE,es-ES,ga-IE,hr-HR,ja-JP,no-NO,pt-PT,sr-SP,uk-UA,zh-TW,ar-SA,cs-CZ,el-GR,fi-FI,he-IL,hu-HU,ko-KR,pl-PL,ro-RO,sv-SE,vi-VN,bg-BG,da-DK,en,fr-FR,hi-IN,it-IT,nl-NL,pt-BR,ru-RU,tr-TR,zh-CN -o translations -d en --flat true app/**/messages.js",
"fetch-lnd": "node ./scripts/fetch-lnd-for-packaging.js",
@@ -0,0 +1,47 @@
/**
* Webpack config for development electron preload process
*/

import path from 'path'
import merge from 'webpack-merge'
import { EnvironmentPlugin } from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import baseConfig, { rootDir } from './webpack.config.base'

export default merge.smart(baseConfig, {
devtool: 'source-map',

target: 'electron-renderer',

mode: 'development',

entry: {
preload: path.join(rootDir, 'app', 'preload'),
},

output: {
path: path.join(rootDir, 'app', 'dist'),
filename: '[name].dev.js',
},

plugins: [
new EnvironmentPlugin({
NODE_ENV: 'development',
}),

new BundleAnalyzerPlugin({
analyzerMode: process.env.OPEN_ANALYZER === 'true' ? 'server' : 'disabled',
openAnalyzer: process.env.OPEN_ANALYZER === 'true',
}),
],

/**
* Disables webpack processing of __dirname and __filename.
* If you run the bundle in node.js it falls back to these values of node.js.
* https://github.com/webpack/webpack/issues/2010
*/
node: {
__dirname: false,
__filename: false,
},
})

0 comments on commit 4dbeccc

Please sign in to comment.
You can’t perform that action at this time.