Skip to content

Commit e5e8448

Browse files
Merge pull request #19 from neuralinterfaces/v0.0.58
0.0.58 Release
2 parents 5511be3 + 4081bfa commit e5e8448

File tree

15 files changed

+92
-62
lines changed

15 files changed

+92
-62
lines changed

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Follow the prompts to select your favorite framework and features.
1313
After running `npm install`, add Commoners as a dependency:
1414

1515
```bash
16-
npm install -D commoners@0.0.57
16+
npm install -D commoners@0.0.58
1717
```
1818

1919
## Configuring the `package.json` File

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
"@commoners/bluetooth": "0.0.55",
2121
"@commoners/custom-protocol": "0.0.55",
2222
"@commoners/serial": "0.0.55",
23-
"@commoners/solidarity": "0.0.57",
23+
"@commoners/solidarity": "0.0.58",
2424
"@commoners/splash-screen": "0.0.55",
25-
"@commoners/testing": "0.0.57",
26-
"@commoners/windows": "0.0.55",
25+
"@commoners/testing": "0.0.58",
26+
"@commoners/windows": "0.0.58",
2727
"@vitest/coverage-v8": "^2.0.3",
2828
"search-insights": "^2.15.0",
29-
"commoners": "0.0.57",
29+
"commoners": "0.0.58",
3030
"vite": "^5.3.4",
3131
"vitepress": "^1.3.1",
3232
"vitest": "^2.0.3"

