Skip to content

Commit ed12fd9

Browse files
committed
Use 'syncState' rather than 'setState' in components
1 parent 627c7ec commit ed12fd9

8 files changed

Lines changed: 49 additions & 49 deletions

16_game.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ defined earlier.
640640

641641
{{index graphics, optimization, efficiency, state}}
642642

643-
The `setState` method is used to make the display show a given state.
643+
The `syncState` method is used to make the display show a given state.
644644
It first removes the old actor graphics, if any, and then redraws the
645645
actors in their new positions. It may be tempting to try to reuse the
646646
((DOM)) elements for actors, but to make that work, we would need a
@@ -650,7 +650,7 @@ there will typically be only a handful of actors in the game,
650650
redrawing all of them is not expensive.
651651

652652
```{includeCode: true}
653-
DOMDisplay.prototype.setState = function(state) {
653+
DOMDisplay.prototype.syncState = function(state) {
654654
if (this.actorLayer) this.actorLayer.remove();
655655
this.actorLayer = drawActors(state.actors);
656656
this.dom.appendChild(this.actorLayer);
@@ -772,7 +772,7 @@ We are now able to display our tiny level.
772772
<script>
773773
let simpleLevel = new Level(simpleLevelPlan);
774774
let display = new DOMDisplay(document.body, simpleLevel);
775-
display.setState(State.start(simpleLevel));
775+
display.syncState(State.start(simpleLevel));
776776
</script>
777777
```
778778

