From e7eac0386482387605b83da1ddc99826a98386b2 Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sat, 23 Sep 2023 13:43:00 +0100 Subject: [PATCH 1/5] added a note about implementing suspending hooks --- website/docs/concepts/suspense.mdx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/website/docs/concepts/suspense.mdx b/website/docs/concepts/suspense.mdx index 6c697d82f00..f4a2d135746 100644 --- a/website/docs/concepts/suspense.mdx +++ b/website/docs/concepts/suspense.mdx @@ -78,6 +78,17 @@ fn use_user() -> SuspensionResult { } ``` +#### Note on implementing suspending hooks +[`Suspension::new`](https://docs.rs/yew/latest/yew/suspense/struct.Suspension.html#method.new) returns 2 values: the suspension context itself, and a suspension handle. +The latter is the one responsible for signaling when to re-render the suspended components, it provides 2 interchangable ways to do so: +1. Calling its [`resume`](https://docs.rs/yew/latest/yew/suspense/struct.SuspensionHandle.html#method.resume) method. +2. Dropping the handle. +If the handle isn't stored for longer than the hook's scope, the suspended components will be constantly re-rendered as often as possible, thus hampering performance. +If the handle is forgotten, i.e. with [`forget`](https://doc.rust-lang.org/std/mem/fn.forget.html) or [`ManuallyDrop`](https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html#) won't allow the suspended components to be re-rendered ever again. +Thus, to effectively use suspension, the handle needs to be stored until it's time to update components, i.e. with newly received data. +In the example above, the suspension handle is preserved by being moved into a closure and passed into `on_load_user_complete`. +When the hypothetical user will be loaded, the closure will be called, thus calling `handle.resume()` and re-rendering the components associated with the suspension context. + # Complete Example ```rust From 1c670dadc20f6ce0bfed2fd83125ed3ead9e286d Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sat, 23 Sep 2023 14:10:54 +0100 Subject: [PATCH 2/5] fixed formatting --- website/docs/concepts/suspense.mdx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/docs/concepts/suspense.mdx b/website/docs/concepts/suspense.mdx index f4a2d135746..72d48d8dddd 100644 --- a/website/docs/concepts/suspense.mdx +++ b/website/docs/concepts/suspense.mdx @@ -79,10 +79,13 @@ fn use_user() -> SuspensionResult { ``` #### Note on implementing suspending hooks + [`Suspension::new`](https://docs.rs/yew/latest/yew/suspense/struct.Suspension.html#method.new) returns 2 values: the suspension context itself, and a suspension handle. The latter is the one responsible for signaling when to re-render the suspended components, it provides 2 interchangable ways to do so: + 1. Calling its [`resume`](https://docs.rs/yew/latest/yew/suspense/struct.SuspensionHandle.html#method.resume) method. 2. Dropping the handle. + If the handle isn't stored for longer than the hook's scope, the suspended components will be constantly re-rendered as often as possible, thus hampering performance. If the handle is forgotten, i.e. with [`forget`](https://doc.rust-lang.org/std/mem/fn.forget.html) or [`ManuallyDrop`](https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html#) won't allow the suspended components to be re-rendered ever again. Thus, to effectively use suspension, the handle needs to be stored until it's time to update components, i.e. with newly received data. From a409afab90f4bd8f91df30a1af78a961d0389936 Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sat, 23 Sep 2023 14:20:23 +0100 Subject: [PATCH 3/5] extracted part of the note into a danger block --- website/docs/concepts/suspense.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/docs/concepts/suspense.mdx b/website/docs/concepts/suspense.mdx index 72d48d8dddd..fcf78a77905 100644 --- a/website/docs/concepts/suspense.mdx +++ b/website/docs/concepts/suspense.mdx @@ -86,9 +86,9 @@ The latter is the one responsible for signaling when to re-render the suspended 1. Calling its [`resume`](https://docs.rs/yew/latest/yew/suspense/struct.SuspensionHandle.html#method.resume) method. 2. Dropping the handle. -If the handle isn't stored for longer than the hook's scope, the suspended components will be constantly re-rendered as often as possible, thus hampering performance. -If the handle is forgotten, i.e. with [`forget`](https://doc.rust-lang.org/std/mem/fn.forget.html) or [`ManuallyDrop`](https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html#) won't allow the suspended components to be re-rendered ever again. -Thus, to effectively use suspension, the handle needs to be stored until it's time to update components, i.e. with newly received data. +:::danger +The suspension handle must be stored until it's time to update components, i.e. with newly received data; +otherwise, the suspended components will enter an infinite re-render loop, thus hampering performance. In the example above, the suspension handle is preserved by being moved into a closure and passed into `on_load_user_complete`. When the hypothetical user will be loaded, the closure will be called, thus calling `handle.resume()` and re-rendering the components associated with the suspension context. From 484c2fb952641180a8437fbb561a1c6779d37285 Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sat, 23 Sep 2023 14:26:16 +0100 Subject: [PATCH 4/5] added ::: --- website/docs/concepts/suspense.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/concepts/suspense.mdx b/website/docs/concepts/suspense.mdx index fcf78a77905..d8a367cce46 100644 --- a/website/docs/concepts/suspense.mdx +++ b/website/docs/concepts/suspense.mdx @@ -91,6 +91,7 @@ The suspension handle must be stored until it's time to update components, i.e. otherwise, the suspended components will enter an infinite re-render loop, thus hampering performance. In the example above, the suspension handle is preserved by being moved into a closure and passed into `on_load_user_complete`. When the hypothetical user will be loaded, the closure will be called, thus calling `handle.resume()` and re-rendering the components associated with the suspension context. +::: # Complete Example From 0de5e3a4521baa745e9ce5236e6f04d304b5d9ea Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sat, 23 Sep 2023 14:29:28 +0100 Subject: [PATCH 5/5] added spaces --- website/docs/concepts/suspense.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/concepts/suspense.mdx b/website/docs/concepts/suspense.mdx index d8a367cce46..efd8d1e566c 100644 --- a/website/docs/concepts/suspense.mdx +++ b/website/docs/concepts/suspense.mdx @@ -87,10 +87,12 @@ The latter is the one responsible for signaling when to re-render the suspended 2. Dropping the handle. :::danger + The suspension handle must be stored until it's time to update components, i.e. with newly received data; otherwise, the suspended components will enter an infinite re-render loop, thus hampering performance. In the example above, the suspension handle is preserved by being moved into a closure and passed into `on_load_user_complete`. When the hypothetical user will be loaded, the closure will be called, thus calling `handle.resume()` and re-rendering the components associated with the suspension context. + ::: # Complete Example