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.

Revisions

  1. Maximilian Hoffmann revised this gist Dec 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions view.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const logger = () => console.log;
    import buzz from '@maxhoffmann/buzz';

    function clicks(broadcast) {
    window.addEventListener('click', broadcast('click'));
    @@ -22,6 +22,6 @@ function view() {
    }
    }

    const services = [logger, clicks, counter, view];
    const services = [clicks, counter, view];

    buzz(services);
  2. Maximilian Hoffmann revised this gist Dec 7, 2016. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions view.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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);
    }
    }
    }

    function view() {
    return message => {
    if (message.type === 'counter') {
    document.body.innerHTML = message.data;
    }
    }
    }

    const services = [logger, clicks, counter, view];

    buzz(services);
  3. Maximilian Hoffmann revised this gist Dec 7, 2016. 1 changed file with 0 additions and 29 deletions.
    29 changes: 0 additions & 29 deletions maxhoffmann_buzz.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    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);
    }
    }
    }
    }
    }
  4. Maximilian Hoffmann renamed this gist Dec 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Maximilian Hoffmann renamed this gist Dec 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Maximilian Hoffmann revised this gist Dec 7, 2016. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions buzz.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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);
    }
    }
    }
    }
    }
  7. Maximilian Hoffmann revised this gist Dec 7, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion clicks.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import buzz from 'buzz';
    import buzz from '@maxhoffmann/buzz';

    function clicks(broadcast) {
    window.addEventListener('click', broadcast('click'));
    2 changes: 1 addition & 1 deletion counter.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import buzz from 'buzz';
    import buzz from '@maxhoffmann/buzz';

    const logger = () => console.log;

  8. Maximilian Hoffmann created this gist Dec 7, 2016.
    10 changes: 10 additions & 0 deletions clicks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import buzz from 'buzz';

    function clicks(broadcast) {
    window.addEventListener('click', broadcast('click'));
    }
    const logger = () => console.log;

    const services = [logger, clicks];

    buzz(services);
    22 changes: 22 additions & 0 deletions counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import buzz from '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);