-
-
Save fariazz/9196bcb8f1c076c0d2d8 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/hobeji
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
function Observable(forEach) { | |
this._forEach = forEach; | |
} | |
var noop = function noop() {}; | |
Observable.prototype.forEach = function (observerOrOnNext, onError, onCompleted) { | |
if (typeof observerOrOnNext === "function") { | |
return this._forEach({ | |
onNext: observerOrOnNext, | |
onError: onError || noop, | |
onCompleted: onCompleted || noop | |
}); | |
} else { | |
return this._forEach(observerOrOnNext); | |
} | |
}; | |
Observable.of = function () { | |
debugger; | |
var args = Array.prototype.slice.call(arguments); | |
return new Observable(function forEach(observer) { | |
var unsubscribed = false; | |
var handle = setTimeout(function () { | |
for (var counter = 0; counter < args.length && !unsubscribed; counter++) { | |
observer.onNext(args[counter]); | |
} | |
if (!unsubscribed) { | |
observer.onCompleted(); | |
} | |
}); | |
return { | |
dispose: function dispose() { | |
clearTimeout(handle); | |
unsubscribed = true; | |
} | |
}; | |
}); | |
}; | |
Observable.prototype.map = function (projectionFunction) { | |
var self = this; | |
return new Observable(function forEach(observer) { | |
return self.forEach(function onNext(item) { | |
observer.onNext(projectionFunction(item)); | |
}, function onError(error) { | |
observer.onError(error); | |
}, function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
Observable.prototype.filter = function (predicateFunction) { | |
var self = this; | |
return new Observable(function forEach(observer) { | |
return self.forEach(function onNext(item) { | |
if (predicateFunction(item)) { | |
observer.onNext(item); | |
} | |
}, function onError(error) { | |
observer.onError(error); | |
}, function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
var subscription = Observable.of(1, 2, 3).map(function (x) { | |
return x + 1; | |
}).filter(function (x) { | |
return x > 2; | |
}).forEach(function (x) { | |
console.log(x); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function Observable(forEach) { | |
this._forEach = forEach; | |
} | |
var noop = function() { }; | |
Observable.prototype.forEach = function( | |
observerOrOnNext, | |
onError, | |
onCompleted) { | |
if (typeof observerOrOnNext === "function") { | |
return this._forEach({ | |
onNext: observerOrOnNext, | |
onError: onError || noop, | |
onCompleted: onCompleted || noop | |
}); | |
} | |
else { | |
return this._forEach(observerOrOnNext); | |
} | |
}; | |
Observable.of = function() { | |
debugger; | |
var args = Array.prototype.slice.call(arguments); | |
return new Observable(function forEach(observer) { | |
var unsubscribed = false; | |
var handle = setTimeout(function() { | |
for(var counter = 0; counter < args.length && !unsubscribed; counter++) { | |
observer.onNext(args[counter]); | |
} | |
if (!unsubscribed) { | |
observer.onCompleted(); | |
} | |
}); | |
return { | |
dispose: function() { | |
clearTimeout(handle); | |
unsubscribed = true; | |
} | |
}; | |
}); | |
}; | |
Observable.prototype.map = function(projectionFunction) { | |
var self = this; | |
return new Observable(function forEach(observer){ | |
return self.forEach( | |
function onNext(item) { | |
observer.onNext(projectionFunction(item)); | |
}, | |
function onError(error) { | |
observer.onError(error); | |
}, | |
function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
Observable.prototype.filter = function(predicateFunction) { | |
var self = this; | |
return new Observable(function forEach(observer){ | |
return self.forEach( | |
function onNext(item) { | |
if (predicateFunction(item)) { | |
observer.onNext(item); | |
} | |
}, | |
function onError(error) { | |
observer.onError(error); | |
}, | |
function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
var subscription = | |
Observable. | |
of(1,2,3). | |
map(x => x + 1). | |
filter(x => x > 2). | |
forEach(x => { | |
console.log(x); | |
});</script></body> | |
</html> |
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
"use strict"; | |
function Observable(forEach) { | |
this._forEach = forEach; | |
} | |
var noop = function noop() {}; | |
Observable.prototype.forEach = function (observerOrOnNext, onError, onCompleted) { | |
if (typeof observerOrOnNext === "function") { | |
return this._forEach({ | |
onNext: observerOrOnNext, | |
onError: onError || noop, | |
onCompleted: onCompleted || noop | |
}); | |
} else { | |
return this._forEach(observerOrOnNext); | |
} | |
}; | |
Observable.of = function () { | |
debugger; | |
var args = Array.prototype.slice.call(arguments); | |
return new Observable(function forEach(observer) { | |
var unsubscribed = false; | |
var handle = setTimeout(function () { | |
for (var counter = 0; counter < args.length && !unsubscribed; counter++) { | |
observer.onNext(args[counter]); | |
} | |
if (!unsubscribed) { | |
observer.onCompleted(); | |
} | |
}); | |
return { | |
dispose: function dispose() { | |
clearTimeout(handle); | |
unsubscribed = true; | |
} | |
}; | |
}); | |
}; | |
Observable.prototype.map = function (projectionFunction) { | |
var self = this; | |
return new Observable(function forEach(observer) { | |
return self.forEach(function onNext(item) { | |
observer.onNext(projectionFunction(item)); | |
}, function onError(error) { | |
observer.onError(error); | |
}, function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
Observable.prototype.filter = function (predicateFunction) { | |
var self = this; | |
return new Observable(function forEach(observer) { | |
return self.forEach(function onNext(item) { | |
if (predicateFunction(item)) { | |
observer.onNext(item); | |
} | |
}, function onError(error) { | |
observer.onError(error); | |
}, function onCompleted() { | |
observer.onCompleted(); | |
}); | |
}); | |
}; | |
var subscription = Observable.of(1, 2, 3).map(function (x) { | |
return x + 1; | |
}).filter(function (x) { | |
return x > 2; | |
}).forEach(function (x) { | |
console.log(x); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment