Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active March 23, 2022 22:31
Show Gist options
  • Save bitifet/5c994cd1ae4f613964b90285b211b041 to your computer and use it in GitHub Desktop.
Save bitifet/5c994cd1ae4f613964b90285b211b041 to your computer and use it in GitHub Desktop.
Event Driven Async Modules
// 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))
);
})();
// sampleModule/submodule1.js
// ==========================
// EDAM Submodule Example
"use strict";
module.exports = (async ()=>{
// Submodule dependencies:
// const sbmDep1 = await require(…);
// …
return {
triggerSomething() {
this.emit("something");
},
};
})();
// 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