@@ -1175,7 +1175,7 @@ function runLevel(level, Display) {
11751175
return new Promise(resolve => {
11761176
runAnimation(time => {
11771177
state = state.update(time, arrowKeys);
1178-
display.setState(state);
1178+
display.syncState(state);
11791179
if (state.status == "playing") {
11801180
return true;
11811181
} else if (ending > 0) {
@@ -1323,7 +1323,7 @@ it is finished.
13231323
return new Promise(resolve => {
13241324
runAnimation(time => {
13251325
state = state.update(time, arrowKeys);
1326-
display.setState(state);
1326+
display.syncState(state);
13271327
if (state.status == "playing") {
13281328
return true;
13291329
} else if (ending > 0) {

17_canvas.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ that represent the game's elements.
863863

864864
We define another display object type called `CanvasDisplay`,
865865
supporting the same ((interface)) as `DOMDisplay` from [Chapter
866-
?](game#domdisplay), namely the methods `setState` and `clear`.
866+
?](game#domdisplay), namely the methods `syncState` and `clear`.
867867

868868
{{index state}}
869869

@@ -899,11 +899,11 @@ class CanvasDisplay {
899899
}
900900
```
901901

902-
The `setState` method first computes a new viewport and then draws
902+
The `syncState` method first computes a new viewport and then draws
903903
the game scene at the appropriate position.
904904

905905
```{sandbox: "game", includeCode: true}
906-
CanvasDisplay.prototype.setState = function(state) {
906+
CanvasDisplay.prototype.syncState = function(state) {
907907
this.updateViewport(state);
908908
this.clearDisplay(state.status);
909909
this.drawBackground(state.level);

19_paint.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ the component. Most constructors will also take some other values,
115115
that won't change over time, such as the function they can use to
116116
((dispatch)) an action.
117117

118-
{{index "setState method"}}
118+
{{index "syncState method"}}
119119

120-
Each component has a `setState` method that is used to synchronize it
120+
Each component has a `syncState` method that is used to synchronize it
121121
to a new state value. The method takes one argument, the state, which
122122
is of the same type as the first argument to its constructor.
123123

@@ -287,21 +287,21 @@ class PictureCanvas {
287287
onmousedown: event => this.mouse(event, pointerDown),
288288
ontouchstart: event => this.touch(event, pointerDown)
289289
});
290-
this.setState(picture);
290+
this.syncState(picture);
291291
}
292-
setState(picture) {
292+
syncState(picture) {
293293
if (this.picture == picture) return;
294294
this.picture = picture;
295295
drawPicture(this.picture, this.dom, scale);
296296
}
297297
}
298298
```
299299

300-
{{index "setState method", efficiency}}
300+
{{index "syncState method", efficiency}}
301301

302302
We draw each pixel as a 10-by-10 square, as determined by the `scale`
303303
constant. To avoid unnecessary work, the component keeps track of its
304-
current picture, and does a redraw only when `setState` is given a new
304+
current picture, and does a redraw only when `syncState` is given a new
305305
picture.
306306

307307
{{index "drawPicture function"}}
@@ -445,10 +445,10 @@ class PixelEditor {
445445
...this.controls.reduce(
446446
(a, c) => a.concat(" ", c.dom), []));
447447
}
448-
setState(state) {
448+
syncState(state) {
449449
this.state = state;
450-
this.canvas.setState(state.picture);
451-
for (let ctrl of this.controls) ctrl.setState(state);
450+
this.canvas.syncState(state.picture);
451+
for (let ctrl of this.controls) ctrl.syncState(state);
452452
}
453453
}
454454
```
@@ -457,14 +457,14 @@ The pointer handler given to `PictureCanvas` calls the currently
457457
selected tool with the appropriate arguments and, if that returns a
458458
move handler, adapts it to also receive the state.
459459

460-
{{index "reduce method", "map method", whitespace, "setState method"}}
460+
{{index "reduce method", "map method", whitespace, "syncState method"}}
461461

462462
All controls are constructed and stored in `this.controls` so that
463463
they can be updated when the application state changes. The call to
464464
`reduce` introduces spaces between the controls' DOM elements. That
465465
way they don't look so pressed together.
466466

467-
{{index "select (HTML tag)", "change event", "ToolSelect class", "setState method"}}
467+
{{index "select (HTML tag)", "change event", "ToolSelect class", "syncState method"}}
468468

469469
The first control is the ((tool)) selection menu. It creates a
470470
`<select>` element with an option for each tool, and sets up a
@@ -481,7 +481,7 @@ class ToolSelect {
481481
}, name)));
482482
this.dom = elt("label", null, "🖌 Tool: ", this.select);
483483
}
484-
setState(state) { this.select.value = state.tool; }
484+
syncState(state) { this.select.value = state.tool; }
485485
}
486486
```
487487

@@ -508,7 +508,7 @@ Depending on the browser, the color picker might look like this:
508508

509509
if}}
510510

511-
{{index "ColorSelect class", "setState method"}}
511+
{{index "ColorSelect class", "syncState method"}}
512512

513513
This ((control)) creates such a field and wires it up to stay
514514
synchronized with the application state's `color` property.
@@ -523,7 +523,7 @@ class ColorSelect {
523523
});
524524
this.dom = elt("label", null, "🎨 Color: ", this.input);
525525
}
526-
setState(state) { this.input.value = state.color; }
526+
syncState(state) { this.input.value = state.color; }
527527
}
528528
```
529529

@@ -664,7 +664,7 @@ We can now test our application!
664664
controls: [ToolSelect, ColorSelect],
665665
dispatch(action) {
666666
state = updateState(state, action);
667-
app.setState(state);
667+
app.syncState(state);
668668
}
669669
});
670670
document.querySelector("div").appendChild(app.dom);
@@ -700,7 +700,7 @@ class SaveButton {
700700
link.click();
701701
link.remove();
702702
}
703-
setState(state) { this.picture = state.picture; }
703+
syncState(state) { this.picture = state.picture; }
704704
}
705705
```
706706

@@ -742,7 +742,7 @@ class LoadButton {
742742
onclick: () => startLoad(dispatch)
743743
}, "📁 Load");
744744
}
745-
setState() {}
745+
syncState() {}
746746
}
747747
748748
function startLoad(dispatch) {
@@ -916,7 +916,7 @@ class UndoButton {
916916
disabled: state.done.length == 0
917917
}, "⮪ Undo");
918918
}
919-
setState(state) {
919+
syncState(state) {
920920
this.dom.disabled = state.done.length == 0;
921921
}
922922
}
@@ -955,7 +955,7 @@ function startPixelEditor({state = startState,
955955
controls,
956956
dispatch(action) {
957957
state = historyUpdateState(state, action);
958-
app.setState(state);
958+
app.syncState(state);
959959
}
960960
});
961961
return app.dom;
@@ -1081,10 +1081,10 @@ keys are held down.
10811081
...this.controls.reduce(
10821082
(a, c) => a.concat(" ", c.dom), []));
10831083
}
1084-
setState(state) {
1084+
syncState(state) {
10851085
this.state = state;
1086-
this.canvas.setState(state.picture);
1087-
for (let ctrl of this.controls) ctrl.setState(state);
1086+
this.canvas.syncState(state.picture);
1087+
for (let ctrl of this.controls) ctrl.syncState(state);
10881088
}
10891089
}
10901090
@@ -1126,9 +1126,9 @@ in `drawPicture`. Creating a new state and updating the rest of the
11261126
DOM isn't very expensive, but repainting all the pixels on the canvas
11271127
is quite a bit of work.
11281128

1129-
{{index "setState method", "PictureCanvas class"}}
1129+
{{index "syncState method", "PictureCanvas class"}}
11301130

1131-
Find a way to make the `setState` method of `PictureCanvas` faster by
1131+
Find a way to make the `syncState` method of `PictureCanvas` faster by
11321132
redrawing only the pixels that actually changed.
11331133

11341134
{{index "drawPicture function", compatibility}}
@@ -1149,7 +1149,7 @@ transparent again.
11491149
<div></div>
11501150
<script>
11511151
// Change this method
1152-
PictureCanvas.prototype.setState = function(picture) {
1152+
PictureCanvas.prototype.syncState = function(picture) {
11531153
if (this.picture == picture) return;
11541154
this.picture = picture;
11551155
drawPicture(this.picture, this.dom, scale);

21_skillsharing.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,10 @@ class SkillShareApp {
971971
renderUserField(state.user, dispatch),
972972
this.talkDOM,
973973
renderTalkForm(dispatch));
974-
this.setState(state);
974+
this.syncState(state);
975975
}
976976
977-
setState(state) {
977+
syncState(state) {
978978
if (state.talks != this.talks) {
979979
this.talkDOM.textContent = "";
980980
for (let talk of state.talks) {
@@ -1000,7 +1000,7 @@ function runApp() {
10001000
let state, app;
10011001
function dispatch(action) {
10021002
state = handleAction(state, action);
1003-
app.setState(state);
1003+
app.syncState(state);
10041004
}
10051005
10061006
pollTalks(talks => {
@@ -1091,12 +1091,12 @@ to solve it?
10911091

10921092
{{hint
10931093

1094-
{{index "comment field reset (exercise)", template, "setState method"}}
1094+
{{index "comment field reset (exercise)", template, "syncState method"}}
10951095

10961096
The best way to do this is probably to make talks component objects,
1097-
with a `setState` method, so that they can be updated to show a
1097+
with a `syncState` method, so that they can be updated to show a
10981098
modified version of the talk. During normal operation, the only way a
1099-
talk can be changed is by adding more comments, so the `setState`
1099+
talk can be changed is by adding more comments, so the `syncState`
11001100
method can be relatively simple.
11011101

11021102
The difficult part is that, when a changed list of talks comes in, we

code/solutions/16_2_pausing_the_game.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
}
5454

5555
state = state.update(time, arrowKeys);
56-
display.setState(state);
56+
display.syncState(state);
5757
if (state.status == "playing") {
5858
return true;
5959
} else if (ending > 0) {

code/solutions/19_1_keyboard_bindings.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
}
4141
}
4242
}
43-
setState(state) {
43+
syncState(state) {
4444
this.state = state;
45-
this.canvas.setState(state.picture);
46-
for (let ctrl of this.controls) ctrl.setState(state);
45+
this.canvas.syncState(state.picture);
46+
for (let ctrl of this.controls) ctrl.syncState(state);
4747
}
4848
}
4949

code/solutions/19_2_efficient_drawing.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<div></div>
77
<script>
8-
PictureCanvas.prototype.setState = function(picture) {
8+
PictureCanvas.prototype.syncState = function(picture) {
99
if (this.picture == picture) return;
1010
drawPicture(picture, this.dom, scale, this.picture);
1111
this.picture = picture;

code/solutions/21_2_comment_field_resets.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class Talk {
2626
}
2727
}, elt("input", {type: "text", name: "comment"}), " ",
2828
elt("button", {type: "submit"}, "Add comment")));
29-
this.setState(talk);
29+
this.syncState(talk);
3030
}
3131

32-
setState(talk) {
32+
syncState(talk) {
3333
this.talk = talk;
3434
this.comments.textContent = "";
3535
for (let comment of talk.comments) {
@@ -47,18 +47,18 @@ class SkillShareApp {
4747
renderUserField(state.user, dispatch),
4848
this.talkDOM,
4949
renderTalkForm(dispatch));
50-
this.setState(state);
50+
this.syncState(state);
5151
}
5252

53-
setState(state) {
53+
syncState(state) {
5454
if (state.talks == this.talks) return;
5555
this.talks = state.talks;
5656

5757
for (let talk of state.talks) {
5858
let cmp = this.talkMap[talk.title];
5959
if (cmp && cmp.talk.author == talk.author &&
6060
cmp.talk.summary == talk.summary) {
61-
cmp.setState(talk);
61+
cmp.syncState(talk);
6262
} else {
6363
if (cmp) cmp.dom.remove();
6464
cmp = new Talk(talk, this.dispatch);

0 commit comments

Comments
 (0)