packages/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "commoners",
33
"description": "Cross-Platform Development for the Rest of Us",
4-
"version": "0.0.57",
4+
"version": "0.0.58",
55
"type": "module",
66
"license": "MIT",
77
"engines": {
@@ -18,7 +18,7 @@
1818
"watch": "vite build --watch"
1919
},
2020
"dependencies": {
21-
"@commoners/solidarity": "0.0.57",
21+
"@commoners/solidarity": "^0.0.58",
2222
"cac": "^6.7.14"
2323
},
2424
"devDependencies": {

packages/core/assets/electron/main.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,9 @@ const runWindowPlugins = async (win: BrowserWindow | null = null, type = 'load',
263263

264264
const __listeners = []
265265

266-
const transferredFlags = {
267-
__id: windowCount,
268-
__main: isMainWindow
269-
}
266+
267+
const __id = windowCount
268+
const transferredFlags = { __id, __main: isMainWindow }
270269

271270
windowCount++
272271

@@ -319,7 +318,7 @@ const runWindowPlugins = async (win: BrowserWindow | null = null, type = 'load',
319318

320319
// ------------------------ Main Window Default Behaviors ------------------------
321320
if (isMainWindow) {
322-
ipcMain.once('commoners:ready', () => {
321+
ipcMain.once(`commoners:ready:${__id}`, () => {
323322
globals.mainWindow = win
324323
globals.firstInitialized = true
325324
readyQueue.forEach(f => f(win))
@@ -355,6 +354,8 @@ const runWindowPlugins = async (win: BrowserWindow | null = null, type = 'load',
355354

356355
win.show() // Allow annotating to skip show
357356

357+
win.__ready = new Promise(resolve => win.once(`commoners:ready:${__id}`, resolve)) // Commoners plugins are all loaded
358+
358359
return win
359360
}
360361

@@ -393,17 +394,20 @@ runAppPlugins().then(() => {
393394
app.whenReady().then(async () => {
394395

395396
// ------------------------ Service Creation ------------------------
396-
const { services: active } = await services.createAll(config.services, {
397+
const { services: active, close: closeService } = await services.createAll(config.services, {
397398
target: 'desktop',
398399
build: isProduction,
399400
root: isProduction ? __dirname : join(__dirname, '..', '..'), // Back out of default outDir
400401
onClosed: (id, code) => serviceSend(id, 'closed', code),
401-
onLog: (id, msg) => serviceSend(id, 'log', msg.toString()),
402+
onLog: (id, msg) => serviceSend(id, 'log', msg.toString())
402403
})
403404

404405
// ------------------------Track Service Status in Windows ------------------------
405406
if (active) {
406-
for (let id in active) serviceOn(id, 'status', (event) => event.returnValue = active[id].status)
407+
for (let id in active) {
408+
serviceOn(id, 'status', (event) => event.returnValue = active[id].status)
409+
serviceOn(id, 'close', () => closeService(id))
410+
}
407411
ipcMain.on('commoners:services', (event) => event.returnValue = services.sanitize(active)) // Expose to renderer process (and ensure URLs are correct)
408412
}
409413

packages/core/assets/electron/preload.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ const TEMP_COMMONERS = {
2828
removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel),
2929
}
3030

31+
// Handle service interactions
3132
for (let id in TEMP_COMMONERS.services) {
3233
const service = TEMP_COMMONERS.services[id]
3334

34-
const serviceStates = {
35-
status: ipcRenderer.sendSync(`services:${id}:status`)
36-
}
35+
service.status = ipcRenderer.sendSync(`services:${id}:status`)
3736

3837
const listeners = {
3938
closed: []
@@ -42,22 +41,24 @@ for (let id in TEMP_COMMONERS.services) {
4241
}
4342

4443
ipcRenderer.on(`services:${id}:log`, (_) => {
45-
if (serviceStates.status) return
46-
serviceStates.status = true
44+
if (service.status) return
45+
service.status = true
4746
})
4847

4948
ipcRenderer.on(`services:${id}:closed`, (_, code) => {
50-
if (serviceStates.status === false) return
51-
serviceStates.status = false
49+
if (service.status === false) return
50+
service.status = false
5251
listeners.closed.forEach(f => f(code))
5352
})
5453

5554
// ---------------- Assign Functions ----------------
5655
service.onClosed = (listener) => {
57-
if (serviceStates.status === false) listener()
56+
if (service.status === false) listener()
5857
listeners.closed.push(listener)
5958
}
6059

60+
service.close = () => ipcRenderer.send(`services:${id}:close`)
61+
6162
}
6263

6364
// Expose ipcRenderer

packages/core/assets/services/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ async function getServiceUrl(service) {
278278
// For Client
279279
url: await getServiceUrl({ src, url, host, port }),
280280

281-
// NOTE: Not in types...
282-
states: null,
281+
status: null,
283282

284283
}
285284

@@ -347,13 +346,12 @@ export async function start(config, id, opts = {}) {
347346

348347
if (childProcess.stderr) childProcess.stderr.on('data', (data) => printServiceMessage(label, data, 'error'));
349348

349+
// Notify of process closure gracefully
350350
childProcess.on('close', (code) => {
351-
if (code !== null) {
352-
config.status = false
353-
if (opts.onClosed) opts.onClosed(id, code)
354-
delete processes[id]
355-
printServiceMessage(label, `Exited with code ${code}`, 'error')
356-
}
351+
config.status = false
352+
if (opts.onClosed) opts.onClosed(id, code)
353+
delete processes[id]
354+
if (code !== null) printServiceMessage(label, `Exited with code ${code}`, 'error')
357355
});
358356

359357
// process.on('close', (code) => code === null ? console.log(chalk.gray(`Restarting ${label}...`)) : console.error(chalk.red(`[${label}] exited with code ${code}`)));

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@commoners/solidarity",
33
"description": "Build solidarity across platform",
4-
"version": "0.0.57",
4+
"version": "0.0.58",
55
"type": "module",
66
"license": "MIT",
77
"exports": {

packages/core/start.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { join } from "node:path";
33

44
// Internal Imports
55
import { build, configureForDesktop, createServices, resolveConfig } from './index.js'
6-
import { ensureTargetConsistent, globalTempDir, handleTemporaryDirectories, isDesktop, isMobile, onCleanup } from "./globals.js";
6+
import { globalTempDir, handleTemporaryDirectories, isDesktop, isMobile, onCleanup } from "./globals.js";
77
import { ResolvedConfig, ResolvedService, UserConfig } from "./types.js";
88
import { createServer } from "./vite/index.js";
99

1010
// Internal Utilities
11-
import { buildAssets, getAppAssets } from "./utils/assets.js";
1211
import { printHeader, printTarget } from "./utils/formatting.js"
1312
import { updateServicesWithLocalIP } from "./utils/ip/index.js";
1413
import { buildAllAssets } from "./build.js";

packages/core/types.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ type ExtraServiceMetadata = _ExtraServiceMetadata & {
8888

8989
export type UserService = string | (BaseServiceMetadata & ExtraServiceMetadata) // Can nest build by platform type
9090

91+
92+
type ServiceStatus = null | boolean
9193
export type ResolvedService = {
9294

9395
// For Service Build
@@ -100,6 +102,8 @@ export type ResolvedService = {
100102

101103
// For Client
102104
url: string // What URL to use for service requests
105+
106+
status: ServiceStatus,
103107
}
104108

105109
// ------------------- Plugins -------------------
@@ -312,7 +316,11 @@ type ExposedServices = {
312316
}
313317

314318
type ExposedDesktopServices = {
315-
[x:string]: ExposedService & { onClosed: () => void }
319+
[x:string]: ExposedService & {
320+
onClosed: () => void,
321+
close: () => void,
322+
status: ServiceStatus
323+
}
316324
}
317325

318326
type ExposedPlugins = {
@@ -328,8 +336,9 @@ type BaseCommonersGlobalObject = {
328336
DEV: boolean,
329337
PROD: boolean,
330338

331-
ROOT: string,
339+
ENV: Record<string, any>, // Environment Variables loaded using Vite
332340

341+
ROOT: string,
333342

334343
__READY: Function, // Resolve Function
335344
__PLUGINS?: RawPlugins // Raw Plugins

packages/core/vite/plugins/commoners.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,23 @@ export default ({
159159
160160
const { send, services, quit, args } = globalThis.__commoners ?? {}
161161
162+
const { __id } = args ?? {}
163+
162164
const GLOBAL = globalThis.commoners = JSON.parse(\`${JSON.stringify(globalObject)}\`)
163165
if (services) GLOBAL.SERVICES = services // Replace with sanitized services from Electron if available
164166
if (GLOBAL.DESKTOP === true) GLOBAL.DESKTOP = { quit, ...args } // Ensure desktop is configured properly at the start
165167
166168
GLOBAL.READY = new Promise(res => {
167-
const ogRes = res
168-
res = (...args) => {
169-
ogRes(...args)
169+
170+
const resolve = (...args) => {
171+
res(...args) // Resolve the promise
170172
delete GLOBAL.__READY
171-
if (send) send('commoners:ready')
173+
if (send) {
174+
send("commoners:ready:" + __id) // Notify the main process that everything is ready
175+
}
172176
}
173177
174-
GLOBAL.__READY = res
178+
GLOBAL.__READY = resolve
175179
})
176180
177181
// Directly import the plugins from the transpiled configuration object

packages/plugins/windows/index.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const linkToMainWindow = (eventTarget) => {
4040

4141
const linkToMainElectronWindow = (id, context) => {
4242

43-
context.on(`link:${id}`, () => context.send(`${id}:link`)) // Start linking
43+
// Start linking
44+
context.once(`link:${id}`, () => context.send(`${id}:link`))
45+
4446
const readyPromise = new Promise(resolve => context.on(`ready:${id}`, resolve))
4547

4648
return {
@@ -213,10 +215,7 @@ export default (windows: Windows): Plugin => {
213215

214216
if (globalThis.COMMONERS_WINDOW_POPUP) {
215217
const eventTarget = new EventTarget()
216-
217-
return {
218-
main: linkToMainWindow(eventTarget)
219-
}
218+
return { main: linkToMainWindow(eventTarget) }
220219
}
221220

222221
const manager = windowTypes.reduce((acc, type) => {
@@ -238,16 +237,15 @@ export default (windows: Windows): Plugin => {
238237
}, {})
239238

240239
return manager
240+
241241
}
242242

243243

244244
// ---------------------- Electron ----------------------
245245

246246
const isMain = this.__main
247247

248-
if (!isMain) return {
249-
main: linkToMainElectronWindow(this.__id, this)
250-
}
248+
if (!isMain) return { main: linkToMainElectronWindow(this.__id, this) }
251249

252250
const manager = windowTypes.reduce((acc, type) => {
253251

@@ -297,24 +295,42 @@ export default (windows: Windows): Plugin => {
297295
this.on("open", async (_, type, requestId) => {
298296

299297
const { window } = windows[type]
298+
299+
300+
const win = await createWindow(
301+
assets[type],
302+
window
303+
);
300304

301-
const win = await createWindow(assets[type], window);
302305
this.setAttribute(win, "type", type) // Assign type attribute to window
303306
const id = win.__id
304307

305308
this.on(`${id}:message`, (_, value) => this.send(`${id}:message`, value), win); // Send from ID
306309
this.on(`message:${id}`, (_, value) => this.send(`message:${id}`, value), win); // Send to ID
307310

311+
312+
let linkInterval;
308313
// Handle link request from ID
309314
this.on(`${id}:link`, () => {
315+
clearInterval(linkInterval)
310316
this.send(`ready:${id}`); // Always send ready to dependent windows
311317
this.send(`${requestId}:ready`, id); // Send ready to main window
312318
}, win);
313319

314320
this.on(`${id}:close`, () => win.close(), win); // Trigger window closure
315321
win.on("closed", () => this.send(`${id}:closed`)); // Listen for window closure
316322

317-
this.send(`link:${id}`) // Request link to ID
323+
// NOTE: This does not resolve when build...for some reason
324+
// // Wait until the frontend plugins are ready
325+
// win.__ready.then(() => {
326+
// this.send(`link:${id}`), // Request link to ID
327+
// console.log('Commoners ready on window', id, type)
328+
// })
329+
330+
// Request to link until successful
331+
const requestToLink = () => this.send(`link:${id}`) // Request link to ID
332+
requestToLink()
333+
linkInterval = setInterval(requestToLink, 100)
318334
});
319335

320336
},
@@ -325,11 +341,8 @@ export default (windows: Windows): Plugin => {
325341

326342
this.WINDOWS[__id] = win
327343

328-
if (__main) {
329-
win.on("closed", () => Object.values(this.WINDOWS).forEach(_win => _win !== win && _win.close())); // Close all windows when the main window has closed
330-
}
331-
332-
344+
// Close all windows when the main window has closed
345+
if (__main) win.on("closed", () => Object.values(this.WINDOWS).forEach(_win => _win !== win && _win.close()));
333346
},
334347
unload: function (win) {
335348
delete this.WINDOWS[win.__id]

packages/plugins/windows/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@commoners/windows",
3-
"version": "0.0.55",
3+
"version": "0.0.58",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.mjs",
66
"license": "MIT",
@@ -12,6 +12,6 @@
1212
"vite": "^5.3.4"
1313
},
1414
"peerDependencies": {
15-
"@commoners/solidarity": "^0.0.55"
15+
"@commoners/solidarity": "^0.0.58"
1616
}
1717
}

0 commit comments

Comments
 (0)