-
-
Save cb-v4s/5e81b3afc1f1e9a9271d01971e5a2ba4 to your computer and use it in GitHub Desktop.
mediator pattern js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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