Skip to content

Commit d2478bf

Browse files
author
Rich Harris
committed
improve code somewhat
1 parent f0bcb33 commit d2478bf

File tree

3 files changed

+49
-65
lines changed

3 files changed

+49
-65
lines changed

content/tutorial/01-svelte/07-lifecycle/03-update/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ This [Eliza](https://en.wikipedia.org/wiki/ELIZA) chatbot is annoying to use, be
1111
```js
1212
/// file: App.svelte
1313
let div;
14-
let autoscroll;
14+
+++let autoscroll = false;+++
1515

1616
beforeUpdate(() => {
17-
autoscroll = div && div.offsetHeight + div.scrollTop > div.scrollHeight - 20;
17+
+++autoscroll = div && div.offsetHeight + div.scrollTop > div.scrollHeight - 20;+++
1818
});
1919

2020
afterUpdate(() => {
21-
if (autoscroll) div.scrollTo(0, div.scrollHeight);
21+
+++ if (autoscroll) {
22+
div.scrollTo(0, div.scrollHeight);
23+
}+++
2224
});
2325
```
2426

content/tutorial/01-svelte/07-lifecycle/03-update/app-a/src/lib/App.svelte

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,35 @@
1717
});
1818
1919
const eliza = new Eliza();
20+
const pause = (ms) => new Promise((fulfil) => setTimeout(fulfil, ms));
21+
22+
const typing = { author: 'eliza', text: '...' };
2023
2124
let comments = [
2225
{ author: 'eliza', text: eliza.getInitial() }
2326
];
2427
25-
function handleKeydown(event) {
26-
if (event.key === 'Enter') {
27-
const text = event.target.value;
28-
if (!text) return;
28+
async function handleKeydown(event) {
29+
if (event.key === 'Enter' && event.target.value) {
30+
event.target.value = '';
2931
30-
comments = comments.concat({
32+
const comment = {
3133
author: 'user',
32-
text
33-
});
34+
text: event.target.value
35+
};
3436
35-
event.target.value = '';
37+
const reply = {
38+
author: 'eliza',
39+
text: eliza.transform(comment.text)
40+
};
41+
42+
comments = [...comments, comment];
43+
44+
await pause(200 * (1 + Math.random()));
45+
comments = [...comments, typing];
3646
37-
const reply = eliza.transform(text);
38-
39-
setTimeout(() => {
40-
comments = comments.concat({
41-
author: 'eliza',
42-
text: '...',
43-
placeholder: true
44-
});
45-
46-
setTimeout(() => {
47-
comments = comments
48-
.filter(
49-
(comment) => !comment.placeholder
50-
)
51-
.concat({
52-
author: 'eliza',
53-
text: reply
54-
});
55-
}, 500 + Math.random() * 500);
56-
}, 200 + Math.random() * 200);
47+
await pause(500 * (1 + Math.random()));
48+
comments = [...comments, reply].filter(comment => comment !== typing);
5749
}
5850
}
5951
</script>

content/tutorial/01-svelte/07-lifecycle/03-update/app-b/src/lib/App.svelte

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,48 @@
66
} from 'svelte';
77
88
let div;
9-
let autoscroll;
9+
let autoscroll = false;
1010
1111
beforeUpdate(() => {
12-
autoscroll =
13-
div &&
14-
div.offsetHeight + div.scrollTop >
15-
div.scrollHeight - 20;
12+
autoscroll = div && div.offsetHeight + div.scrollTop > div.scrollHeight - 20;
1613
});
1714
1815
afterUpdate(() => {
19-
if (autoscroll)
16+
if (autoscroll) {
2017
div.scrollTo(0, div.scrollHeight);
18+
}
2119
});
2220
2321
const eliza = new Eliza();
22+
const pause = (ms) => new Promise((fulfil) => setTimeout(fulfil, ms));
23+
24+
const typing = { author: 'eliza', text: '...' };
2425
2526
let comments = [
2627
{ author: 'eliza', text: eliza.getInitial() }
2728
];
2829
29-
function handleKeydown(event) {
30-
if (event.key === 'Enter') {
31-
const text = event.target.value;
32-
if (!text) return;
30+
async function handleKeydown(event) {
31+
if (event.key === 'Enter' && event.target.value) {
32+
event.target.value = '';
3333
34-
comments = comments.concat({
34+
const comment = {
3535
author: 'user',
36-
text
37-
});
36+
text: event.target.value
37+
};
3838
39-
event.target.value = '';
39+
const reply = {
40+
author: 'eliza',
41+
text: eliza.transform(comment.text)
42+
};
43+
44+
comments = [...comments, comment];
45+
46+
await pause(200 * (1 + Math.random()));
47+
comments = [...comments, typing];
4048
41-
const reply = eliza.transform(text);
42-
43-
setTimeout(() => {
44-
comments = comments.concat({
45-
author: 'eliza',
46-
text: '...',
47-
placeholder: true
48-
});
49-
50-
setTimeout(() => {
51-
comments = comments
52-
.filter(
53-
(comment) => !comment.placeholder
54-
)
55-
.concat({
56-
author: 'eliza',
57-
text: reply
58-
});
59-
}, 500 + Math.random() * 500);
60-
}, 200 + Math.random() * 200);
49+
await pause(500 * (1 + Math.random()));
50+
comments = [...comments, reply].filter(comment => comment !== typing);
6151
}
6252
}
6353
</script>

0 commit comments

Comments
 (0)