Skip to content

Commit

Permalink
Removed Installer service circular dependencies (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvcabral authored Feb 3, 2025
1 parent e2f1489 commit 91fbc64
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
21 changes: 19 additions & 2 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*--------------------------------------------------------------------------------------------*/
import { app, BrowserWindow, ipcMain } from "electron";
import { getAudioMuted, getSimulatorOption } from "./settings";
import { subscribeInstaller } from "../server/installer";
import { runOnPeerRoku, resetPeerRoku } from "./roku";
import { appFocused } from "./window";
import { subscribeECP } from "../server/ecp";
Expand Down Expand Up @@ -103,7 +104,7 @@ mm_icon_focus_hd=pkg:/images/channel-poster_hd.png
splash_screen_hd=pkg:/images/splash-screen_hd.jpg`;
const poster = fs.readFileSync(path.join(__dirname, "images", "channel-icon.png"));
const zewZip = zipSync({
"manifest": [strToU8(manifest), {}],
manifest: [strToU8(manifest), {}],
"source/main.brs": [strToU8(code), {}],
"images/channel-poster_hd.png": [poster, {}],
});
Expand Down Expand Up @@ -146,6 +147,8 @@ function focusWindow(window) {
}
}

// Server Events

subscribeECP("files", launchApp);

function launchApp(event, data) {
Expand All @@ -169,7 +172,21 @@ function launchApp(event, data) {
}
loadFile([zipPath], input);
} else {
window?.webContents.send("console", `ECP Launch: File not found! App Id=${appID}`, true);
window?.webContents.send(
"console",
`ECP Launch: File not found! App Id=${appID}`,
true
);
}
}
}

subscribeInstaller("files", installApp);

function installApp(event, data) {
if (event === "install") {
const input = new Map();
input.set("source", data.source);
loadFile([data.file], input);
}
}
16 changes: 15 additions & 1 deletion src/helpers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setPort,
isInstallerEnabled,
setPassword,
subscribeInstaller,
} from "../server/installer";
import { createMenu, createShortMenu, checkMenuItem } from "../menu/menuService";

Expand Down Expand Up @@ -876,7 +877,8 @@ export function getModelName(model) {
return modelName ? modelName[0].replace(/ *\([^)]*\) */g, "") : `Roku (${model})`;
}

// Services Status
// Server Events

subscribeECP("settings", updateECPStatus);

export function updateECPStatus(event, enabled) {
Expand All @@ -889,6 +891,18 @@ export function updateECPStatus(event, enabled) {
}
}

subscribeInstaller("settings", updateInstallerStatus);

export function updateInstallerStatus(event, data) {
if (event === "enabled") {
setPreference("services.installer", data.enabled ? ["enabled"] : []);
checkMenuItem("web-installer", data.enabled);
const window = BrowserWindow.fromId(1);
window?.webContents.send("serverStatus", "Web", data.enabled, data.port);
window?.webContents.send("refreshMenu");
}
}

// Settings Helper Functions

function saveSimulatorSettings(options, window) {
Expand Down
13 changes: 9 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import minimist from "minimist";
import jetpack from "fs-jetpack";
import { app, screen } from "electron";
import { DateTime } from "luxon";
import { setPassword, setPort, enableInstaller, updateInstallerStatus } from "./server/installer";
import { setPassword, setPort, enableInstaller } from "./server/installer";
import { initECP, enableECP } from "./server/ecp";
import { enableTelnet, updateTelnetStatus } from "./server/telnet";
import {
Expand All @@ -33,6 +33,7 @@ import {
setThemeSource,
setTimeZone,
updateECPStatus,
updateInstallerStatus,
} from "./helpers/settings";
import {
createWindow,
Expand Down Expand Up @@ -144,9 +145,13 @@ app.on("ready", () => {
mainWindow.webContents.on("dom-ready", () => {
let settings = getSettings(mainWindow);
if (!firstLoad) {
updateECPStatus(settings.value("services.ecp").includes("enabled"));
updateTelnetStatus(settings.value("services.telnet").includes("enabled"));
updateInstallerStatus(settings.value("services.installer").includes("enabled"));
const status = "enabled";
updateECPStatus(status, settings.value("services.ecp").includes(status));
updateTelnetStatus(settings.value("services.telnet").includes(status));
updateInstallerStatus(status, {
enabled: settings.value("services.installer").includes(status),
port: settings.value("services.webPort"),
});
}
if (settings.preferences.remote) {
setRemoteKeys(settings.defaults.remote, settings.preferences.remote);
Expand Down
33 changes: 17 additions & 16 deletions src/server/installer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*---------------------------------------------------------------------------------------------
* BrightScript Simulation Desktop Application (https://github.com/lvcabral/brs-desktop)
*
* Copyright (c) 2019-2024 Marcelo Lv Cabral. All Rights Reserved.
* Copyright (c) 2019-2025 Marcelo Lv Cabral. All Rights Reserved.
*
* Licensed under the MIT License. See LICENSE in the repository root for license information.
*--------------------------------------------------------------------------------------------*/
Expand All @@ -11,9 +11,6 @@ import fs from "fs";
import path from "path";
import http from "http";
import crypt from "crypto";
import { loadFile } from "../helpers/files";
import { setPreference } from "../helpers/settings";
import { checkMenuItem } from "../menu/menuService";

const credentials = {
userName: "rokudev",
Expand Down Expand Up @@ -92,10 +89,8 @@ export function enableInstaller() {
done = "file";
});
writeStream.on("finish", () => {
const input = new Map();
input.set("source", "auto-run-dev");
loadFile([saveTo], input);
})
notifyAll("install", { file: saveTo, source: "auto-run-dev" });
});
} catch (error) {
res.writeHead(500);
res.end(
Expand Down Expand Up @@ -198,7 +193,7 @@ export function enableInstaller() {
})
.listen(port, () => {
isInstallerEnabled = true;
updateInstallerStatus(isInstallerEnabled);
notifyAll("enabled", { enabled: true, port: port });
});
server.on("error", (e) => {
if (e.code === "EADDRINUSE") {
Expand All @@ -216,16 +211,22 @@ export function disableInstaller() {
server.close();
}
isInstallerEnabled = false;
updateInstallerStatus(isInstallerEnabled);
notifyAll("enabled", { enabled: false, port: port });
}
}

export function updateInstallerStatus(enabled) {
setPreference("services.installer", enabled ? ["enabled"] : []);
checkMenuItem("web-installer", enabled);
const window = BrowserWindow.fromId(1);
window.webContents.send("serverStatus", "Web", enabled, port);
window.webContents.send("refreshMenu");
// Observers Handling
const observers = new Map();
export function subscribeInstaller(observerId, observerCallback) {
observers.set(observerId, observerCallback);
}
export function unsubscribeInstaller(observerId) {
observers.delete(observerId);
}
function notifyAll(eventName, eventData) {
observers.forEach((callback, id) => {
callback(eventName, eventData);
});
}

// Helper Functions
Expand Down

0 comments on commit 91fbc64

Please sign in to comment.