Created
February 6, 2017 14:48
-
-
Save anonymous/4f0760118ffbde58c248ea010817899e to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zonowoc
This file contains hidden or 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> | |
| <button id="button">Click me</button> | |
| <script id="jsbin-javascript"> | |
| 'use strict'; | |
| var button = document.getElementById('button'); | |
| function Observable(forEach) { | |
| this._forEach = forEach; | |
| } | |
| Observable.prototype = { | |
| forEach: function forEach(onNext, onError, onCompleted) { | |
| // are they passing in functions | |
| if (typeof onNext === "function") { | |
| return this._forEach({ | |
| onNext: onNext, | |
| onError: onError || function () {}, | |
| onCompleted: onCompleted || function () {} | |
| }); | |
| } else { | |
| // are they passing an Observer | |
| return this._forEach(onNext); | |
| } | |
| }, | |
| map: function map(projectionFunction) { | |
| var self = this; | |
| // mapped observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach(function onNext(x) { | |
| observer.onNext(projectionFunction(x)); | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| }); | |
| }, | |
| filter: function filter(testFunction) { | |
| var self = this; | |
| // filtered observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach(function onNext(x) { | |
| if (testFunction(x)) { | |
| observer.onNext(x); | |
| } | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| }); | |
| }, | |
| take: function take(num) { | |
| var self = this; | |
| // take observable | |
| return new Observable(function forEach(observer) { | |
| var counter = 0, | |
| subscription = self.forEach(function onNext(v) { | |
| observer.onNext(v); | |
| counter++; | |
| if (counter === num) { | |
| observer.onCompleted(); | |
| subscription.dispose(); | |
| } | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| return subscription; | |
| }); | |
| } | |
| }; | |
| Observable.fromEvent = function (dom, eventName) { | |
| return new Observable(function forEach(observer) { | |
| var handler = function handler(e) { | |
| return observer.onNext(e); | |
| }; | |
| dom.addEventListener(eventName, handler); | |
| // Subscription | |
| return { | |
| dispose: function dispose() { | |
| dom.removeEventListener(eventName, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| Observable.observations = function (obj) { | |
| return new Observable(function forEach(observer) { | |
| var handler = function handler(e) { | |
| return observer.onNext(e); | |
| }; | |
| Object.observe(obj, handler); | |
| // Subscription | |
| return { | |
| dispose: function dispose() { | |
| Object.unobserve(obj, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| /* | |
| var clicks = | |
| Observable. | |
| fromEvent(button, "click"). | |
| filter(e => e.pageX > 40). | |
| map(e => e.pageX + "px"); | |
| */ | |
| var person = { name: 'Jim' }; | |
| Object.observations(person).forEach(function (changes) { | |
| console.log(changes); | |
| }); | |
| person.name = "Don"; | |
| person.age = 23; | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">var button = document.getElementById('button'); | |
| function Observable(forEach) { | |
| this._forEach = forEach; | |
| } | |
| Observable.prototype = { | |
| forEach: function(onNext, onError, onCompleted) { | |
| // are they passing in functions | |
| if (typeof onNext === "function") { | |
| return this._forEach({ | |
| onNext: onNext, | |
| onError: onError || function() {}, | |
| onCompleted: onCompleted || function() {} | |
| }); | |
| } | |
| else { | |
| // are they passing an Observer | |
| return this._forEach(onNext); | |
| } | |
| }, | |
| map: function(projectionFunction) { | |
| var self = this; | |
| // mapped observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach( | |
| function onNext(x) { observer.onNext(projectionFunction(x)); }, | |
| function onError(e) { observer.onError(e); }, | |
| function onCompleted() { observer.onCompleted(); }); | |
| }); | |
| }, | |
| filter: function(testFunction) { | |
| var self = this; | |
| // filtered observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach( | |
| function onNext(x) { | |
| if (testFunction(x)) { | |
| observer.onNext(x); | |
| } | |
| }, | |
| function onError(e) { observer.onError(e); }, | |
| function onCompleted() { observer.onCompleted(); }); | |
| }); | |
| }, | |
| take: function(num) { | |
| var self = this; | |
| // take observable | |
| return new Observable(function forEach(observer) { | |
| var counter = 0, | |
| subscription = self.forEach( | |
| function onNext(v) { | |
| observer.onNext(v); | |
| counter++; | |
| if (counter === num) { | |
| observer.onCompleted(); | |
| subscription.dispose(); | |
| } | |
| }, | |
| function onError(e) { | |
| observer.onError(e); | |
| }, | |
| function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| return subscription; | |
| }); | |
| } | |
| }; | |
| Observable.fromEvent = function(dom, eventName) { | |
| return new Observable(function forEach(observer) { | |
| var handler = (e) => observer.onNext(e); | |
| dom.addEventListener(eventName, handler); | |
| // Subscription | |
| return { | |
| dispose: () => { | |
| dom.removeEventListener(eventName, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| Observable.observations = function(obj) { | |
| return new Observable(function forEach(observer) { | |
| var handler = (e) => observer.onNext(e); | |
| Object.observe(obj, handler); | |
| // Subscription | |
| return { | |
| dispose: () => { | |
| Object.unobserve(obj, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| /* | |
| var clicks = | |
| Observable. | |
| fromEvent(button, "click"). | |
| filter(e => e.pageX > 40). | |
| map(e => e.pageX + "px"); | |
| */ | |
| var person = { name: 'Jim'}; | |
| Object.observations(person).forEach(changes => { | |
| console.log(changes); | |
| }); | |
| person.name = "Don"; | |
| person.age = 23; | |
| </script></body> | |
| </html> |
This file contains hidden or 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'; | |
| var button = document.getElementById('button'); | |
| function Observable(forEach) { | |
| this._forEach = forEach; | |
| } | |
| Observable.prototype = { | |
| forEach: function forEach(onNext, onError, onCompleted) { | |
| // are they passing in functions | |
| if (typeof onNext === "function") { | |
| return this._forEach({ | |
| onNext: onNext, | |
| onError: onError || function () {}, | |
| onCompleted: onCompleted || function () {} | |
| }); | |
| } else { | |
| // are they passing an Observer | |
| return this._forEach(onNext); | |
| } | |
| }, | |
| map: function map(projectionFunction) { | |
| var self = this; | |
| // mapped observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach(function onNext(x) { | |
| observer.onNext(projectionFunction(x)); | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| }); | |
| }, | |
| filter: function filter(testFunction) { | |
| var self = this; | |
| // filtered observable | |
| return new Observable(function forEach(observer) { | |
| return self.forEach(function onNext(x) { | |
| if (testFunction(x)) { | |
| observer.onNext(x); | |
| } | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| }); | |
| }, | |
| take: function take(num) { | |
| var self = this; | |
| // take observable | |
| return new Observable(function forEach(observer) { | |
| var counter = 0, | |
| subscription = self.forEach(function onNext(v) { | |
| observer.onNext(v); | |
| counter++; | |
| if (counter === num) { | |
| observer.onCompleted(); | |
| subscription.dispose(); | |
| } | |
| }, function onError(e) { | |
| observer.onError(e); | |
| }, function onCompleted() { | |
| observer.onCompleted(); | |
| }); | |
| return subscription; | |
| }); | |
| } | |
| }; | |
| Observable.fromEvent = function (dom, eventName) { | |
| return new Observable(function forEach(observer) { | |
| var handler = function handler(e) { | |
| return observer.onNext(e); | |
| }; | |
| dom.addEventListener(eventName, handler); | |
| // Subscription | |
| return { | |
| dispose: function dispose() { | |
| dom.removeEventListener(eventName, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| Observable.observations = function (obj) { | |
| return new Observable(function forEach(observer) { | |
| var handler = function handler(e) { | |
| return observer.onNext(e); | |
| }; | |
| Object.observe(obj, handler); | |
| // Subscription | |
| return { | |
| dispose: function dispose() { | |
| Object.unobserve(obj, handler); | |
| } | |
| }; | |
| }); | |
| }; | |
| /* | |
| var clicks = | |
| Observable. | |
| fromEvent(button, "click"). | |
| filter(e => e.pageX > 40). | |
| map(e => e.pageX + "px"); | |
| */ | |
| var person = { name: 'Jim' }; | |
| Object.observations(person).forEach(function (changes) { | |
| console.log(changes); | |
| }); | |
| person.name = "Don"; | |
| person.age = 23; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment