mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Add async callback to module when loaded
This commit is contained in:
parent
98855de71f
commit
3947deb7bd
54
js/app.js
54
js/app.js
@ -66,7 +66,7 @@ var App = function() {
|
|||||||
*
|
*
|
||||||
* argument module string - The name of the module (including subpath).
|
* argument module string - The name of the module (including subpath).
|
||||||
*/
|
*/
|
||||||
var loadModule = function(module) {
|
var loadModule = function(module, callback) {
|
||||||
|
|
||||||
var elements = module.split("/");
|
var elements = module.split("/");
|
||||||
var moduleName = elements[elements.length - 1];
|
var moduleName = elements[elements.length - 1];
|
||||||
@ -103,6 +103,10 @@ var App = function() {
|
|||||||
m.setName(moduleName);
|
m.setName(moduleName);
|
||||||
m.setPath(path.resolve(moduleFolder));
|
m.setPath(path.resolve(moduleFolder));
|
||||||
nodeHelpers.push(m);
|
nodeHelpers.push(m);
|
||||||
|
|
||||||
|
m.loaded(callback);
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -111,14 +115,24 @@ var App = function() {
|
|||||||
*
|
*
|
||||||
* argument module string - The name of the module (including subpath).
|
* argument module string - The name of the module (including subpath).
|
||||||
*/
|
*/
|
||||||
var loadModules = function(modules) {
|
var loadModules = function(modules, callback) {
|
||||||
console.log("Loading module helpers ...");
|
console.log("Loading module helpers ...");
|
||||||
|
|
||||||
for (var m in modules) {
|
var loadNextModule = function() {
|
||||||
loadModule(modules[m]);
|
if (modules.length > 0) {
|
||||||
}
|
var nextModule = modules[0];
|
||||||
|
loadModule(nextModule, function() {
|
||||||
|
modules = modules.slice(1);
|
||||||
|
loadNextModule();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// All modules are loaded
|
||||||
|
console.log("All module helpers loaded.");
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
console.log("All module helpers loaded.");
|
loadNextModule();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* cmpVersions(a,b)
|
/* cmpVersions(a,b)
|
||||||
@ -164,24 +178,24 @@ var App = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadModules(modules);
|
loadModules(modules, function() {
|
||||||
|
var server = new Server(config, function(app, io) {
|
||||||
|
console.log("Server started ...");
|
||||||
|
|
||||||
var server = new Server(config, function(app, io) {
|
for (var h in nodeHelpers) {
|
||||||
console.log("Server started ...");
|
var nodeHelper = nodeHelpers[h];
|
||||||
|
nodeHelper.setExpressApp(app);
|
||||||
|
nodeHelper.setSocketIO(io);
|
||||||
|
nodeHelper.start();
|
||||||
|
}
|
||||||
|
|
||||||
for (var h in nodeHelpers) {
|
console.log("Sockets connected & modules started ...");
|
||||||
var nodeHelper = nodeHelpers[h];
|
|
||||||
nodeHelper.setExpressApp(app);
|
|
||||||
nodeHelper.setSocketIO(io);
|
|
||||||
nodeHelper.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Sockets connected & modules started ...");
|
if (typeof callback === "function") {
|
||||||
|
callback(config);
|
||||||
if (typeof callback === "function") {
|
}
|
||||||
callback(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -96,6 +96,21 @@ requiresVersion: "2.1.0",
|
|||||||
####`init()`
|
####`init()`
|
||||||
This method is called when a module gets instantiated. In most cases you do not need to subclass this method.
|
This method is called when a module gets instantiated. In most cases you do not need to subclass this method.
|
||||||
|
|
||||||
|
####`loaded(callback)`
|
||||||
|
|
||||||
|
*Introduced in version: 2.1.1.*
|
||||||
|
|
||||||
|
This method is called when a module is loaded. Subsequent modules in the config are not yet loaded. The `callback` function MUST be called when the module is done loading. In most cases you do not need to subclass this method.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
````javascript
|
||||||
|
loaded: function(callback) {
|
||||||
|
this.finishLoading();
|
||||||
|
Log.log(this.name + ' is loaded!');
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
####`start()`
|
####`start()`
|
||||||
This method is called when all modules are loaded an the system is ready to boot up. Keep in mind that the dom object for the module is not yet created. The start method is a perfect place to define any additional module properties:
|
This method is called when all modules are loaded an the system is ready to boot up. Keep in mind that the dom object for the module is not yet created. The start method is a perfect place to define any additional module properties:
|
||||||
|
|
||||||
|
5
modules/node_modules/node_helper/index.js
generated
vendored
5
modules/node_modules/node_helper/index.js
generated
vendored
@ -14,6 +14,11 @@ NodeHelper = Class.extend({
|
|||||||
console.log("Initializing new module helper ...");
|
console.log("Initializing new module helper ...");
|
||||||
},
|
},
|
||||||
|
|
||||||
|
loaded: function(callback) {
|
||||||
|
console.log("Module helper loaded: " + this.name);
|
||||||
|
callback();
|
||||||
|
},
|
||||||
|
|
||||||
start: function() {
|
start: function() {
|
||||||
console.log("Staring module helper: " + this.name);
|
console.log("Staring module helper: " + this.name);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user