This repository was archived by the owner on Aug 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
event handler
github-actions[bot] edited this page Sep 30, 2024
·
9 revisions
NOTE: this example uses stews
const Class = require('aepl');
const { Soup } = require('stews');
// creates the main class
new Class("Main", class {
constructor() {
this.events = new Soup(Object);
}
on(name, func) {
this.events.get(name).listen(func);
return func;
}
off(name, func) {
this.events.get(name).listeners.remove(func);
}
});
// creates the event subclass
Main.new("subclass", "Event", class {
constructor(name) {
this.name = name;
this.listeners = new Soup();
this.parent.events.push(name, this);
}
listen(func) {
this.listeners.push(func);
}
fire() {
this.listeners.forEach( (f) => {
f(...Array.from(arguments));
});
}
});
// example
let main = new Main();
let event = new main.Event("test");
let f = main.on("test", function(a, b) {
console.log(a, b);
});
event.fire("a", "b"); // a b
main.off("test", f); // removes the listener
event.fire("a", "b"); // doesn't log anythingIf you want to check out the different versions and changes check out the releases
For a look into the development side check out the src folder
init()
from()
inspect()
new()
addClass()
addFunc()
addProp()
addChore()
addAsyncChore()
addPreClass()
addPreFunc()
addPreProp()
addPreChore()
addPreAChore()
setName()
setInspect()
Subclass
Function
Property
Chore
AsyncChore
Preclass
PreFunction
PreProperty
PreChore
PreAsyncChore
compact
multiple-layers
event handler
alternate names
setting and getting inspects