Skip to content

Instantly share code, notes, and snippets.

@maxhoffmann
Last active December 7, 2016 17:08
Show Gist options
  • Select an option

  • Save maxhoffmann/f8bc15332940f9941c22c40f16bf28e2 to your computer and use it in GitHub Desktop.

Select an option

Save maxhoffmann/f8bc15332940f9941c22c40f16bf28e2 to your computer and use it in GitHub Desktop.
buzz API
import buzz from '@maxhoffmann/buzz';
function clicks(broadcast) {
window.addEventListener('click', broadcast('click'));
}
const logger = () => console.log;
const services = [logger, clicks];
buzz(services);
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);
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