Last active
December 7, 2016 17:08
-
-
Save maxhoffmann/f8bc15332940f9941c22c40f16bf28e2 to your computer and use it in GitHub Desktop.
buzz API
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
| import buzz from '@maxhoffmann/buzz'; | |
| function clicks(broadcast) { | |
| window.addEventListener('click', broadcast('click')); | |
| } | |
| const logger = () => console.log; | |
| const services = [logger, clicks]; | |
| buzz(services); |
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
| import buzz from '@maxhoffmann/buzz'; | |
| const logger = () => console.log; | |
| function clicks(broadcast) { | |
| window.addEventListener('click', broadcast('click')); | |
| } | |
| function counter(broadcast) { | |
| let state = 0; | |
| return message => { | |
| if (message.type === 'click') { | |
| state += 1; | |
| broadcast('counter')(state); | |
| } | |
| } | |
| } | |
| const services = [logger, clicks, counter]; | |
| buzz(services); |
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
| export function buzz(services) { | |
| const _bus = []; | |
| const _services = []; | |
| const broadcast = type => data => { | |
| _bus.push({ type, data }); | |
| } | |
| setupServices(broadcast); | |
| requestAnimationFrame(run); | |
| function setupServices(broadcast) { | |
| for (const service of services) { | |
| _services.push(service(broadcast)) | |
| } | |
| } | |
| function run(time) { | |
| requestAnimationFrame(run); | |
| for (const message of _bus.splice(0, 100)) { | |
| for (const service of _services) { | |
| if (typeof service === 'function') { | |
| service(message); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment