Skip to content

Commit af75e9a

Browse files
committed
fix: add W3C FileReader spec compliance for state transitions
FileReader was missing LOADING state guard (should throw InvalidStateError if already reading per W3C spec), state transition to LOADING before async read, and loadstart event dispatch. Added all three to comply with the W3C FileReader specification. Signed-off-by: Srikanth Patchava <spatchava@meta.com> Signed-off-by: Srikanth Patchava <srikanth.patchava@outlook.com>
1 parent 0af2ed6 commit af75e9a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

packages/react-native/Libraries/Blob/FileReader.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class FileReader extends EventTarget {
7272
}
7373

7474
readAsArrayBuffer(blob: ?Blob): void {
75+
if (this._readyState === LOADING) {
76+
throw new DOMException(
77+
'The object is in an invalid state.',
78+
'InvalidStateError',
79+
);
80+
}
81+
7582
this._aborted = false;
7683

7784
if (blob == null) {
@@ -80,6 +87,9 @@ class FileReader extends EventTarget {
8087
);
8188
}
8289

90+
this._readyState = LOADING;
91+
this.dispatchEvent(new Event('loadstart'));
92+
8393
NativeFileReaderModule.readAsDataURL(blob.data).then(
8494
(text: string) => {
8595
if (this._aborted) {
@@ -103,6 +113,13 @@ class FileReader extends EventTarget {
103113
}
104114

105115
readAsDataURL(blob: ?Blob): void {
116+
if (this._readyState === LOADING) {
117+
throw new DOMException(
118+
'The object is in an invalid state.',
119+
'InvalidStateError',
120+
);
121+
}
122+
106123
this._aborted = false;
107124

108125
if (blob == null) {
@@ -111,6 +128,9 @@ class FileReader extends EventTarget {
111128
);
112129
}
113130

131+
this._readyState = LOADING;
132+
this.dispatchEvent(new Event('loadstart'));
133+
114134
NativeFileReaderModule.readAsDataURL(blob.data).then(
115135
(text: string) => {
116136
if (this._aborted) {
@@ -130,6 +150,13 @@ class FileReader extends EventTarget {
130150
}
131151

132152
readAsText(blob: ?Blob, encoding: string = 'UTF-8'): void {
153+
if (this._readyState === LOADING) {
154+
throw new DOMException(
155+
'The object is in an invalid state.',
156+
'InvalidStateError',
157+
);
158+
}
159+
133160
this._aborted = false;
134161

135162
if (blob == null) {
@@ -138,6 +165,9 @@ class FileReader extends EventTarget {
138165
);
139166
}
140167

168+
this._readyState = LOADING;
169+
this.dispatchEvent(new Event('loadstart'));
170+
141171
NativeFileReaderModule.readAsText(blob.data, encoding).then(
142172
(text: string) => {
143173
if (this._aborted) {

0 commit comments

Comments
 (0)