-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entries compounding/not clearing out when using LiveUpload from a hook #3610
Comments
I've checked most of the JS code to locate where the extra entries are sent without much luck. However I can inspect the WS connection and it looks to be in the The first validate event looks OK: [
"4",
"14",
"lv:phx-GBZDkJIZVqWmUwCG",
"event",
{
"type": "form",
"event": "validate",
"value": "_target=files",
"uploads": {
"phx-GBZDkKKTiWKJPwDG": [
{
"path": "files",
"ref": "0",
"last_modified": 1704894902000,
"name": "2",
"relative_path": "",
"type": "",
"size": 153600
}
]
}
}
] then the next validate event: [
"4",
"25",
"lv:phx-GBZDkJIZVqWmUwCG",
"event",
{
"type": "form",
"event": "validate",
"value": "_target=files",
"uploads": {
"phx-GBZDkKKTiWKJPwDG": [
{
"path": "files",
"ref": "0",
"last_modified": 1704894902000,
"name": "2",
"relative_path": "",
"type": "",
"size": 153600
},
{
"path": "files",
"ref": "1",
"last_modified": 1704894902000,
"name": "3",
"relative_path": "",
"type": "",
"size": 153600
}
]
}
}
]
still carries the previous file The third event shifts out the first file, but now carries the second and third file: [
"4",
"36",
"lv:phx-GBZDkJIZVqWmUwCG",
"event",
{
"type": "form",
"event": "validate_catalog",
"value": "_target=transformer",
"uploads": {
"phx-GBZDkKKTiWKJPwDG": [
{
"path": "transformer",
"ref": "1",
"last_modified": 1704894902000,
"name": "3",
"relative_path": "",
"type": "",
"size": 153600
},
{
"path": "transformer",
"ref": "2",
"last_modified": 1704894902000,
"name": "4",
"relative_path": "",
"type": "",
"size": 153600
}
]
}
}
] etc |
So: If I add a this.handleEvent('upload_send_next_file', () => {
console.log('Uploading more files! Remainder:', filesRemaining)
// waiting a tick here to allow LV to reset the files(?)
setTimeout(() => {
if (filesRemaining.length > 0) {
console.log('got MORE!')
const nextFile = filesRemaining.shift()
if (nextFile != undefined) {
console.log('Uploading file: ', nextFile.name)
this.upload('files', [nextFile])
}
} else {
console.log('Done uploading, noop!')
}
}, 0)
}) Maybe this is to be expected? |
Sorry for the noise / 🦆 —— last note: It also works with |
@tmjoen I didn't look into this in detail yet, but it looks very similar to https://github.com/phoenixframework/phoenix_live_view/blob/main/test/e2e/support/issues/issue_2965.ex; do you know what's the difference? |
@SteffenDE Thanks for looking! In the issue_2965 file it initially waits for the "upload_scrub_list" which will delay the time between the input event and this.upload in the same way as requestAnimationFrame I guess? Also maybe it's different with the noop writer? |
@tmjoen this is because currently events are dispatched before the push is acknowledged to the caller. We can fix this in LV by dispatching events in the next microtick. diff --git a/assets/js/phoenix_live_view/view.js b/assets/js/phoenix_live_view/view.js
index 6b9fe4ce..c01986aa 100644
--- a/assets/js/phoenix_live_view/view.js
+++ b/assets/js/phoenix_live_view/view.js
@@ -669,7 +669,9 @@ export default class View {
})
}
- this.liveSocket.dispatchEvents(events)
+ // dispatch events in the next microtick to allow file progress to be tracked
+ // before hook event handlers are invoked (#3610)
+ window.requestAnimationFrame(() => { this.liveSocket.dispatchEvents(events) })
if(phxChildrenAdded){ this.joinNewChildren() }
} @chrismccord the pushFileProgress callback is only called when the pushWithReply promise resolves, but the (hook) events are dispatched earlier: phoenix_live_view/assets/js/phoenix_live_view/upload_entry.js Lines 61 to 64 in 38be0a1
Let's see what Chris thinks :) |
Environment
Actual behavior
I'm building a queued uploader, where I'll only upload one file at a time. I stumbled into some weirdness, though. If I queue up 20 files, the first file gets consumed and removed from entries, but is readded again on the next upload. I first checked
dispatchUploads
on the LV JS side, and that seems correct — it only sends one file at a time. I then checkedmaybe_update_uploads
in Phoenix.LiveView.Channel and here it seems to compound.The first upload:
The second upload:
etc.
This means that the entries remaining in the uploaders config. Maybe they're not cleared out somewhere when using
this.upload
from a hook? I couldn't find out more, unfortunately..Single file app here:
Expected behavior
Entries are cleared out when consumed and there isn't any compounding of upload entries when uploading through a hook.
The text was updated successfully, but these errors were encountered: