Skip to content

Commit 68c269c

Browse files
authored
More correct filereader event firing (nodejs#1796)
1 parent 9fac791 commit 68c269c

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lib/fileapi/filereader.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ class FileReader extends EventTarget {
182182
set onloadend (fn) {
183183
webidl.brandCheck(this, FileReader)
184184

185+
if (this[kEvents].loadend) {
186+
this.removeEventListener('loadend', this[kEvents].loadend)
187+
}
188+
185189
if (typeof fn === 'function') {
186190
this[kEvents].loadend = fn
191+
this.addEventListener('loadend', fn)
187192
} else {
188193
this[kEvents].loadend = null
189194
}
@@ -198,8 +203,13 @@ class FileReader extends EventTarget {
198203
set onerror (fn) {
199204
webidl.brandCheck(this, FileReader)
200205

206+
if (this[kEvents].error) {
207+
this.removeEventListener('error', this[kEvents].error)
208+
}
209+
201210
if (typeof fn === 'function') {
202211
this[kEvents].error = fn
212+
this.addEventListener('error', fn)
203213
} else {
204214
this[kEvents].error = null
205215
}
@@ -214,8 +224,13 @@ class FileReader extends EventTarget {
214224
set onloadstart (fn) {
215225
webidl.brandCheck(this, FileReader)
216226

227+
if (this[kEvents].loadstart) {
228+
this.removeEventListener('loadstart', this[kEvents].loadstart)
229+
}
230+
217231
if (typeof fn === 'function') {
218232
this[kEvents].loadstart = fn
233+
this.addEventListener('loadstart', fn)
219234
} else {
220235
this[kEvents].loadstart = null
221236
}
@@ -230,8 +245,13 @@ class FileReader extends EventTarget {
230245
set onprogress (fn) {
231246
webidl.brandCheck(this, FileReader)
232247

248+
if (this[kEvents].progress) {
249+
this.removeEventListener('progress', this[kEvents].progress)
250+
}
251+
233252
if (typeof fn === 'function') {
234253
this[kEvents].progress = fn
254+
this.addEventListener('progress', fn)
235255
} else {
236256
this[kEvents].progress = null
237257
}
@@ -246,8 +266,13 @@ class FileReader extends EventTarget {
246266
set onload (fn) {
247267
webidl.brandCheck(this, FileReader)
248268

269+
if (this[kEvents].load) {
270+
this.removeEventListener('load', this[kEvents].load)
271+
}
272+
249273
if (typeof fn === 'function') {
250274
this[kEvents].load = fn
275+
this.addEventListener('load', fn)
251276
} else {
252277
this[kEvents].load = null
253278
}
@@ -262,8 +287,13 @@ class FileReader extends EventTarget {
262287
set onabort (fn) {
263288
webidl.brandCheck(this, FileReader)
264289

290+
if (this[kEvents].abort) {
291+
this.removeEventListener('abort', this[kEvents].abort)
292+
}
293+
265294
if (typeof fn === 'function') {
266295
this[kEvents].abort = fn
296+
this.addEventListener('abort', fn)
267297
} else {
268298
this[kEvents].abort = null
269299
}

lib/fileapi/util.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,19 @@ function readOperation (fr, blob, type, encodingName) {
191191

192192
/**
193193
* @see https://w3c.github.io/FileAPI/#fire-a-progress-event
194+
* @see https://dom.spec.whatwg.org/#concept-event-fire
194195
* @param {string} e The name of the event
195196
* @param {import('./filereader').FileReader} reader
196197
*/
197198
function fireAProgressEvent (e, reader) {
199+
// The progress event e does not bubble. e.bubbles must be false
200+
// The progress event e is NOT cancelable. e.cancelable must be false
198201
const event = new ProgressEvent(e, {
199202
bubbles: false,
200203
cancelable: false
201204
})
202205

203206
reader.dispatchEvent(event)
204-
try {
205-
// eslint-disable-next-line no-useless-call
206-
reader[`on${e}`]?.call(reader, event)
207-
} catch (err) {
208-
// Prevent the error from being swallowed
209-
queueMicrotask(() => {
210-
throw err
211-
})
212-
}
213207
}
214208

215209
/**

0 commit comments

Comments
 (0)