Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 26, 2012 20:15
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 Raynos/3483305 to your computer and use it in GitHub Desktop.
Save Raynos/3483305 to your computer and use it in GitHub Desktop.
Splice Stream

I prefer to think more heavily in streams.

A sequence is a stream of splice commands. A listWidget is a writable splicestream. A crdt Sequence is a readable splice stream.

SpliceStream works incredibly well if all your add / remove / move commands manipulate delta streams. I.e. the list interpretation of the splice stream is a list of delta streams.

The only issue with both splicestream and deltastream is initial synchronization of data. I need to think about that more

var SpliceStream = require("splice-stream")

// Seq is a splice stream
var stream = crdtSeq.createStream()

// List widget is a splice stream
// Every Time a splice event comes in with order changes for a crdt row
// Then either it's move or a new ItemWidget is created for that row
// Item widget is a delta stream
var widget = ListWidget(ItemWidget)

// Go!
stream.pipe(widget)

function ListWidget(ItemWidget) {
    var stream = SpliceStream()
        , splicer = stream.createSplicer()
        , ul = document.createElement("ul")

    splicer.on("add", function (row) {
        var widget = ItemWidget()
        widget.appendTo(ul)
        row.createStream().pipe(widget)
    })

    ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment