ea64c138c3
input.js is now responsible for processing mouse input and translating it to relevant events. This allows for less bloat on the main logic in index.js and easy implementation of new functionality Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
27 lines
646 B
JavaScript
27 lines
646 B
JavaScript
/**
|
|
* Implementation of a simple Oberver Pattern for custom event handling
|
|
*/
|
|
function Observer() {
|
|
this.handlers = new Set();
|
|
}
|
|
|
|
Observer.prototype = {
|
|
// Adds handler for this message
|
|
on(callback) {
|
|
this.handlers.add(callback);
|
|
return callback;
|
|
},
|
|
clear(callback) {
|
|
return this.handlers.delete(callback);
|
|
},
|
|
emit(msg) {
|
|
this.handlers.forEach(async (handler) => {
|
|
try {
|
|
await handler(msg);
|
|
} catch (e) {
|
|
console.warn('Observer failed to run handler');
|
|
console.warn(handler);
|
|
}
|
|
});
|
|
},
|
|
};
|