callbag-gun
v1.2.0
Published
Read from & write to gun using callbags
Downloads
5
Readme
/**
- 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 };