custom-module: clean & docs

This commit is contained in:
bordun 2020-10-27 10:30:09 +01:00
parent 6cc020c10d
commit 44b03941f9
3 changed files with 15 additions and 11 deletions

View file

@ -99,6 +99,12 @@ Saves an object/array to a json file (path is relative to the custom module loca
----
#### `require(path)`
Load another javascript module. Returns the value of `module.exports` defined in this module. If `path` is a relative path, it will be resolved against its parent module's path. See [Managing Big Modules](../managing-big-modules)
----
#### Other javascript globals
- `console`
- `setTimeout`
@ -107,3 +113,8 @@ Saves an object/array to a json file (path is relative to the custom module loca
- `clearInterval`
----
## Autoreload
Custom modules (including submodules loaded with `require()`) are reloaded automatically when they are modified. Upon reload, timers (`setTimeout` and `setInterval`) and event listeners (added to the  `app` object) are reset.

View file

@ -1,6 +1,4 @@
Custom modules don't let you use node's `require` function at runtime, but you can manage your sources in multiple files and bundle them into a single file before loading it in OSC.
The common pattern for this is using the [browserify](http://browserify.org/) library:
Starting with `v1.6.0`, custom modules can load submodules with the `require()` function.
**1**. define some variable in `number.js`
@ -22,8 +20,3 @@ module.exports = {
}
}
```
**3**. bundle your sources into `custom-module.js`:
`browserify main.js -o custom-module.js --standalone module.exports`

View file

@ -16,7 +16,7 @@ class CustomModule {
this.oscOutFilter = null
this.submodule = !!parentDir
this.filename = this.submodule ? path.resolve(parentDir, file) : file
this.filename = this.submodule && !path.isAbsolute(file) ? path.resolve(parentDir, file) : file
this.timeouts = []
this.intervals = []
@ -27,7 +27,7 @@ class CustomModule {
loadJSON: (url)=>{
if (url.split('.').pop() === 'json') {
try {
url = path.resolve(path.dirname(customModule), url)
url = path.resolve(path.dirname(this.filename), url)
return JSON.parse(fs.readFileSync(url, 'utf8'))
} catch(e) {
console.error('(ERROR) could not load json file from ' + url)
@ -39,7 +39,7 @@ class CustomModule {
},
saveJSON: (url, data)=>{
if (url.split('.').pop() === 'json') {
url = path.resolve(path.dirname(customModule), url)
url = path.resolve(path.dirname(this.filename), url)
try {
return fs.writeFileSync(url, JSON.stringify(data, null, ' '))
} catch(e) {