Skip to content

Instantly share code, notes, and snippets.

@cb-v4s
Last active July 6, 2021 01:10
Show Gist options
  • Select an option

  • Save cb-v4s/5e81b3afc1f1e9a9271d01971e5a2ba4 to your computer and use it in GitHub Desktop.

Select an option

Save cb-v4s/5e81b3afc1f1e9a9271d01971e5a2ba4 to your computer and use it in GitHub Desktop.
mediator pattern js
const mediator = (() => {
const events = {};
const subscribe = (event, fn) => {
if (!events[event]){
events[event] = [];
}
events[event].push(fn);
}
const dispatch = (event, pl) => {
if (!events[event]) return false;
events[event].forEach((e) => e(pl));
}
return {
dispatch: dispatch,
subscribe: subscribe
}
})();
// esta función contiene el código de inicio de sesión en la aplicación ...
const signin = ({ username, password }) => {
console.log(`Iniciando sesión con el usuario ${ username } ...`);
}
mediator.subscribe('login', signin);
mediator.dispatch('login', { username: 'foo', password: 'bar' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment