Skip to content

Commit 49fed51

Browse files
platform reload
1 parent a5171ed commit 49fed51

File tree

7 files changed

+29
-36
lines changed

7 files changed

+29
-36
lines changed

constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ exports.WAKEUP = toPath(new URL(BIN + WAKEUP_EXEC, swapURL))
7070
exports.RUNTIME = toPath(new URL(BIN + RUNTIME_EXEC, swapURL))
7171
exports.DESKTOP_RUNTIME = toPath(new URL(BIN + DESKTOP_EXEC, swapURL))
7272

73-
exports.BARE_RESTART_EXIT_CODE = 75
73+
exports.RESTART_EXIT_CODE = 75
7474

7575
function electronModuleURL () {
7676
const u = pathToFileURL(process.execPath)

electron-main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ async function electronMain (cmd) {
5050
electron.ipcMain.on('send-to', (e, id, channel, message) => { electron.webContents.fromId(id)?.send(channel, message) })
5151

5252
const app = await gui.app()
53-
app.unloading().then(async () => {
54-
await app.close()
53+
app.unloading().then(async ({ type }) => {
54+
if (type === 'close') await app.close()
5555
}) // note: would be unhandled rejection on failure, but should never fail
5656
}
5757

gui/preload.js

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ module.exports = class PearGUI extends ReadyResource {
4545
desktopSources: (options = {}) => ipc.desktopSources(options)
4646
}
4747

48+
ipc.messages({ type: 'pear/reload' }).once('data', async ({ hard }) => {
49+
await ipc.close()
50+
if (hard === true) await ipc.waitForLock()
51+
location.reload()
52+
})
53+
4854
const kGuiCtrl = Symbol('gui:ctrl')
4955

5056
class Parent extends EventEmitter {
@@ -213,6 +219,11 @@ module.exports = class PearGUI extends ReadyResource {
213219
this.View = View
214220
}
215221

222+
reload = (opts) => {
223+
if (opts?.platform) return super.reload()
224+
location.reload()
225+
}
226+
216227
exit = (code) => {
217228
process.exitCode = code
218229
electron.ipcRenderer.sendSync('exit', code)

lib/api.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const Worker = require('./worker')
3-
const { BARE_RESTART_EXIT_CODE } = require('../constants')
3+
const { RESTART_EXIT_CODE } = require('../constants')
44
const noop = () => {}
55
const teardown = global.Bare ? require('./teardown') : noop
66
const program = global.Bare || global.process
@@ -102,15 +102,8 @@ class API {
102102
}
103103

104104
reload = async (opts = {}) => {
105-
if (!opts.platform) {
106-
// TODO: use Pear.shutdown when it lands instead
107-
if (this.#state.type === 'terminal') Bare.exit(BARE_RESTART_EXIT_CODE)
108-
else global.location.reload()
109-
110-
return
111-
}
112-
113-
return this.#reftrack(this.#ipc.restart({ ...opts, hard: false }))
105+
if (opts.platform) return this.#reftrack(this.#ipc.restart({ ...opts, hard: false }))
106+
this.exit(RESTART_EXIT_CODE)
114107
}
115108

116109
updates = (listener) => this.messages({ type: 'pear/updates' }, listener)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"paparam": "^1.4.0",
9595
"pear-changelog": "^1.0.1",
9696
"pear-interface": "^1.0.0",
97-
"pear-ipc": "^1.0.17",
97+
"pear-ipc": "^1.1.0",
9898
"pear-link": "^2.0.1",
9999
"pear-updater": "^3.1.0",
100100
"protomux": "^3.6.0",

run/index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ const {
1919
} = require('../errors')
2020
const parseLink = require('../lib/parse-link')
2121
const teardown = require('../lib/teardown')
22-
const { PLATFORM_LOCK } = require('../constants')
23-
const fsext = require('fs-native-extensions')
22+
const { isWindows } = require('which-runtime')
2423

2524
module.exports = async function run ({ ipc, args, cmdArgs, link, storage, detached, flags, appArgs, indices }) {
2625
const { drive, pathname } = parseLink(link)
@@ -116,15 +115,10 @@ module.exports = async function run ({ ipc, args, cmdArgs, link, storage, detach
116115

117116
global.Pear = pear
118117

119-
const reloadSubscriber = ipc.messages({ type: 'pear/reload' })
120-
reloadSubscriber.on('data', async () => {
121-
ipc.stream.destroy()
122-
123-
const fd = await new Promise((resolve, reject) => fs.open(PLATFORM_LOCK, 'r+', (err, fd) => err ? reject(err) : resolve(fd)))
124-
await fsext.waitForLock(fd)
125-
await new Promise((resolve, reject) => fs.close(fd, (err) => err ? reject(err) : resolve(fd)))
126-
127-
await global.Pear.restart()
118+
ipc.messages({ type: 'pear/reload' }).once('data', async ({ hard }) => {
119+
await ipc.close()
120+
if (hard === true) await ipc.waitForLock()
121+
await global.Pear.reload()
128122
})
129123

130124
const protocol = new Module.Protocol({

subsystems/sidecar/index.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ class Sidecar extends ReadyResource {
515515

516516
async restart ({ platform = false, hard = true } = {}, client) {
517517
if (this.verbose) console.log(`${hard ? 'Hard' : 'Soft'} restarting ${platform ? 'platform' : 'client'}`)
518-
if (platform === false) {
518+
if (hard === true && platform === false) {
519519
const { dir, cwd, cmdArgs, env } = client.userData.state
520520
const appling = client.userData.state.appling
521521
const opts = { cwd, env, detached: true, stdio: 'ignore' }
@@ -553,24 +553,19 @@ class Sidecar extends ReadyResource {
553553
return
554554
}
555555

556-
if (!hard && this.hasClients) {
557-
const seen = new Set()
558-
for (const { userData: app } of this.clients) {
559-
if (!app.state || seen.has(app.state.id)) continue
560-
seen.add(app.state.id)
561-
app.message({ type: 'pear/reload' })
562-
}
563-
}
556+
for (const { userData: app } of this.clients) app.message({ type: 'pear/reload', hard })
564557

565558
const sidecarClosed = new Promise((resolve) => this.corestore.once('close', resolve))
566559
let restarts = await this.#shutdown(client)
560+
567561
// ample time for any OS cleanup operations:
568562
await new Promise((resolve) => setTimeout(resolve, 1500))
563+
564+
if (hard === false) return
565+
569566
// shutdown successful, reset death clock
570567
this.deathClock()
571568

572-
if (!hard) return
573-
574569
restarts = restarts.filter(({ run }) => run)
575570
if (restarts.length === 0) return
576571
if (this.verbose) console.log('Restarting', restarts.length, 'apps')

0 commit comments

Comments
 (0)