Skip to content

Commit 6007520

Browse files
committed
Anything that change should be a let not a const
1 parent 48024c7 commit 6007520

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

lib/components_guide_web/templates/web_standards/idempotent-javascript-operations.html.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ As a bonus, we can add change tracking which would allow us to detect whether th
6666

6767
```js
6868
const myList = new Set();
69-
const myListChangeCount = 0;
69+
let myListChangeCount = 0;
7070

7171
function add(showID) {
7272
const before = myList.size;
@@ -154,8 +154,8 @@ myList.remove(123);
154154
Before:
155155

156156
```js
157-
const searchResults = null;
158-
const searchError = null;
157+
let searchResults = null;
158+
let searchError = null;
159159

160160
async function performSearch(searchQuery) {
161161
const params = new URLSearchParams({ q: searchQuery });
@@ -179,11 +179,11 @@ performSearch('russian doll');
179179
After:
180180

181181
```js
182-
const currentSearchQuery = null;
183-
const searchQueryChangeCount = 0;
184-
const searchResultsChangeCount = 0;
185-
const searchResults = null;
186-
const searchError = null;
182+
let currentSearchQuery = null;
183+
let searchQueryChangeCount = 0;
184+
let searchResultsChangeCount = 0;
185+
let searchResults = null;
186+
let searchError = null;
187187

188188
function performSearch(searchQuery) {
189189
if (currentSearchQuery === searchQuery) {
@@ -231,10 +231,10 @@ Even better with `AbortSignal`:
231231

232232
```js
233233
let aborter = new AbortController();
234-
const currentSearchQuery = null;
235-
const searchResultsChangeCount = 0;
236-
const searchResults = null;
237-
const searchError = null;
234+
let currentSearchQuery = null;
235+
let searchResultsChangeCount = 0;
236+
let searchResults = null;
237+
let searchError = null;
238238

239239
function performSearch(searchQuery) {
240240
if (currentSearchQuery === searchQuery) {
@@ -320,8 +320,8 @@ class ValueTicker extends Ticker {
320320

321321
```js
322322
const ticker = new ValueTicker('');
323-
const searchResults = null;
324-
const searchError = null;
323+
let searchResults = null;
324+
let searchError = null;
325325

326326
function performSearch(searchQuery) {
327327
if (!ticker.next(searchQuery)) {
@@ -365,16 +365,24 @@ Draft: coming soon perhaps?
365365
## Switching profiles
366366

367367
```js
368-
const currentProfileID = null;
369-
const currentProfileChangeCount = 0;
368+
let currentProfileID = null;
369+
let currentProfileChangeCount = 0;
370370

371371
function changeProfile(profileID) {
372+
if (currentProfileID === profileID) {
373+
return;
374+
}
375+
372376
currentProfileID = profileID;
377+
currentProfileChangeCount++;
373378
}
374379

375380
// currentProfileID: null
381+
// currentProfileChangeCount: 0
376382
changeProfile(123);
377383
// currentProfileID: 123
384+
// currentProfileChangeCount: 1
378385
changeProfile(123);
379386
// currentProfileID: 123
387+
// currentProfileChangeCount: 1
380388
```

0 commit comments

Comments
 (0)