1.2.0 • Published 6 years ago

callbag-gun v1.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

/**

  • callbag-gun

  • Read from and write to gun contexts using callbags.
  • yarn install callbag-gun
  • How to use:
  • readFrom(gun): converts values coming from a gun context to a callbag
  • Example:
  • pipe(readFrom(gun.get('id')), forEach(value => console.log(value)));
  • writeTo(gun): listens to a callbag and writes values into gun
  • Example:
  • pipe(interval(1000), take(2), writeTo(gun.get('id'))); // 0, 1 */

const forEach = require("callbag-for-each");

const readFrom = (gun, options = {}) => { let isRunning = false, event;

const cleanup = () => { if (!isRunning && event) { event.off(); event = void 0; } };

return (start, sink) => { if (start !== 0) return; isRunning = true; sink(0, t => { if (t === 2) { isRunning = false; cleanup(); } }); gun.on((data, key, context, _event) => { event = _event; if (!isRunning) { cleanup(); } else { sink(1, data); } }, options); }; };

const writeTo = gun => forEach(value => gun.put(value));

module.exports = { readFrom, writeTo };

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago