Skip to content

Commit

Permalink
Update emojisDemo, README
Browse files Browse the repository at this point in the history
  • Loading branch information
cham-s committed Nov 18, 2024
1 parent f0d76da commit a095b1f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
// Executable Demos
.executable(name: "EmojisDemo", targets: ["EmojisDemo"]),
.executable(name: "GettingStartedDemo", targets: ["GettingStartedDemo"]),
.executable(name: "ListenOperatorDemo", targets: ["ListenOperatorDemo"]),
.executable(name: "OnOperatorDemo", targets: ["OnOperatorDemo"]),
.executable(name: "LogOperatorDemo", targets: ["LogOperatorDemo"]),
.executable(name: "PingIntervalDemo", targets: ["PingIntervalDemo"]),

Expand Down Expand Up @@ -57,7 +57,7 @@ let package = Package(
),

.executableTarget(
name: "ListenOperatorDemo",
name: "OnOperatorDemo",
dependencies: [
.product(name: "AsyncWebSocketClient", package: "async-websocket"),
.product(name: "AsyncWebSocketClientLive", package: "async-websocket"),
Expand Down
59 changes: 28 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Demonstrates some uses of the library.

- [Ping Interval](#interval)

- [Listen Operator](#listen)
- [On Operator](#listen)

- [Log Operator](#log)

Expand Down Expand Up @@ -138,7 +138,7 @@ struct MainApp {

#### Ping Interval<a name="interval"></a>

Some servers requires the client to ping them at a specified interval to keep the connection alive. This demo shows how to set up this operation.
Some servers require the client to ping them at a specified interval to keep the connection alive. This demo shows how to set up this operation.

```shell
swift run PingIntervalDemo
Expand Down Expand Up @@ -185,12 +185,12 @@ struct MainApp {
}
```

#### Listen Operator<a name="listen"></a>
#### On Operator<a name="listen"></a>

Demonstrates the use of the `on` operator to focus on a particular event.

```shell
swift run ListenOperatorDemo
swift run OnOperatorDemo
```

```swift
Expand Down Expand Up @@ -324,36 +324,33 @@ struct MainApp {
)

for await _ in connection
.log()
.on(\.connected)
{
// Starts listening for emoji messages
for await message in try await webSocket.receive(id)
.emojiMessage() {
switch message {

case let .welcome(welcome):
print(welcome.message)
startStreamTask.withValue {
$0 = Task {
try await request(id: id, request: .startStream)
}
}

case let .event(event):
switch event {
case let .emojiDidChangedEvent(emoji):
print("New emoji: ", emoji.newEmoji)
}

case let .response(result):
try await onResponse(result)
case .request:
// Not handled by the client
break
.log()
.on(\.connected)
{
let messages = try await webSocket.receive(id).emojiMessage()
// Starts listening for emoji messages
for await message in messages {
switch message {
case let .welcome(welcome):
print(welcome.message)
startStreamTask.withValue {
$0 = Task {
try await request(id: id, request: .startStream)
}
}
case let .event(event):
switch event {
case let .emojiDidChangedEvent(emoji):
print("New emoji: ", emoji.newEmoji)
}
case let .response(result):
try await onResponse(result)
case .request:
// Not handled by the client
break
}
}
}

// Awaiting for possible error thrown during tasks execution
try await startStreamTask.value?.value
Expand Down
10 changes: 5 additions & 5 deletions Sources/EmojisDemo/EmojisDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ struct MainApp {
.log()
.on(\.connected)
{
let messages = try await webSocket.receive(id).emojiMessage()

// Starts listening for emoji messages
for await message in try await webSocket.receive(id)
.emojiMessage() {
for await message in messages {
switch message {

case let .welcome(welcome):
print(welcome.message)
startStreamTask.withValue {
$0 = Task {
try await request(id: id, request: .startStream)
}
}

case let .event(event):
switch event {
case let .emojiDidChangedEvent(emoji):
print("New emoji: ", emoji.newEmoji)
}

case let .response(result):
try await onResponse(result)
case .request:
Expand Down

0 comments on commit a095b1f

Please sign in to comment.