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
// 読み込むとき、ES6 ( + browserify) だと便利 | |
import Rx from "rx"; // ES6 modules | |
var images = ["1.png", "2.png", "3.png"]; | |
function loadImages() { | |
return Rx.Observable.create(loadImagesObservable); | |
} | |
function loadImagesObservable(observer) { | |
var index = 0; | |
var loadImage = (name) => { | |
var elem = document.createElement("image"); | |
elem.src = name; | |
elem.onload = function() { | |
observer.onNext(index); | |
if (index >= images.length - 1) { | |
observer.onCompleted(); | |
} else { | |
loadImage(images[++index]); | |
} | |
} | |
}; | |
loadImage(images[index]); | |
return function() { console.log("廃棄されました。"); }; | |
} | |
function main() { | |
var subscription = loadImages().filter(index => index % 5 === 0).subscribe((index) => { | |
console.log(index + "個目を読み込みました"); | |
}, err => { | |
console.log("Observe Error: " + err); | |
}, () => { | |
console.log("読み込み完了"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment