Skip to content

Instantly share code, notes, and snippets.

View Raynos's full-sized avatar

Jake Verbaten Raynos

View GitHub Profile
var destinations = new WeakMap()
function MultiWrite(writable) {
var destinations = [writable]
var stream = new BaseWritableStream({
write: function (chunk) {
return Promise.all(destinations.map(function (dest) {
return dest.write(chunk)
}))
type EventedRepository<T> := {
store: (Array<T>, Callback<Array<T>>),
update: (id: String, delta: Object, Callback<T>),
remove: (id: String, Callback<void>),
drop: (Callback<void>),
sub: (namespace: String, opts: Object) => EventedRepository<X>,
getById: (id: String, Callback<T>),
getAll: (Callback<Array<T>>),
getFor: (key: String, value: Any, Callback<Array<T>>)
@Raynos
Raynos / 0readme.md
Created September 7, 2012 03:51
Crazy Streams

Lazy streams

A lazy stream takes a readable stream and wraps it.

Any reads or pipes on it propagate to the original stream.

This allows you to model your program flow as small transformations on streaming data and only apply them when you actually pipe the wrapped input stream into an output stream

@Raynos
Raynos / x.js
Created August 29, 2012 01:06
Content types
var types = ContentTypes({
"json": JsonStreamFactory
, "html": HtmlStreamFactory
})
function requestHandler(req, res) {
types(req, function write(data) {
}, function end() {
@Raynos
Raynos / index.js
Created August 22, 2012 06:43
leaderboard
function LeaderBoard(doc) {
var elem = Fragment(html)
, leaderBoardElem = elem.querySelector(".leaderboard")
, playerSet = doc.createSet("type", "player")
, oldSelected = selected.get("player")
, sel = selectron()
playerSet.forEach(addPlayer)
playerSet.on("add", addPlayer)
@Raynos
Raynos / buffer-streams.js
Created August 14, 2012 19:27
Buffer a stream and return a buffered stream
function BufferStream(stream, options) {
var bufferWritable = PauseStream().pause()
, bufferReadable = through()
, bufferStream = duplex(bufferWritable, bufferReadable)
bufferReadable.pipe(stream).pipe(bufferWritable)
stream.on("error", reemit)
return bufferStream
var pair = renderRoom(roomDiv, roomName, userName)
, write = pair[0]
, read = pair[1]
, duplex = es.duplex(write, read)
//write.pipe(roomStream).pipe(read) WRITE/READ WORKS
duplex.pipe(roomStream).pipe(duplex) // DUPLEX DOES NOTHING >:(
function createStream(meta) {
var ps = PauseStream()
, stream = mdm.createStream(meta)
, write = WriteStream()
, read = ReadStream()
write.pipe(ps).pipe(stream).pipe(read)
mdm.on("disconnect", function () {
ps.pause()
@Raynos
Raynos / creating-n-sized-lists.js
Created July 24, 2012 19:13
Creating n sized lists
// curry is ncurry(2)
var intoList = curry(function (size, list, elem) {
var lastElem = last(list)
if (lastElem && lastElem.length < size) {
lastElem.push(elem)
} else {
list.push([elem])
}
return list
})
@Raynos
Raynos / compose.js
Created June 19, 2012 19:02
Function composition
var slice = [].slice
function compose() {
var context = {}
return slice.call(arguments).reduceRight(combineFunctions)
function combineFunctions(memo, current) {
return applyInOrder