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

pull/6480/head
fivem 3 years ago committed by GitHub
parent 000c7ca5ba
commit bca46b9f70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,10 +1,20 @@
<script> <script>
import { onDestroy } from 'svelte'; import Timer from './Timer.svelte';
let open = false;
let seconds = 0; let seconds = 0;
const toggle = () => (open = !open);
const handleTick = () => (seconds += 1);
</script> </script>
<p> <div>
The page has been open for <button on:click={toggle}>{open ? 'Close' : 'Open'} Timer</button>
<p>
The Timer component has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'} {seconds} {seconds === 1 ? 'second' : 'seconds'}
</p> </p>
{#if open}
<Timer callback={handleTick} />
{/if}
</div>

@ -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>

@ -1,5 +1,9 @@
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
export function onInterval(callback, milliseconds) { export function onInterval(callback, milliseconds) {
// implementation goes here const interval = setInterval(callback, milliseconds);
onDestroy(() => {
// Fix the memory leak here
});
} }

@ -1,11 +1,20 @@
<script> <script>
import { onInterval } from './utils.js'; import Timer from './Timer.svelte';
let open = true;
let seconds = 0; let seconds = 0;
onInterval(() => seconds += 1, 1000);
const toggle = () => (open = !open);
const handleTick = () => (seconds += 1);
</script> </script>
<p> <div>
The page has been open for <button on:click={toggle}>{open ? 'Close' : 'Open'} Timer</button>
<p>
The Timer component has been open for
{seconds} {seconds === 1 ? 'second' : 'seconds'} {seconds} {seconds === 1 ? 'second' : 'seconds'}
</p> </p>
{#if open}
<Timer callback={handleTick} />
{/if}
</div>

@ -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>

@ -10,8 +10,8 @@ For example, we can add a `setInterval` function when our component initialises,
<script> <script>
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
let seconds = 0; let counter = 0;
const interval = setInterval(() => seconds += 1, 1000); const interval = setInterval(() => counter += 1, 1000);
onDestroy(() => clearInterval(interval)); onDestroy(() => clearInterval(interval));
</script> </script>
@ -37,7 +37,9 @@ export function onInterval(callback, milliseconds) {
<script> <script>
import { onInterval } from './utils.js'; import { onInterval } from './utils.js';
let seconds = 0; let counter = 0;
onInterval(() => seconds += 1, 1000); onInterval(() => counter += 1, 1000);
</script> </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.
Loading…
Cancel
Save