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

Improve duplicate module filtering. Update SocketIO catch-all API. #3523

Merged
merged 4 commits into from
Aug 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _This release is scheduled to be released on 2024-10-01._
- [weather] Updated `apiVersion` default from 2.5 to 3.0 (#3424)
- [core] Updated dependencies
- [core] Allow custom module positions by setting `allowCustomModulePositions` in `config.js` (fixes #3504, related to https://github.com/MagicMirrorOrg/MagicMirror/pull/3445)
- [core] Updated SocketIO catch all to new API

### Fixed

Expand Down
30 changes: 12 additions & 18 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const Log = require("logger");
const Server = require(`${__dirname}/server`);
const Utils = require(`${__dirname}/utils`);
const defaultModules = require(`${__dirname}/../modules/default/defaultmodules`);
const helperhash = {};

// Get version number.
global.version = require(`${__dirname}/../package.json`).version;
Expand Down Expand Up @@ -176,23 +175,15 @@ function App () {

const helperPath = `${moduleFolder}/node_helper.js`;

// find out if helper was loaded before for this module
// only load it once
let loadHelper = helperhash[moduleName] ? false : true;

// if this is the 1st time for this module, check for helper file
// otherwise, its already loaded, if found
if (loadHelper) {
try {
fs.accessSync(helperPath, fs.R_OK);
// indicte we found one to load for this module
helperhash[moduleName] = true;
} catch (e) {
loadHelper = false;
Log.log(`No helper found for module: ${moduleName}.`);
}
let loadHelper = true;
try {
fs.accessSync(helperPath, fs.R_OK);
} catch (e) {
loadHelper = false;
Log.log(`No helper found for module: ${moduleName}.`);
}
// if the helper was found, AND needed 1st time

// if the helper was found
if (loadHelper) {
const Module = require(helperPath);
let m = new Module();
Expand Down Expand Up @@ -270,7 +261,10 @@ function App () {
if (module.disabled) continue;
if (module.module) {
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
modules.push(module.module);
// Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
if (!modules.includes(module.module)) {
modules.push(module.module);
}
} else {
Log.warn("Invalid module position found for this configuration:", module);
}
Expand Down
15 changes: 2 additions & 13 deletions js/node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,9 @@ const NodeHelper = Class.extend({
Log.log(`Connecting socket for: ${this.name}`);

io.of(this.name).on("connection", (socket) => {
// add a catch all event.
const onevent = socket.onevent;
socket.onevent = function (packet) {
const args = packet.data || [];
onevent.call(this, packet); // original call
packet.data = ["*"].concat(args);
onevent.call(this, packet); // additional call to catch-all
};

// register catch all.
socket.on("*", (notification, payload) => {
if (notification !== "*") {
this.socketNotificationReceived(notification, payload);
}
socket.onAny((notification, payload) => {
this.socketNotificationReceived(notification, payload);
});
});
}
Expand Down