Skip to content

Commit

Permalink
Merge pull request #19 from neuralinterfaces/v0.0.58
Browse files Browse the repository at this point in the history
0.0.58 Release
  • Loading branch information
garrettmflynn authored Dec 7, 2024
2 parents 5511be3 + 4081bfa commit e5e8448
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Follow the prompts to select your favorite framework and features.
After running `npm install`, add Commoners as a dependency:

```bash
npm install -D [email protected].57
npm install -D [email protected].58
```

## Configuring the `package.json` File
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"@commoners/bluetooth": "0.0.55",
"@commoners/custom-protocol": "0.0.55",
"@commoners/serial": "0.0.55",
"@commoners/solidarity": "0.0.57",
"@commoners/solidarity": "0.0.58",
"@commoners/splash-screen": "0.0.55",
"@commoners/testing": "0.0.57",
"@commoners/windows": "0.0.55",
"@commoners/testing": "0.0.58",
"@commoners/windows": "0.0.58",
"@vitest/coverage-v8": "^2.0.3",
"search-insights": "^2.15.0",
"commoners": "0.0.57",
"commoners": "0.0.58",
"vite": "^5.3.4",
"vitepress": "^1.3.1",
"vitest": "^2.0.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "commoners",
"description": "Cross-Platform Development for the Rest of Us",
"version": "0.0.57",
"version": "0.0.58",
"type": "module",
"license": "MIT",
"engines": {
Expand All @@ -18,7 +18,7 @@
"watch": "vite build --watch"
},
"dependencies": {
"@commoners/solidarity": "0.0.57",
"@commoners/solidarity": "^0.0.58",
"cac": "^6.7.14"
},
"devDependencies": {
Expand Down
20 changes: 12 additions & 8 deletions packages/core/assets/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,9 @@ const runWindowPlugins = async (win: BrowserWindow | null = null, type = 'load',

const __listeners = []

const transferredFlags = {
__id: windowCount,
__main: isMainWindow
}

const __id = windowCount
const transferredFlags = { __id, __main: isMainWindow }

windowCount++

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

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

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

win.__ready = new Promise(resolve => win.once(`commoners:ready:${__id}`, resolve)) // Commoners plugins are all loaded

return win
}

Expand Down Expand Up @@ -393,17 +394,20 @@ runAppPlugins().then(() => {
app.whenReady().then(async () => {

// ------------------------ Service Creation ------------------------
const { services: active } = await services.createAll(config.services, {
const { services: active, close: closeService } = await services.createAll(config.services, {
target: 'desktop',
build: isProduction,
root: isProduction ? __dirname : join(__dirname, '..', '..'), // Back out of default outDir
onClosed: (id, code) => serviceSend(id, 'closed', code),
onLog: (id, msg) => serviceSend(id, 'log', msg.toString()),
onLog: (id, msg) => serviceSend(id, 'log', msg.toString())
})

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

Expand Down
17 changes: 9 additions & 8 deletions packages/core/assets/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ const TEMP_COMMONERS = {
removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel),
}

// Handle service interactions
for (let id in TEMP_COMMONERS.services) {
const service = TEMP_COMMONERS.services[id]

const serviceStates = {
status: ipcRenderer.sendSync(`services:${id}:status`)
}
service.status = ipcRenderer.sendSync(`services:${id}:status`)

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

ipcRenderer.on(`services:${id}:log`, (_) => {
if (serviceStates.status) return
serviceStates.status = true
if (service.status) return
service.status = true
})

ipcRenderer.on(`services:${id}:closed`, (_, code) => {
if (serviceStates.status === false) return
serviceStates.status = false
if (service.status === false) return
service.status = false
listeners.closed.forEach(f => f(code))
})

// ---------------- Assign Functions ----------------
service.onClosed = (listener) => {
if (serviceStates.status === false) listener()
if (service.status === false) listener()
listeners.closed.push(listener)
}

service.close = () => ipcRenderer.send(`services:${id}:close`)

}

// Expose ipcRenderer
Expand Down
14 changes: 6 additions & 8 deletions packages/core/assets/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ async function getServiceUrl(service) {
// For Client
url: await getServiceUrl({ src, url, host, port }),

// NOTE: Not in types...
states: null,
status: null,

}

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

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

// Notify of process closure gracefully
childProcess.on('close', (code) => {
if (code !== null) {
config.status = false
if (opts.onClosed) opts.onClosed(id, code)
delete processes[id]
printServiceMessage(label, `Exited with code ${code}`, 'error')
}
config.status = false
if (opts.onClosed) opts.onClosed(id, code)
delete processes[id]
if (code !== null) printServiceMessage(label, `Exited with code ${code}`, 'error')
});

// process.on('close', (code) => code === null ? console.log(chalk.gray(`Restarting ${label}...`)) : console.error(chalk.red(`[${label}] exited with code ${code}`)));
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@commoners/solidarity",
"description": "Build solidarity across platform",
"version": "0.0.57",
"version": "0.0.58",
"type": "module",
"license": "MIT",
"exports": {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { join } from "node:path";

// Internal Imports
import { build, configureForDesktop, createServices, resolveConfig } from './index.js'
import { ensureTargetConsistent, globalTempDir, handleTemporaryDirectories, isDesktop, isMobile, onCleanup } from "./globals.js";
import { globalTempDir, handleTemporaryDirectories, isDesktop, isMobile, onCleanup } from "./globals.js";
import { ResolvedConfig, ResolvedService, UserConfig } from "./types.js";
import { createServer } from "./vite/index.js";

// Internal Utilities
import { buildAssets, getAppAssets } from "./utils/assets.js";
import { printHeader, printTarget } from "./utils/formatting.js"
import { updateServicesWithLocalIP } from "./utils/ip/index.js";
import { buildAllAssets } from "./build.js";
Expand Down
13 changes: 11 additions & 2 deletions packages/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type ExtraServiceMetadata = _ExtraServiceMetadata & {

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


type ServiceStatus = null | boolean
export type ResolvedService = {

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

// For Client
url: string // What URL to use for service requests

status: ServiceStatus,
}

// ------------------- Plugins -------------------
Expand Down Expand Up @@ -312,7 +316,11 @@ type ExposedServices = {
}

type ExposedDesktopServices = {
[x:string]: ExposedService & { onClosed: () => void }
[x:string]: ExposedService & {
onClosed: () => void,
close: () => void,
status: ServiceStatus
}
}

type ExposedPlugins = {
Expand All @@ -328,8 +336,9 @@ type BaseCommonersGlobalObject = {
DEV: boolean,
PROD: boolean,

ROOT: string,
ENV: Record<string, any>, // Environment Variables loaded using Vite

ROOT: string,

__READY: Function, // Resolve Function
__PLUGINS?: RawPlugins // Raw Plugins
Expand Down
14 changes: 9 additions & 5 deletions packages/core/vite/plugins/commoners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,23 @@ export default ({
const { send, services, quit, args } = globalThis.__commoners ?? {}
const { __id } = args ?? {}
const GLOBAL = globalThis.commoners = JSON.parse(\`${JSON.stringify(globalObject)}\`)
if (services) GLOBAL.SERVICES = services // Replace with sanitized services from Electron if available
if (GLOBAL.DESKTOP === true) GLOBAL.DESKTOP = { quit, ...args } // Ensure desktop is configured properly at the start
GLOBAL.READY = new Promise(res => {
const ogRes = res
res = (...args) => {
ogRes(...args)
const resolve = (...args) => {
res(...args) // Resolve the promise
delete GLOBAL.__READY
if (send) send('commoners:ready')
if (send) {
send("commoners:ready:" + __id) // Notify the main process that everything is ready
}
}
GLOBAL.__READY = res
GLOBAL.__READY = resolve
})
// Directly import the plugins from the transpiled configuration object
Expand Down
43 changes: 28 additions & 15 deletions packages/plugins/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const linkToMainWindow = (eventTarget) => {

const linkToMainElectronWindow = (id, context) => {

context.on(`link:${id}`, () => context.send(`${id}:link`)) // Start linking
// Start linking
context.once(`link:${id}`, () => context.send(`${id}:link`))

const readyPromise = new Promise(resolve => context.on(`ready:${id}`, resolve))

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

if (globalThis.COMMONERS_WINDOW_POPUP) {
const eventTarget = new EventTarget()

return {
main: linkToMainWindow(eventTarget)
}
return { main: linkToMainWindow(eventTarget) }
}

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

return manager

}


// ---------------------- Electron ----------------------

const isMain = this.__main

if (!isMain) return {
main: linkToMainElectronWindow(this.__id, this)
}
if (!isMain) return { main: linkToMainElectronWindow(this.__id, this) }

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

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

const { window } = windows[type]


const win = await createWindow(
assets[type],
window
);

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

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


let linkInterval;
// Handle link request from ID
this.on(`${id}:link`, () => {
clearInterval(linkInterval)
this.send(`ready:${id}`); // Always send ready to dependent windows
this.send(`${requestId}:ready`, id); // Send ready to main window
}, win);

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

this.send(`link:${id}`) // Request link to ID
// NOTE: This does not resolve when build...for some reason
// // Wait until the frontend plugins are ready
// win.__ready.then(() => {
// this.send(`link:${id}`), // Request link to ID
// console.log('Commoners ready on window', id, type)
// })

// Request to link until successful
const requestToLink = () => this.send(`link:${id}`) // Request link to ID
requestToLink()
linkInterval = setInterval(requestToLink, 100)
});

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

this.WINDOWS[__id] = win

if (__main) {
win.on("closed", () => Object.values(this.WINDOWS).forEach(_win => _win !== win && _win.close())); // Close all windows when the main window has closed
}


// Close all windows when the main window has closed
if (__main) win.on("closed", () => Object.values(this.WINDOWS).forEach(_win => _win !== win && _win.close()));
},
unload: function (win) {
delete this.WINDOWS[win.__id]
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/windows/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@commoners/windows",
"version": "0.0.55",
"version": "0.0.58",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"license": "MIT",
Expand All @@ -12,6 +12,6 @@
"vite": "^5.3.4"
},
"peerDependencies": {
"@commoners/solidarity": "^0.0.55"
"@commoners/solidarity": "^0.0.58"
}
}
Loading

0 comments on commit e5e8448

Please sign in to comment.