Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: app lazy-loads app-context #2378

Merged
merged 16 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 53 additions & 28 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"scripts": {
"start": "cross-env NODE_ENV=development electron .",
"lint": "ts-standard && standard",
"lint:fix": "ts-standard --fix && standard --fix",
"test": "cross-env NODE_ENV=test playwright test 'test/unit/.*.spec.js'",
"test:e2e": "xvfb-maybe cross-env NODE_ENV=test playwright test test/e2e/launch.e2e.test.js",
"test:e2e": "xvfb-maybe cross-env NODE_ENV=test playwright test -c test/e2e/playwright.config.js",
"postinstall": "run-s install-app-deps patch-deps",
"install-app-deps": "electron-builder install-app-deps",
"patch-deps": "patch-package",
Expand Down Expand Up @@ -49,7 +50,7 @@
"homepage": "https://github.com/ipfs-shipyard/ipfs-desktop",
"devDependencies": {
"@electron/notarize": "^1.2.3",
"@playwright/test": "^1.24.0",
"@playwright/test": "^1.35.1",
"cross-env": "^7.0.3",
"dotenv": "^16.0.0",
"electron": "^19.1.9",
Expand All @@ -58,7 +59,7 @@
"ipfs-or-gateway": "^4.1.0",
"npm-run-all": "^4.1.5",
"patch-package": "^6.5.0",
"playwright": "^1.24.0",
"playwright": "^1.35.1",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"semver-regex": "3.1.4",
Expand Down
7 changes: 6 additions & 1 deletion src/add-to-ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs-extra')
const logger = require('./common/logger')
const { notify, notifyError } = require('./common/notify')
const { analyticsKeys } = require('./analytics/keys')
const getCtx = require('./context')

async function copyFileToMfs (ipfs, cid, filename) {
let i = 0
Expand Down Expand Up @@ -76,6 +77,7 @@ function sendNotification (launchWebUI, hasFailures, successCount, filename) {
body = i18n.t('itemsFailedNotification.message')
}

// @ts-expect-error
fn({ title, body }, () => {
// force refresh for Files screen to pick up newly added items
// https://github.com/ipfs/ipfs-desktop/issues/1763
Expand Down Expand Up @@ -109,7 +111,9 @@ async function addFileOrDirectory (ipfs, filepath) {
return { cid, filename }
}

module.exports = async function ({ getIpfsd, launchWebUI }, files) {
module.exports = async function (files) {
const ctx = getCtx()
const getIpfsd = await ctx.getProp('getIpfsd')
const ipfsd = await getIpfsd()
if (!ipfsd) {
return
Expand All @@ -136,6 +140,7 @@ module.exports = async function ({ getIpfsd, launchWebUI }, files) {
}

const { cid, filename } = await getShareableCid(ipfsd.api, successes)
const launchWebUI = ctx.getFn('launchWebUI')
sendNotification(launchWebUI, failures.length !== 0, successes.length, filename)

const query = filename ? `?filename=${encodeURIComponent(filename)}` : ''
Expand Down
10 changes: 8 additions & 2 deletions src/analytics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ const { join } = require('path')
const { app } = require('electron')
const { existsSync, mkdirSync } = require('fs')
const ipcMainEvents = require('../common/ipc-main-events')
const logger = require('../common/logger')
const getCtx = require('../context')

module.exports = async function (ctx) {
module.exports = async function () {
logger.info('[analytics] init...')
// workaround: recursive mkdir https://github.com/Countly/countly-sdk-nodejs/pull/14
const countlyDataDir = join(app.getPath('userData'), 'countly-data')
if (!existsSync(countlyDataDir)) {
mkdirSync(countlyDataDir, { recursive: true })
}

// @ts-expect-error
Countly.init({
url: 'https://countly.ipfs.io',
app_key: COUNTLY_KEY,
Expand All @@ -23,7 +27,8 @@ module.exports = async function (ctx) {
storage_path: countlyDataDir
})

ctx.countlyDeviceId = Countly.device_id
// @ts-expect-error
getCtx().setProp('countlyDeviceId', Countly.device_id)

ipcMain.on(ipcMainEvents.COUNTLY_ADD_CONSENT, (_, consent) => {
Countly.add_consent(consent)
Expand All @@ -32,4 +37,5 @@ module.exports = async function (ctx) {
ipcMain.on(ipcMainEvents.COUNTLY_REMOVE_CONSENT, (_, consent) => {
Countly.remove_consent(consent)
})
logger.info('[analytics] init done')
}
3 changes: 3 additions & 0 deletions src/app-menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { app, Menu, shell } = require('electron')
const logger = require('./common/logger')

const template = [
{
Expand Down Expand Up @@ -88,6 +89,8 @@ if (process.platform === 'darwin') {
}

module.exports = function () {
logger.info('[appMenu] init...')
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
logger.info('[appMenu] init done...')
}
8 changes: 4 additions & 4 deletions src/argv-files-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs-extra')
const addToIpfs = require('./add-to-ipfs')

async function argvHandler (argv, ctx) {
async function argvHandler (argv) {
let handled = false

const files = []
Expand All @@ -18,16 +18,16 @@ async function argvHandler (argv, ctx) {
}

if (files.length > 0) {
await addToIpfs(ctx, files)
await addToIpfs(files)
handled = true
}

return handled
}

module.exports = async function (ctx) {
module.exports = async function () {
// Checks current proccess
await argvHandler(process.argv, ctx)
await argvHandler(process.argv)
}

module.exports.argvHandler = argvHandler
1 change: 1 addition & 0 deletions src/auto-launch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
const { app } = require('electron')
const i18n = require('i18next')
const os = require('os')
Expand Down
23 changes: 13 additions & 10 deletions src/auto-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = require('../common/logger')
const { showDialog } = require('../dialogs')
const { IS_MAC, IS_WIN, IS_APPIMAGE } = require('../common/consts')
const ipcMainEvents = require('../common/ipc-main-events')
const getCtx = require('../context')

function isAutoUpdateSupported () {
// atm only macOS, windows and AppImage builds support autoupdate mechanism,
Expand All @@ -16,7 +17,8 @@ function isAutoUpdateSupported () {
let updateNotification = null // must be a global to avoid gc
let feedback = false

function setup (ctx) {
function setup () {
const ctx = getCtx()
// we download manually in 'update-available'
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = true
Expand Down Expand Up @@ -138,14 +140,15 @@ function setup (ctx) {
updateNotification.show()
}
})
const stopIpfs = ctx.getFn('stopIpfs')

// In some cases before-quit event is not emitted before all windows are closed,
// and we need to do cleanup here
const beforeQuitCleanup = async () => {
BrowserWindow.getAllWindows().forEach(w => w.removeAllListeners('close'))
app.removeAllListeners('window-all-closed')
try {
const s = await ctx.stopIpfs()
const s = await stopIpfs()
logger.info(`[beforeQuitCleanup] stopIpfs had finished with status: ${s}`)
} catch (err) {
logger.error('[beforeQuitCleanup] stopIpfs had an error', err)
Expand All @@ -166,33 +169,33 @@ async function checkForUpdates () {
ipcMain.emit(ipcMainEvents.UPDATING_ENDED)
}

module.exports = async function (ctx) {
module.exports = async function () {
if (['test', 'development'].includes(process.env.NODE_ENV ?? '')) {
ctx.manualCheckForUpdates = () => {
getCtx().setProp('manualCheckForUpdates', () => {
showDialog({
title: 'Not available in development',
message: 'Yes, you called this function successfully.',
buttons: [i18n.t('close')]
})
}
})
return
}
if (!isAutoUpdateSupported()) {
ctx.manualCheckForUpdates = () => {
getCtx().setProp('manualCheckForUpdates', () => {
shell.openExternal('https://github.com/ipfs-shipyard/ipfs-desktop/releases/latest')
}
})
return
}

setup(ctx)
setup()

checkForUpdates() // background check

setInterval(checkForUpdates, 43200000) // every 12 hours

// enable on-demand check via About submenu
ctx.manualCheckForUpdates = () => {
getCtx().setProp('manualCheckForUpdates', () => {
feedback = true
checkForUpdates()
}
})
}