FileReader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. const Blob = require('./Blob');
  12. const EventTarget = require('event-target-shim');
  13. import NativeFileReaderModule from './NativeFileReaderModule';
  14. type ReadyState =
  15. | 0 // EMPTY
  16. | 1 // LOADING
  17. | 2; // DONE
  18. type ReaderResult = string | ArrayBuffer;
  19. const READER_EVENTS = [
  20. 'abort',
  21. 'error',
  22. 'load',
  23. 'loadstart',
  24. 'loadend',
  25. 'progress',
  26. ];
  27. const EMPTY = 0;
  28. const LOADING = 1;
  29. const DONE = 2;
  30. class FileReader extends (EventTarget(...READER_EVENTS): any) {
  31. static EMPTY: number = EMPTY;
  32. static LOADING: number = LOADING;
  33. static DONE: number = DONE;
  34. EMPTY: number = EMPTY;
  35. LOADING: number = LOADING;
  36. DONE: number = DONE;
  37. _readyState: ReadyState;
  38. _error: ?Error;
  39. _result: ?ReaderResult;
  40. _aborted: boolean = false;
  41. _subscriptions: Array<*> = [];
  42. constructor() {
  43. super();
  44. this._reset();
  45. }
  46. _reset(): void {
  47. this._readyState = EMPTY;
  48. this._error = null;
  49. this._result = null;
  50. }
  51. _clearSubscriptions(): void {
  52. this._subscriptions.forEach(sub => sub.remove());
  53. this._subscriptions = [];
  54. }
  55. _setReadyState(newState: ReadyState) {
  56. this._readyState = newState;
  57. this.dispatchEvent({type: 'readystatechange'});
  58. if (newState === DONE) {
  59. if (this._aborted) {
  60. this.dispatchEvent({type: 'abort'});
  61. } else if (this._error) {
  62. this.dispatchEvent({type: 'error'});
  63. } else {
  64. this.dispatchEvent({type: 'load'});
  65. }
  66. this.dispatchEvent({type: 'loadend'});
  67. }
  68. }
  69. readAsArrayBuffer() {
  70. throw new Error('FileReader.readAsArrayBuffer is not implemented');
  71. }
  72. readAsDataURL(blob: ?Blob) {
  73. this._aborted = false;
  74. if (blob == null) {
  75. throw new TypeError(
  76. "Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'",
  77. );
  78. }
  79. NativeFileReaderModule.readAsDataURL(blob.data).then(
  80. (text: string) => {
  81. if (this._aborted) {
  82. return;
  83. }
  84. this._result = text;
  85. this._setReadyState(DONE);
  86. },
  87. error => {
  88. if (this._aborted) {
  89. return;
  90. }
  91. this._error = error;
  92. this._setReadyState(DONE);
  93. },
  94. );
  95. }
  96. readAsText(blob: ?Blob, encoding: string = 'UTF-8') {
  97. this._aborted = false;
  98. if (blob == null) {
  99. throw new TypeError(
  100. "Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'",
  101. );
  102. }
  103. NativeFileReaderModule.readAsText(blob.data, encoding).then(
  104. (text: string) => {
  105. if (this._aborted) {
  106. return;
  107. }
  108. this._result = text;
  109. this._setReadyState(DONE);
  110. },
  111. error => {
  112. if (this._aborted) {
  113. return;
  114. }
  115. this._error = error;
  116. this._setReadyState(DONE);
  117. },
  118. );
  119. }
  120. abort() {
  121. this._aborted = true;
  122. // only call onreadystatechange if there is something to abort, as per spec
  123. if (this._readyState !== EMPTY && this._readyState !== DONE) {
  124. this._reset();
  125. this._setReadyState(DONE);
  126. }
  127. // Reset again after, in case modified in handler
  128. this._reset();
  129. }
  130. get readyState(): ReadyState {
  131. return this._readyState;
  132. }
  133. get error(): ?Error {
  134. return this._error;
  135. }
  136. get result(): ?ReaderResult {
  137. return this._result;
  138. }
  139. }
  140. module.exports = FileReader;