Skip to content

Commit

Permalink
Add create_isomorphic_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Oct 23, 2024
1 parent 9b95b49 commit c54db72
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions packages/sycamore-web/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,28 @@ impl<T: 'static> Resource<T> {
}
}

/// Attach handlers to call the refetch function on the client side.
fn fetch_on_client(self) -> Self {
if is_not_ssr!() {
create_effect(move || {
self.is_loading.set(true);
// Take all the scopes and create a new guard.
for scope in self.scopes.take() {
let guard = SuspenseTaskGuard::from_scope(scope);
self.guards.update(|guards| guards.push(guard));
}
/// Attach handlers to always call the refetch function to get the latest value.
fn always_refetch(self) -> Self {
create_effect(move || {
self.is_loading.set(true);

Check warning on line 49 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L47-L49

Added lines #L47 - L49 were not covered by tests
// Take all the scopes and create a new guard.
for scope in self.scopes.take() {
let guard = SuspenseTaskGuard::from_scope(scope);
self.guards.update(|guards| guards.push(guard));
}

Check warning on line 54 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L51-L54

Added lines #L51 - L54 were not covered by tests

let fut = self.refetch.update_silent(|f| f());
let fut = self.refetch.update_silent(|f| f());

Check warning on line 56 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L56

Added line #L56 was not covered by tests

sycamore_futures::create_suspense_task(async move {
let value = fut.await;
batch(move || {
self.value.set(Some(value));
self.is_loading.set(false);
// Now, drop all the guards to resolve suspense.
self.guards.update(|guards| guards.clear());
});
sycamore_futures::create_suspense_task(async move {
let value = fut.await;
batch(move || {
self.value.set(Some(value));
self.is_loading.set(false);
// Now, drop all the guards to resolve suspense.
self.guards.update(|guards| guards.clear());

Check warning on line 64 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L58-L64

Added lines #L58 - L64 were not covered by tests
});
})
}
});
});

Check warning on line 67 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L66-L67

Added lines #L66 - L67 were not covered by tests

self
}
Expand Down Expand Up @@ -95,18 +93,37 @@ impl<T: 'static> Deref for Resource<T> {
}
}

/// Create a resrouce that will only be resolved on the client side.
/// Create a resource value that is fetched on both client and server.
///
/// If the resource has any dependencies, it is recommended to use [`on`] to make them explicit.
/// This will ensure that the dependencies are tracked since reactive variables inside async
/// contexts are not tracked automatically.
pub fn create_isomorphic_resource<F, Fut, T>(f: F) -> Resource<T>
where
F: FnMut() -> Fut + 'static,
Fut: Future<Output = T> + 'static,
T: 'static,
{
Resource::new(f).always_refetch()
}

Check warning on line 108 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L101-L108

Added lines #L101 - L108 were not covered by tests

/// Create a resource value that is fetched only on the client.
///
/// On the server, the resource will forever be in the loading state.
///
/// On the server, the resource will always be marked as loading.
/// If the resource has any dependencies, it is recommended to use [`on`] to make them explicit.
/// This will ensure that the dependencies are tracked since reactive variables inside async
/// contexts are not tracked automatically.
pub fn create_client_resource<F, Fut, T>(f: F) -> Resource<T>
where
F: FnMut() -> Fut + 'static,
Fut: Future<Output = T> + 'static,
T: 'static,
{
Resource::new(f).fetch_on_client()
let resource = Resource::new(f);
if is_not_ssr!() {
resource.always_refetch()

Check warning on line 125 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L123-L125

Added lines #L123 - L125 were not covered by tests
} else {
resource

Check warning on line 127 in packages/sycamore-web/src/resource.rs

View check run for this annotation

Codecov / codecov/patch

packages/sycamore-web/src/resource.rs#L127

Added line #L127 was not covered by tests
}
}

0 comments on commit c54db72

Please sign in to comment.