You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/components_guide_web/templates/robust_javascript_interactivity/idempotent-javascript-operations.html.md
+59-31Lines changed: 59 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,49 +7,77 @@ Here I’m going to use the example of an online video service like Netflix, whe
7
7
8
8
## Adding and removing a show from a user’s list
9
9
10
-
Before, using an Array:
10
+
Apps like Netflix allow you to add a show to a list to watch later. If you’re like me, you rarely get around to actually watching them.
11
+
12
+
To implement this, we could use an array that holds all the show IDs like so:
11
13
12
14
```js
13
-
constmyList= [];
15
+
constwatchLater= [];
14
16
15
17
functionadd(showID) {
16
-
myList.push(showID);
18
+
watchLater.push(showID);
19
+
console.log(watchLater);
17
20
}
18
21
19
22
add(123);
23
+
// [123]
24
+
```
25
+
26
+
However, there’s an issue when we add the same item twice:
27
+
28
+
```js
20
29
add(123);
21
-
myList;
22
-
//Appears twice, not idempotent :(
30
+
add(123);
31
+
//The same show ID appears twice :(
23
32
// [123, 123]
24
33
```
25
34
26
-
After, using a Set:
35
+
We could fix this by adding logic to detect whether the item is already in the array, and skip adding it if so:
36
+
37
+
```js
38
+
functionadd(showID) {
39
+
if (watchLater.includes(showID)) {
40
+
return;
41
+
}
42
+
43
+
watchLater.push(showID);
44
+
console.log(watchLater);
45
+
}
46
+
```
47
+
48
+
But wouldn’t it be great if we had a simpler solution? It’s annoying to have to think of these edge cases and code around them.
49
+
50
+
If we change our data structure to one that enforces uniqueness from the beginning, then our double entry problem is solved.
51
+
52
+
In JavaScript, a `Set` is a data structure that is ordered just like an array, but it enforces uniqueness:
27
53
28
54
```js
29
-
constmyList=newSet();
55
+
constwatchLater=newSet();
30
56
31
57
functionadd(showID) {
32
-
myList.add(showID);
58
+
watchLater.add(showID);
59
+
console.log(watchLater);
33
60
}
34
61
35
62
add(123);
36
63
add(123);
37
-
myList;
38
-
// Appears once, is idempotent :)
64
+
// Appears only once :)
39
65
// Set { 123 }
40
66
```
41
67
42
68
We can extend this for also removing an item from the list:
43
69
44
70
```js
45
-
constmyList=newSet();
71
+
constwatchLater=newSet();
46
72
47
73
functionadd(showID) {
48
-
myList.add(showID);
74
+
watchLater.add(showID);
75
+
console.log(watchLater);
49
76
}
50
77
51
78
functionremove(showID) {
52
-
myList.delete(showID);
79
+
watchLater.delete(showID);
80
+
console.log(watchLater);
53
81
}
54
82
55
83
add(123);
@@ -62,43 +90,43 @@ remove(123);
62
90
// Set {}
63
91
```
64
92
65
-
As a bonus, we can add change tracking which would allow us to detect whether the data has actually changed, and if a consumer say needed to update or re-render.
93
+
As a bonus, we can add change tracking which would allow us to detect whether the data has actually changed, and and if not, then avoid say a re-render.
0 commit comments