Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 26, 2012 21:14
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/3483629 to your computer and use it in GitHub Desktop.
Save Raynos/3483629 to your computer and use it in GitHub Desktop.
Improved sorta
var sort = sorta(Widget)
var stream = SpliceStream()
// A sorta instance is a writable splicestream. Pipe any readable splice stream into
// it
stream.pipe(sort)
var list = stream.createList()
list.add(...)
list.remove(...)
list.move(item, newpos)
list.sort(sortFunction)
// Called for every unique item added to the list
// Will pipe the widget in & out of the item (the item is a stream or an object
// which has a createStream method which will be called)
function Widget() {
var stream = DeltaStream()
/* render logic */
return stream
}
function sorta(createStream) {
var stream = SpliceStream()
, list = stream.createList()
, ul = document.createElement("ul")
, widgets = {}
list.on("add", function (stream) {
var sync = stream.sync
if (stream.createStream) {
stream = stream.createStream()
}
var widget = createStream()
widgets[stream.id] = widget
stream.pipe(widget).pipe(stream)
if (widget.appendTo) {
widget.appendTo(ul)
}
if (sync) {
sync(widget)
}
})
list.on("remove", function (stream) {
if (stream.createStream) {
stream = stream.createStream()
}
var widget = widgets[stream.id]
if (widget.destroy) {
widget.destroy()
}
})
list.on("move", function (stream) {
if (stream.createStream) {
stream = stream.createStream()
}
var widget = widgets[stream.id]
/* move logic */
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment