Last active
September 18, 2016 23:39
-
-
Save Widdershin/ab270fe8dd2e2756a2704d050007c4b2 to your computer and use it in GitHub Desktop.
xstream producers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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