Created
June 5, 2017 19:36
-
-
Save iansinnott/3d0ba1e9edc3e6967bc51da7020926b0 to your computer and use it in GitHub Desktop.
A simple Rx abstraction over the FileReader API
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
/** | |
* Read the text contents of a File or Blob using the FileReader interface. | |
* This is an async interface so it makes sense to handle it with Rx. | |
* @param {blob} File | Blob | |
* @return Observable<string> | |
*/ | |
const readFile = (blob) => Observable.create(obs => { | |
if (!(blob instanceof Blob)) { | |
obs.error(new Error('`blob` must be an instance of File or Blob.')); | |
return; | |
} | |
const reader = new FileReader(); | |
reader.onerror = err => obs.error(err); | |
reader.onabort = err => obs.error(err); | |
reader.onload = () => obs.next(reader.result); | |
reader.onloadend = () => obs.complete(); | |
return reader.readAsText(blob); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is a simple implementation of opening an image in browser using RxJS 7.1
a working fiddle is here: https://jsfiddle.net/MuazSamli/5urd9bcq/25/