Skip to content

Instantly share code, notes, and snippets.

@gabrielsaints
Created October 25, 2019 11:26
Show Gist options
  • Save gabrielsaints/d461b6a8e2f1d11d3ba9b8201c2a23a0 to your computer and use it in GitHub Desktop.
Save gabrielsaints/d461b6a8e2f1d11d3ba9b8201c2a23a0 to your computer and use it in GitHub Desktop.
Classe que deve ser extendida, afim de criar metodos de eventos.
class Evento {
constructor(eventos = []) {
// eslint-disable-next-line no-return-assign
eventos.forEach(evento => (this[evento] = []));
this.eventos = eventos;
}
addEventListener(nome, evento) {
console.log(`%c addEventListener ${nome} `, 'background: #17a2b8; color: #0c0c0c');
try {
this[nome].push(evento);
return this;
} catch (e) {
throw new Error([e.message, nome].join(' - '));
}
}
invoke(event, body) {
console.log(`%c invoke ${event} `, 'background: #ffc107; color: #0c0c0c');
if (this[event] && this[event].length) {
this[event].forEach(e => e(body, this));
}
}
setState(state, value, event = '', data = {}) {
console.log(`%c setState ${state} ${value} ${event} `, 'background: #28a745; color: #0c0c0c');
this[state] = value;
if (event) {
this.invoke(event, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment