Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Last active September 18, 2016 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Widdershin/ab270fe8dd2e2756a2704d050007c4b2 to your computer and use it in GitHub Desktop.
Save Widdershin/ab270fe8dd2e2756a2704d050007c4b2 to your computer and use it in GitHub Desktop.
xstream producers
import fs from 'fs';
import assert from 'assert';
import xs from 'xstream';
function xray (url, classToScrape) {
return function scrape (callback) {
// goes and parses internetty stuff
callback(stuff);
}
}
xray('https://steveiscool.com', 'h1.name')(function (name) {
assert.equal(name, 'Steve');
});
xsfromCallback(xray('https://steveiscool.com', 'h1.name'))
.addListener({
next (name) {
assert.equal(name, 'Steve');
},
// ...
})
function xsFromCallback (callbackableFunction) {
const stream = xs.create();
callbackableFunction(function (data) {
stream.shamefullySendNext(data);
})
return stream;
}
function xsFromCallback (callbackableFunction) {
return xs.create({
start (subject) {
callbackableFunction(function (data) {
subject.next(data);
});
},
stop () {
}
});
}
function xsFromWebsocket (socket) {
return xs.create({
start (subject) {
socket.onmessage = function (data) {
subject.next(data);
};
},
stop () {
}
});
}
xsFromWebsocket(new WebSocket('ws://localhost:3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment