-
Notifications
You must be signed in to change notification settings - Fork 2
/
imageBase64.ts
36 lines (30 loc) · 864 Bytes
/
imageBase64.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Source } from '../index';
import { createSource } from './';
/**
* Create a source that convert a blob image to base64 string
*
* @param image - image blob
* @return callbag source
*
* @public
*/
function imageBase64(image: Blob): Source<string> {
return createSource((next, complete, error) => {
const fileReader = new FileReader();
const handleLoadend = () => {
next(fileReader.result as string);
complete();
};
const handleError = (err: any) => {
error(err);
};
fileReader.addEventListener('loadend', handleLoadend);
fileReader.addEventListener('error', handleError);
fileReader.readAsDataURL(image);
return () => {
fileReader.removeEventListener('loadend', handleLoadend);
fileReader.removeEventListener('error', handleError);
};
});
}
export default imageBase64;