Skip to content

Instantly share code, notes, and snippets.

@phaistonian
Last active November 10, 2015 08:17
Show Gist options
  • Select an option

  • Save phaistonian/2542563e6e5d7e5a2f97 to your computer and use it in GitHub Desktop.

Select an option

Save phaistonian/2542563e6e5d7e5a2f97 to your computer and use it in GitHub Desktop.

Revisions

  1. phaistonian revised this gist Aug 14, 2015. No changes.
  2. phaistonian created this gist Aug 14, 2015.
    43 changes: 43 additions & 0 deletions firesync.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import Rebase from 're-base';

    const endPoint = 'actions';
    const firebaseUrl = 'https://firesync.firebaseio.com';
    const client = String(Date.now() + Math.random() * 100);
    const data = [];
    let initiated = false;
    const base = Rebase.createClass(firebaseUrl);

    function firesync (store) {

    if (!initiated) {
    base.listenTo(endPoint, {
    context: {},
    asArray: true,
    then(newData) {
    newData.filter(item => item.client !== client)
    .forEach(item => {
    store.dispatch({...item.data, client});
    });
    }
    });

    initiated = true;
    }

    return next => action => {
    const value = next(action);

    if (!value.client) {
    base.post(endPoint, {
    data: [...data, {
    client,
    ts: Date.now(),
    data: value
    }]
    });
    }
    return value;
    };
    }

    export default firesync;