Created
August 26, 2012 21:14
-
-
Save Raynos/3483629 to your computer and use it in GitHub Desktop.
Improved sorta
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
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 | |
} |
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
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