openOutpaint/js/util.js
Victor Seiji Hariki ea64c138c3 Add generic mouse input handler
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>
2022-11-20 23:03:07 -03:00

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);
}
});
},
};