From 41d6ecf2df409f5373ced50bf8f3ed7f6820bc05 Mon Sep 17 00:00:00 2001 From: gilgameshskytrooper Date: Mon, 31 May 2021 14:40:42 -0500 Subject: [PATCH] Reduce confusion of unsubcribe function by moving its location from 8a to 8b which is closer to when it get's used --- .../08-stores/01-writable-stores/app-a/App.svelte | 3 --- .../tutorial/08-stores/01-writable-stores/text.md | 2 -- .../08-stores/02-auto-subscriptions/app-a/App.svelte | 4 +--- .../tutorial/08-stores/02-auto-subscriptions/text.md | 11 +++++++++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/site/content/tutorial/08-stores/01-writable-stores/app-a/App.svelte b/site/content/tutorial/08-stores/01-writable-stores/app-a/App.svelte index ee542f789d..19cb505f5a 100644 --- a/site/content/tutorial/08-stores/01-writable-stores/app-a/App.svelte +++ b/site/content/tutorial/08-stores/01-writable-stores/app-a/App.svelte @@ -6,9 +6,6 @@ let count_value; - const unsubscribe = count.subscribe(value => { - count_value = value; - });

The count is {count_value}

diff --git a/site/content/tutorial/08-stores/01-writable-stores/text.md b/site/content/tutorial/08-stores/01-writable-stores/text.md index 546103c4c9..247ef86e8d 100644 --- a/site/content/tutorial/08-stores/01-writable-stores/text.md +++ b/site/content/tutorial/08-stores/01-writable-stores/text.md @@ -25,5 +25,3 @@ function reset() { count.set(0); } ``` - -> The function of assigning subscribe to `unsubscribe` is explained in the next section. diff --git a/site/content/tutorial/08-stores/02-auto-subscriptions/app-a/App.svelte b/site/content/tutorial/08-stores/02-auto-subscriptions/app-a/App.svelte index ee542f789d..88f30de818 100644 --- a/site/content/tutorial/08-stores/02-auto-subscriptions/app-a/App.svelte +++ b/site/content/tutorial/08-stores/02-auto-subscriptions/app-a/App.svelte @@ -6,9 +6,7 @@ let count_value; - const unsubscribe = count.subscribe(value => { - count_value = value; - }); + // declare unsubscribe here

The count is {count_value}

diff --git a/site/content/tutorial/08-stores/02-auto-subscriptions/text.md b/site/content/tutorial/08-stores/02-auto-subscriptions/text.md index af584261f4..6f13bce246 100644 --- a/site/content/tutorial/08-stores/02-auto-subscriptions/text.md +++ b/site/content/tutorial/08-stores/02-auto-subscriptions/text.md @@ -2,10 +2,17 @@ title: Auto-subscriptions --- -The app in the previous example works, but there's a subtle bug — the `unsubscribe` function never gets called. If the component was instantiated and destroyed many times, this would result in a *memory leak*. +The app in the previous example works, but there's a subtle bug — it is missing an `unsubscribe` function. If the component was instantiated and destroyed many times, this would result in a *memory leak*. -One way to fix it would be to use the `onDestroy` [lifecycle hook](tutorial/ondestroy): +Start by declaring `unsubscribe` in `App.svelte`: +```js + const unsubscribe = count.subscribe(value => { + count_value = value; + }); +``` + +Although you declared `unsubscribe`, it is never called. One way to fix it would be to use the `onDestroy` [lifecycle hook](tutorial/ondestroy): ```html