Skip to content

Instantly share code, notes, and snippets.

@danyx23
Last active August 29, 2015 14:20
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 danyx23/d4882a3e52be8f139935 to your computer and use it in GitHub Desktop.
Save danyx23/d4882a3e52be8f139935 to your computer and use it in GitHub Desktop.
Cycle model draft (not working) for more complex objects
// This doesn't work, it's just a draft while thinking how best to model collections of non-trivial objects
const model = (function () {
const mediaAsset$ = createStream(droppedFiles$.filter(droppedFile => droppedFile.type.match('image.*'))
.map (droppedfile => {
const reader = new FileReader();
const asset = {
id = cuid(),
name = droppedfile.name,
imageLoadSucceeded$ = Rx.Observable.fromEvent(reader.onload).map(_ => reader.result),
imageLoadFailed$ = Rx.Observable.fromEvent(reader.onerror).map(_ => false),
imageLoadProgress$ = Rx.Observable.fromEvent(reader.onprogress).map(eventData =>
eventData.lengthComputable ? (eventData.loaded / eventData.total) : 0)
};
reader.readAsDataURL(droppedfile);
return asset;
}));
const collectedMediaAssets = mediaAsset$.scan(new Map(), (itemMap, item) => itemMap.set(item.id, item));
return {
mediaAsset$,
collectedMediaAssets,
inject(intent) {
mediaAsset$.inject(intent.droppedFiles$);
return intent;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment