Last active
August 29, 2015 14:26
-
-
Save jhusain/3c510869ec091772c440 to your computer and use it in GitHub Desktop.
Observable map function
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
class Observable { | |
map(fn) { | |
if (typeof fn !== "function") | |
throw new TypeError(fn + " is not a function"); | |
let C = this.constructor[Symbol.species]; | |
return new C(observer => { | |
this[Symbol.observer]({ | |
start(subscription) { | |
this.subscription = subscription; | |
observer.start(subscription); | |
}, | |
next(value) { | |
try { value = fn(value) } | |
catch (e) { | |
this.subscription.dispose(); | |
return observer.error(e) | |
} | |
return observer.next(value); | |
}, | |
error(value) { return observer.error(value) }, | |
complete(value) { return observer.complete(value) }, | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment