ModulesLoader¶
Warning
The ModulesLoader
is used internally by Obsidian, you should not use it
directly in most cases. See Application to know how to load or unload
a module.
-
class
ModulesLoader
()¶ Loads Obsidian modules, resolving and injecting dependencies.
-
ModulesLoader.
register
(module, params)¶ Register a new module (it will not be loaded, only registered).
const myModule1 = require("my-module-1"); const myModule2 = require("my-module-2"); const fakeFoobarModule = require("fake-foobar-module"); modules.register(myModule1); modules.register(myModule2, { name: "new-module", // renames the module modules: { myModule: "myModule1", // inject the previously loaded myModule1 as myModule foobar: fakeFoobarModule, // inject fakeFoobarModule as foobar }, config: { option: "value", } });
Note
You not need to specify modules to inject, Obsidian resolves them automatically. The
modules
option is only useful when you want to substitute a dependency by an other one.Arguments: - module (Object) – The module to load (see Obsidian Module Definition).
- params (Object) – (optional).
- params.name (string) – A name that will be used instead of
module.name
(optional). - params.modules (Object) – Modules objects or names that will be injected (optional).
- params.config (Object) – Configuration that will be passed to the module at load time (optional).
-
ModulesLoader.
load
(moduleName)¶ Load a module (the module must be registered first).
modules.load("my-module") .then(myModule => { // do stuff with the module }) .catch(error => { console.error("Unable to load 'my-module':", error); });
Arguments: - moduleName (string) – The module name.
Returns: A promise that returns the loaded module.
-
ModulesLoader.
loadAll
()¶ Load all registered modules that are not already loaded.
modules.register(module1); modules.register(module2); modules.loadAll() .then(loadedModules => { // { // module1: ..., // module2: ..., // } }) .catch(error => { console.error("An error occurred when loading modules:", error); });
Returns: A promise that returns an object containing the loaded modules.
-
ModulesLoader.
unload
(moduleName)¶ Unload a module
modules.unload("my-module") .catch(error => { console.error("Unable to unload 'my-module':", error); });
Arguments: - moduleName (string) – The module name.
Returns: A promise.
-
ModulesLoader.
modules
¶ Access to loaded modules.
{ module1: ..., module2: ..., ... }
-
ModulesLoader.
setApp
(app)¶ Define the (sub)application this module will work with.
Arguments: - app (Application) – The application or sub-application.
-