Skip to content

Commit

Permalink
track in-progress preflights (fixes #2965) (#3004)
Browse files Browse the repository at this point in the history
* track in-progress preflights (fixes #2965)

* Update assets/js/phoenix_live_view/upload_entry.js

---------

Co-authored-by: Chris McCord <chris@chrismccord.com>
  • Loading branch information
SteffenDE and chrismccord committed Jan 17, 2024
1 parent 4a0e36b commit 62792e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion assets/js/phoenix_live_view/live_uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export default class LiveUploader {
}

static filesAwaitingPreflight(input){
return this.activeFiles(input).filter(f => !UploadEntry.isPreflighted(input, f))
return this.activeFiles(input).filter(f => !UploadEntry.isPreflighted(input, f) && !UploadEntry.isPreflightInProgress(f))
}

static markPreflightInProgress(entries){
entries.forEach(entry => UploadEntry.markPreflightInProgress(entry.file))
}

constructor(inputEl, view, onComplete){
Expand All @@ -104,6 +108,9 @@ export default class LiveUploader {
Array.from(LiveUploader.filesAwaitingPreflight(inputEl) || [])
.map(file => new UploadEntry(inputEl, file, view))

// prevent sending duplicate preflight requests
LiveUploader.markPreflightInProgress(this._entries)

this.numEntriesInProgress = this._entries.length
}

Expand Down
11 changes: 10 additions & 1 deletion assets/js/phoenix_live_view/upload_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import DOM from "./dom"
export default class UploadEntry {
static isActive(fileEl, file){
let isNew = file._phxRef === undefined
let isPreflightInProgress = UploadEntry.isPreflightInProgress(file)
let activeRefs = fileEl.getAttribute(PHX_ACTIVE_ENTRY_REFS).split(",")
let isActive = activeRefs.indexOf(LiveUploader.genFileRef(file)) >= 0
return file.size > 0 && (isNew || isActive)
return file.size > 0 && (isNew || isActive || !isPreflightInProgress)
}

static isPreflighted(fileEl, file){
Expand All @@ -26,6 +27,14 @@ export default class UploadEntry {
return isPreflighted && this.isActive(fileEl, file)
}

static isPreflightInProgress(file){
return file._preflightInProgress === true
}

static markPreflightInProgress(file){
file._preflightInProgress = true
}

constructor(fileEl, file, view){
this.ref = LiveUploader.genFileRef(file)
this.fileEl = fileEl
Expand Down

0 comments on commit 62792e1

Please sign in to comment.