@@ -44,6 +44,7 @@ class FileReader extends EventTarget {
4444 _error : ?Error ;
4545 _result : ?ReaderResult ;
4646 _aborted : boolean = false ;
47+ _readId : number = 0 ;
4748
4849 constructor ( ) {
4950 super ( ) ;
@@ -56,6 +57,15 @@ class FileReader extends EventTarget {
5657 this . _result = null ;
5758 }
5859
60+ _startRead ( ) : number {
61+ this . _aborted = false ;
62+ this . _error = null ;
63+ this . _result = null ;
64+ const readId = ++ this . _readId ;
65+ this . _setReadyState ( LOADING ) ;
66+ return readId ;
67+ }
68+
5969 _setReadyState ( newState : ReadyState ) {
6070 this . _readyState = newState ;
6171 this . dispatchEvent ( new Event ( 'readystatechange' ) ) ;
@@ -67,24 +77,24 @@ class FileReader extends EventTarget {
6777 } else {
6878 this . dispatchEvent ( new Event ( 'load' ) ) ;
6979 }
70- this . dispatchEvent ( new Event ( 'loadend' ) ) ;
80+ if ( this . _readyState !== LOADING ) {
81+ this . dispatchEvent ( new Event ( 'loadend' ) ) ;
82+ }
7183 }
7284 }
7385
7486 readAsArrayBuffer ( blob : ?Blob ) : void {
75- this . _aborted = false ;
76-
7787 if ( blob == null ) {
7888 throw new TypeError (
7989 "Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'" ,
8090 ) ;
8191 }
8292
83- this . _setReadyState ( LOADING ) ;
93+ const readId = this . _startRead ( ) ;
8494
8595 NativeFileReaderModule . readAsDataURL ( blob . data ) . then (
8696 ( text : string ) => {
87- if ( this . _aborted ) {
97+ if ( readId !== this . _readId ) {
8898 return ;
8999 }
90100
@@ -95,7 +105,7 @@ class FileReader extends EventTarget {
95105 this . _setReadyState ( DONE ) ;
96106 } ,
97107 error => {
98- if ( this . _aborted ) {
108+ if ( readId !== this . _readId ) {
99109 return ;
100110 }
101111 this . _error = error ;
@@ -105,26 +115,24 @@ class FileReader extends EventTarget {
105115 }
106116
107117 readAsDataURL ( blob : ?Blob ) : void {
108- this . _aborted = false ;
109-
110118 if ( blob == null ) {
111119 throw new TypeError (
112120 "Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'" ,
113121 ) ;
114122 }
115123
116- this . _setReadyState ( LOADING ) ;
124+ const readId = this . _startRead ( ) ;
117125
118126 NativeFileReaderModule . readAsDataURL ( blob . data ) . then (
119127 ( text : string ) => {
120- if ( this . _aborted ) {
128+ if ( readId !== this . _readId ) {
121129 return ;
122130 }
123131 this . _result = text ;
124132 this . _setReadyState ( DONE ) ;
125133 } ,
126134 error => {
127- if ( this . _aborted ) {
135+ if ( readId !== this . _readId ) {
128136 return ;
129137 }
130138 this . _error = error ;
@@ -134,26 +142,24 @@ class FileReader extends EventTarget {
134142 }
135143
136144 readAsText ( blob : ?Blob , encoding : string = 'UTF-8' ) : void {
137- this . _aborted = false ;
138-
139145 if ( blob == null ) {
140146 throw new TypeError (
141147 "Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'" ,
142148 ) ;
143149 }
144150
145- this . _setReadyState ( LOADING ) ;
151+ const readId = this . _startRead ( ) ;
146152
147153 NativeFileReaderModule . readAsText ( blob . data , encoding ) . then (
148154 ( text : string ) => {
149- if ( this . _aborted ) {
155+ if ( readId !== this . _readId ) {
150156 return ;
151157 }
152158 this . _result = text ;
153159 this . _setReadyState ( DONE ) ;
154160 } ,
155161 error => {
156- if ( this . _aborted ) {
162+ if ( readId !== this . _readId ) {
157163 return ;
158164 }
159165 this . _error = error ;
@@ -163,14 +169,12 @@ class FileReader extends EventTarget {
163169 }
164170
165171 abort ( ) {
166- this . _aborted = true ;
167- // only call onreadystatechange if there is something to abort, as per spec
168- if ( this . _readyState !== EMPTY && this . _readyState !== DONE ) {
169- this . _reset ( ) ;
172+ this . _result = null ;
173+ if ( this . _readyState === LOADING ) {
174+ this . _aborted = true ;
175+ this . _readId ++ ;
170176 this . _setReadyState ( DONE ) ;
171177 }
172- // Reset again after, in case modified in handler
173- this . _reset ( ) ;
174178 }
175179
176180 get readyState ( ) : ReadyState {
0 commit comments