Published
Edited
Sep 30, 2020
1 fork
Importers
2 stars
Insert cell
Insert cell
Insert cell
RXall(() => rx.of(1, 2, 3))
Insert cell
Insert cell
RXall(() => rx.interval(100).pipe(op.take(10)), { logMS: true })
Insert cell
Insert cell
RXlatest(() => rx.of(1, 2, 3))
Insert cell
Insert cell
RX(() =>
rx.Observable.create(subscriber => {
subscriber.next(1);
subscriber.error(new Error('oh no'));
subscriber.complete();
})
)
Insert cell
RX(() => rx.throwError('oh no'))
Insert cell
md`To use operators on a stream we need to use \`.pipe()\`:`
Insert cell
RX(() =>
// 'of' allows you to deliver values in a sequence
rx.of(1, 2, 3, 4, 5, 6).pipe(
// only accept values greater than 2
op.filter(v => v > 2),
// multiply all values in a stream by 3
op.map(v => v * 3)
)
)
Insert cell
Insert cell
RXall(() =>
rx.throwError(new Error('oh no')).pipe(
// We can replace errors with normal data
op.catchError(err => rx.of('fixed'))
)
)
Insert cell
RXall(() =>
rx.of(1, 2, 3, 4, 5).pipe(
op.mergeMap(v => {
if (v & 1) return rx.of('odd');

return rx
.throwError(new Error('even values unsupported'))
.pipe(op.catchError(err => rx.of('fixed')));
})
)
)
Insert cell
RXall(() =>
rx.range(1, 100).pipe(
// skip first 3 elements
op.skip(3),
// and take only next 5
op.take(5)
)
)
Insert cell
Insert cell
function RXinput() {
const form = html`<form>
<input name=input />
</form>`;

const obs = rx.Observable.create(subscriber => {
console.log('here');
const verb = "oninput";
form[verb] = e => {
console.log('oninput');
e && e.preventDefault();
subscriber.next(form.input.value);
};
form[verb]();
});

form.value = obs.pipe();

return form;
}
Insert cell
Insert cell
function RXlatest(f, options = {}) {
return Generators.observe(resolve => {
const vals = [];
const start = performance.now();
const logVal = v => {
const ms = Math.floor(performance.now() - start);
const val = options.logMS ? { ms: ms, v: v } : v;
if (options.logAll) {
vals.push(val);
resolve(vals);
} else {
resolve(val);
}
};

resolve([]);

f().subscribe({
next(x) {
logVal(x);
},
error(err) {
if (err.message !== undefined) {
logVal('❌: ' + err.message);
} else {
logVal('❌: ' + err);
}
},
complete() {
logVal('✅');
}
});
});
}
Insert cell
function RXall(f, options = {}) {
options.logAll = true;
return RXlatest(f, options);
}
Insert cell
RX = RXall
Insert cell
rx = await require("rxjs@6.6.2/bundles/rxjs.umd.js")
Insert cell
op = rx.operators
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more