Skip to content
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

fiexed the bug when future restarts the state goes to ready instead of pending #3617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/hooks/src/use_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ where
(rc, Rc::new(Cell::new(Some(changed))))
});

let cb = use_callback(move |_| {
llet cb = use_callback(move |_| {
// Set the state to Pending when the task is restarted
state.set(UseResourceState::Pending);

// Create the user's task
let fut = rc.reset_and_run_in(&mut future);

// Spawn a wrapper task that polls the inner future and watch its dependencies
// Spawn a wrapper task that polls the inner future and watches its dependencies
spawn(async move {
// move the future here and pin it so we can poll it
// Move the future here and pin it so we can poll it
let fut = fut;
pin_mut!(fut);

// Run each poll in the context of the reactive scope
// This ensures the scope is properly subscribed to the future's dependencies
let res = future::poll_fn(|cx| {
Expand All @@ -47,12 +50,13 @@ where
})
})
.await;

// Set the value and state
state.set(UseResourceState::Ready);
value.set(Some(res));
})
});
});


let mut task = use_hook(|| Signal::new(cb(())));

Expand Down
Loading