Skip to content

Commit

Permalink
Fix upload cancellation by using refs instead of upload_controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
masenf committed Dec 12, 2024
1 parent c76797f commit aedfca3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 5 additions & 6 deletions reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ let event_processing = false;
// Array holding pending events to be processed.
const event_queue = [];

// Pending upload promises, by id
const upload_controllers = {};

/**
* Generate a UUID (Used for session tokens).
* Taken from: https://stackoverflow.com/questions/105034/how-do-i-create-a-guid-uuid
Expand Down Expand Up @@ -485,7 +482,9 @@ export const uploadFiles = async (
return false;
}

if (upload_controllers[upload_id]) {
const upload_ref_name = `__upload_controllers_${upload_id}`

if (refs[upload_ref_name]) {
console.log("Upload already in progress for ", upload_id);
return false;
}
Expand Down Expand Up @@ -537,7 +536,7 @@ export const uploadFiles = async (
});

// Send the file to the server.
upload_controllers[upload_id] = controller;
refs[upload_ref_name] = controller;

try {
return await axios.post(getBackendURL(UPLOADURL), formdata, config);
Expand All @@ -557,7 +556,7 @@ export const uploadFiles = async (
}
return false;
} finally {
delete upload_controllers[upload_id];
delete refs[upload_ref_name];
}
};

Expand Down
8 changes: 5 additions & 3 deletions reflex/components/core/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from reflex.utils import format
from reflex.utils.imports import ImportVar
from reflex.vars import VarData
from reflex.vars.base import CallableVar, LiteralVar, Var, get_unique_variable_name
from reflex.vars.base import CallableVar, Var, get_unique_variable_name
from reflex.vars.sequence import LiteralStringVar

DEFAULT_UPLOAD_ID: str = "default"
Expand Down Expand Up @@ -108,7 +108,8 @@ def clear_selected_files(id_: str = DEFAULT_UPLOAD_ID) -> EventSpec:
# UploadFilesProvider assigns a special function to clear selected files
# into the shared global refs object to make it accessible outside a React
# component via `run_script` (otherwise backend could never clear files).
return run_script(f"refs['__clear_selected_files']({id_!r})")
func = Var("__clear_selected_files")._as_ref()
return run_script(f"{func}({id_!r})")


def cancel_upload(upload_id: str) -> EventSpec:
Expand All @@ -120,7 +121,8 @@ def cancel_upload(upload_id: str) -> EventSpec:
Returns:
An event spec that cancels the upload when triggered.
"""
return run_script(f"upload_controllers[{LiteralVar.create(upload_id)!s}]?.abort()")
controller = Var(f"__upload_controllers_{upload_id}")._as_ref()
return run_script(f"{controller}?.abort()")


def get_upload_dir() -> Path:
Expand Down

0 comments on commit aedfca3

Please sign in to comment.