Skip to content

Commit bb5b962

Browse files
committed
tinychat
1 parent 2dbeece commit bb5b962

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

Chapters/TinyChat/TinyChat.md

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ We should be able to add, clear the list, and count the number of messages, so w
183183
```
184184
TCMessageQueue >> add: aMessage
185185
messages add: aMessage
186+
```
186187

188+
```
187189
TCMessageQueue >> reset
188190
messages removeAll
191+
```
189192

193+
```
190194
TCMessageQueue >> size
191195
^ messages size
192196
```
@@ -354,7 +358,7 @@ Now the server is finished and we can test it.
354358
First, let us begin by starting it:
355359

356360
```
357-
TCServer startOn: 8181
361+
TCServer startOn: 8181
358362
```
359363

360364

@@ -390,7 +394,7 @@ ZnClient new
390394
Now we can concentrate on the client part of TinyChat. We decomposed the client into two classes:
391395

392396
- `TinyChat` is the class that defines the connection logic (connection, send, and message reception),
393-
- `TCConsole` is a class defining the user interface.
397+
- `TCConsolePresenter` is a class defining the user interface.
394398

395399

396400
The logic of the client is:
@@ -451,10 +455,12 @@ Now that we have these low-level operations we can define the three HTTP command
451455
```
452456
TinyChat >> cmdLastMessageID
453457
^ self command: '/messages/count'
454-
458+
```
459+
```
455460
TinyChat >> cmdNewMessage
456461
^self command: '/messages/add'
457-
462+
```
463+
```
458464
TinyChat >> cmdMessagesFromLastIndexToEnd
459465
"Returns the server messages from my current last index to the last one on the server."
460466
^ self command: '/messages' argument: lastMessageIndex
@@ -585,7 +591,7 @@ Note that a good evolution would be to decouple the model from its user interfac
585591

586592
```
587593
TinyChat >> start
588-
console := TCConsole attach: self.
594+
console := TCConsolePresenter attach: self.
589595
self sendNewMessage: (TCMessage from: login text: 'I joined the chat room').
590596
lastMessageIndex := self readLastMessageID.
591597
self refreshMessages.
@@ -598,46 +604,51 @@ TinyChat >> start
598604
The user interface is composed of a window with a list and an input field as shown in Figure *@tinychatclient@*.
599605

600606
```
601-
ComposablePresenter << #TCConsole
607+
SpPresenter << #TCConsolePresenter
602608
slots: {#chat . #list . #input};
603609
package: 'TinyChat-client'
604610
```
605611

606612

607-
Note that the class `TCConsole` inherits from `ComposablePresenter`. This class is the root of the user interface logic classes.
608-
`TCConsole` defines the logic of the client interface (i.e. what happens when we enter text in the input field...). Based on the information given in this class, the Spec user interface builder automatically builds the visual representation.
609-
The `chat` instance variable is a reference to an instance of the client model `TinyChat` and requires a setter method (`chat:`). The `list` and `input` instance variables both require an accessor. This is required by the User Interface builder.
613+
Note that the class `TCConsolePresenter` inherits from `SpPresenter`. This class is the root of the user interface logic classes.
614+
`TCConsolePresenter` defines the logic of the client interface (i.e. what happens when we enter text in the input field...).
615+
Based on the information given in this class, the Spec user interface builder automatically builds the visual representation.
616+
The `chat` instance variable is a reference to an instance of the client model `TinyChat` and requires a setter method (`chat:`). The `list` and `input` instance variables both require an accessor.
617+
This is required by the User Interface builder.
610618

611619
```
612-
TCConsole >> input
620+
TCConsolePresenter >> input
613621
^ input
614-
615-
TCConsole >> list
622+
```
623+
```
624+
TCConsolePresenter >> list
616625
^ list
617-
618-
TCConsole >> chat: anObject
626+
```
627+
```
628+
TCConsolePresenter >> chat: anObject
619629
chat := anObject
620630
```
621631

622632

623633
We set the title of the window by defining the method `title`.
624634

625635
```
626-
TCConsole >> title
636+
TCConsolePresenter >> title
627637
^ 'TinyChat'
628638
```
629639

630640

631641
Now we should specify the layout of the graphical elements that compose the client.
632-
To do so we define the class method `TCConsole class>>defaultSpec`. Here we need a column with a list and an input field placed right below.
642+
To do so we define the class method `TCConsolePresenter>>defaultLayout`. Here we need a column with a list and an input field placed right below.
633643

634644
```
635-
TCConsole class >> defaultSpec
636-
<spec: #default>
645+
TCConsolePresenter >> defaultLayout
646+
647+
^ SpBoxLayout newTopToBottom
648+
add: list;
649+
add: input ;
650+
yourself
637651
638-
^ SpecLayout composed
639-
newColumn: [ :c |
640-
c add: #list; add: #input height: 30 ]; yourself
641652
```
642653

643654

@@ -647,13 +658,14 @@ The message `acceptBlock:` defines the action to be executed then the text is en
647658
Here we send it to the chat model and empty it.
648659

649660
```
650-
TCConsole >> initializeWidgets
661+
TCConsolePresenter >> initializeWidgets
662+
663+
list := self newList.
651664
652-
list := ListModel new.
653-
input := TextInputFieldModel new
654-
ghostText: 'Type your message here...';
655-
enabled: true;
656-
acceptBlock: [ :string |
665+
input := self newTextInput.
666+
input
667+
placeholder: 'Type your message here...';
668+
whenSubmitDo: [ :string |
657669
chat send: string.
658670
input text: '' ].
659671
self focusOrder add: input.
@@ -673,15 +685,13 @@ Finally we need to define the class method `TCConsole class>>attach:` that gets
673685
This method opens the graphical elements and puts in place a mechanism that will close the connection as soon as the client closes the window.
674686

675687
```
676-
TCConsole class >> attach: aTinyChat
688+
TCConsolePresenter class >> attach: aTinyChat
677689
| window |
678690
window := self new chat: aTinyChat.
679-
window openWithSpec whenClosedDo: [ aTinyChat disconnect ].
691+
window open whenClosedDo: [ aTinyChat disconnect ].
680692
^ window
681693
```
682694

683-
684-
685695
### Now chatting
686696

687697

0 commit comments

Comments
 (0)