perf: move indexeddb operations to async module (#813)

This commit is contained in:
Nolan Lawson 2018-12-15 17:13:40 -08:00 committed by GitHub
parent 89566cacaa
commit dbd6c35a88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,19 @@
// All database functions are asynchronous, so we can just proxy here and
// put an async import of the database, to avoid including it in the main bundle
// (which doesn't need to run when the user isn't logged in).
import { importDatabase } from '../_utils/asyncModules'
const handler = {
get: function (obj, prop) {
return async function (...args) {
if (!obj[prop]) {
let database = await importDatabase()
obj[prop] = database[prop]
}
return obj[prop].apply(null, args)
}
}
}
export const asyncDatabase = new Proxy({}, handler)

View file

@ -1,3 +1,5 @@
// this used to be workerized, hence the API looks like this // The proxy stuff doesn't play well with IDEs, so use this as
// the dev mode database.js, but swap out the other one in Webpack.
import * as database from './databaseApis' import * as database from './databaseApis'
export { database } export { database }

View file

@ -0,0 +1,2 @@
import { asyncDatabase } from './asyncDatabase'
export { asyncDatabase as database }

View file

@ -47,3 +47,7 @@ export const importStatusVirtualListItem = () => import(
export const importNotificationVirtualListItem = () => import( export const importNotificationVirtualListItem = () => import(
/* webpackChunkName: 'NotificationVirtualListItem.html' */ '../_components/timeline/NotificationVirtualListItem.html' /* webpackChunkName: 'NotificationVirtualListItem.html' */ '../_components/timeline/NotificationVirtualListItem.html'
).then(getDefault) ).then(getDefault)
export const importDatabase = () => import(
/* webpackChunkName: 'database.js' */ '../_database/databaseApis.js'
)

View file

@ -60,6 +60,10 @@ module.exports = {
} }
}, },
plugins: [ plugins: [
new webpack.NormalModuleReplacementPlugin(
/\/_database\/database\.js$/, // this version plays nicer with IDEs
'./database.prod.js'
),
new LodashModuleReplacementPlugin({ new LodashModuleReplacementPlugin({
paths: true paths: true
}) })