Files
gun/examples/angular/src/app/gun.helper.ts
Victor Noël 12897b1bcd on$: fix unsubscribe and add cleanup
Add an example of concurrent subscription to gun in the
view with a button to subscribe and unsubscribe.
2017-04-29 11:25:59 +02:00

31 lines
998 B
TypeScript

import { Observable } from 'rxjs/Observable';
import { Gun } from 'gun/gun';
import { pick } from 'underscore';
export function on$(node, cleanup = true): Observable<any> {
return Observable.fromEventPattern(
h => {
// there is no way to off() an on() until at least one value is trigerred
// so that we can access the event listener to off() it
const signal = { stop: false };
node.on((data, key, at, ev) => {
if (signal.stop) {
ev.off();
} else {
// modifying data directly does not seem to work...
h(cleanup ? pick(data, (v, k, o) => v !== null && k !== '_') : data);
}
});
return signal;
},
(h, signal) => { signal.stop = true; }
);
}
export function val$(node): Observable<any> {
return new Observable(o => node.val(v => {
o.next(v);
o.complete();
}));
}