Skip to content

tutorial: In the onDestroy example, explicitly show the memory leak #5515

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

Merged
merged 8 commits into from
Jun 27, 2021
20 changes: 15 additions & 5 deletions site/content/tutorial/07-lifecycle/02-ondestroy/app-a/App.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<script>
import { onDestroy } from 'svelte';
import Timer from './Timer.svelte';

let open = false;
let seconds = 0;

const toggle = () => (open = !open);
const handleTick = () => (seconds += 1);
</script>

<p>
The page has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'}
</p>
<div>
<button on:click={toggle}>{open ? 'Close' : 'Open'} Timer</button>
<p>
The Timer component has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'}
</p>
{#if open}
<Timer callback={handleTick} />
{/if}
</div>
20 changes: 20 additions & 0 deletions site/content/tutorial/07-lifecycle/02-ondestroy/app-a/Timer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
import { onInterval } from './utils.js';

export let callback;
export let interval = 1000;

onInterval(callback, interval);
</script>

<p>
This component executes a callback every
{interval} millisecond{interval === 1 ? '' : 's'}
</p>

<style>
p {
border: 1px solid blue;
padding: 5px;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { onDestroy } from 'svelte';

export function onInterval(callback, milliseconds) {
// implementation goes here
const interval = setInterval(callback, milliseconds);

onDestroy(() => {
// Fix the memory leak here
});
}
21 changes: 15 additions & 6 deletions site/content/tutorial/07-lifecycle/02-ondestroy/app-b/App.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<script>
import { onInterval } from './utils.js';
import Timer from './Timer.svelte';

let open = true;
let seconds = 0;
onInterval(() => seconds += 1, 1000);

const toggle = () => (open = !open);
const handleTick = () => (seconds += 1);
</script>

<p>
The page has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'}
</p>
<div>
<button on:click={toggle}>{open ? 'Close' : 'Open'} Timer</button>
<p>
The Timer component has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'}
</p>
{#if open}
<Timer callback={handleTick} />
{/if}
</div>
20 changes: 20 additions & 0 deletions site/content/tutorial/07-lifecycle/02-ondestroy/app-b/Timer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
import { onInterval } from './utils.js';

export let callback;
export let interval = 1000;

onInterval(callback, interval);
</script>

<p>
This component executes a callback every
{interval} millisecond{interval === 1 ? '' : 's'}
</p>

<style>
p {
border: 1px solid blue;
padding: 5px;
}
</style>
12 changes: 7 additions & 5 deletions site/content/tutorial/07-lifecycle/02-ondestroy/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ For example, we can add a `setInterval` function when our component initialises,
<script>
import { onDestroy } from 'svelte';

let seconds = 0;
const interval = setInterval(() => seconds += 1, 1000);
let counter = 0;
const interval = setInterval(() => counter += 1, 1000);

onDestroy(() => clearInterval(interval));
</script>
Expand All @@ -37,7 +37,9 @@ export function onInterval(callback, milliseconds) {
<script>
import { onInterval } from './utils.js';

let seconds = 0;
onInterval(() => seconds += 1, 1000);
let counter = 0;
onInterval(() => counter += 1, 1000);
</script>
```
```

Open and close the timer a few times and make sure the counter keeps ticking and the CPU load increases. This is due to a memory leak as the previous timers are not deleted. Don't forget to refresh the page before solving the example.