Last active
March 23, 2022 22:31
-
-
Save bitifet/5c994cd1ae4f613964b90285b211b041 to your computer and use it in GitHub Desktop.
Event Driven Async Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sampleModule/index.js | |
// ===================== | |
// EDAM - Event Driven Async Module | |
// (Example / Skeleton) | |
"use strict"; | |
module.exports = (async ()=>{ | |
// Module-level definitions: | |
const module_preferences = { | |
p1: 10, | |
p2: "sth", | |
}; | |
// Main dependencies: | |
const eventEmitter = require("events"); | |
// const mainDep1 = await require(…); | |
// … | |
// Submodules: | |
const submodules = [ | |
require("./submodule1"), | |
require("./submodule2"), | |
/* … */ | |
]; | |
// Module Core: | |
class sampleModule_core extends eventEmitter { | |
constructor(prefs) { | |
super(); | |
Object.assign(this, prefs) | |
/* … */ | |
}; | |
/* … */ | |
}; | |
// Parallel submodule mounting: | |
return Object.assign( | |
new sampleModule_core(module_preferences) | |
, ...(await Promise.all(submodules)) | |
); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sampleModule/submodule1.js | |
// ========================== | |
// EDAM Submodule Example | |
"use strict"; | |
module.exports = (async ()=>{ | |
// Submodule dependencies: | |
// const sbmDep1 = await require(…); | |
// … | |
return { | |
triggerSomething() { | |
this.emit("something"); | |
}, | |
}; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sampleModule/submodule2.js | |
// ========================== | |
// EDAM Submodule Example | |
"use strict"; | |
module.exports = (async ()=>{ | |
// Submodule dependencies: | |
// const sbmDep1 = await require(…); | |
// … | |
return { | |
sayOnSomething(what) { | |
this.on("something", ()=>console.log(what)); | |
}, | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment