Skip to content

Instantly share code, notes, and snippets.

Dean Radcliffe deanius

View GitHub Profile
View rx-7-concatmap.js
let result$ = download$.concatMap(action => {
const { filename } = action.payload
return ajax( /* same AJAX stuff as before */ )
})
result$.tap(action => {
// save to localStorage
}).subscribe()
View rx-6-ajax.js
agent.on('download/start', ({ action }) => {
const { filename } = action.payload
return ajax({ url: 'download.php?file=' + filename })
.map(result => ({
type: 'download/complete',
payload: {
bytes: result.response,
filename
}
})
View rx-5-rx-storage.js
result$.tap(action => {
const { filename, bytes } = action.payload
localStorage.set(filename, bytes)
console.log('Saved ' + filename)
}).subscribe()
View rx-4-storage.js
let download$ = (simulated or real download requests)
let result$ = (simulated or real results of downloads)
// TODO create result$ from download$
agent.on('download/complete', ({ action }) => {
const { filename, bytes } = action.payload
localStorage.set(filename, bytes)
console.log('Saved ' + filename)
})
View rx-3-real.js
// A series of user-initiated download start requests
let download$ = fromEvent(document, 'click', '.downloadable')
.map(e => months[e.target.attribues['data-month']])
.map(month => ({
type: 'download/start',
payload: { file: month + '.csv' }
})
View rx-2-html.html
<a class="downloadable" data-month="0">Download January</a>
<a class="downloadable" data-month="1">Download February</a>
View rx-1-simulated.js
let months = {0: 'January', 1: 'February', 2: 'March' ...}
// A simulated series of download start requests
let download$ = interval(1000) // Every second
.map(i => months[i]) // becomes a month and we
.map(month => ({ // package it in an object
type: 'download/start',
payload: { file: month + '.csv' }
})
.take(3)
@deanius
deanius / observable-spawn.js
Last active Dec 18, 2018
Need an Observable from a program in another language? Get its stdout as an Observable as an external process!
View observable-spawn.js
var spawn = require('child_process').spawn;
const { Observable, timer } = require('rxjs')
const doAListing = new Observable(notify => {
var child = spawn('sh', ['-c', 'echo 1; sleep 1; echo 2; sleep 1; echo 3;']);
child.stdout.setEncoding('utf8')
child.stdout.on('data', function(data) {
const noNewline = data.replace(/\s$/, '');
notify.next(noNewline);
// 2.1 Uncomment to see how our cancellation function is needed
@deanius
deanius / after.md
Created Dec 4, 2018
Uses Antares' after function to force the return of a JSON array to occur in incremental chunks
View after.md

After is the setTimeout I always wanted!

after(60 * 1000, () => console.log("Its' been a minute, where've ya been?")

It's also an Observable, so if you're using it without Antares, it looks like:

after(60 * 1000, () => console.log("Its' been a minute, where've ya been?").subscribe()

The return value of the function you pass is emitted as the value of the Observable so:

You can’t perform that action at this time.