Skip to content

Commit

Permalink
Added mock actors for RoomReactions protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Sep 1, 2024
1 parent c4fce6d commit 5688ef2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
37 changes: 32 additions & 5 deletions Example/AblyChatExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct ContentView: View {

@State private var title = "Room: "
@State private var messages = [Message]()
@State private var reactions = ""
@State private var newMessage = ""

private func room() async -> Room {
Expand All @@ -18,7 +19,12 @@ struct ContentView: View {

var body: some View {
VStack {
Text(title).font(.headline)
Text(title)
.font(.headline)
.padding(5)
Text(reactions)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(5)
List(messages, id: \.timeserial) { message in
MessageBasicView(message: message)
.flip()
Expand All @@ -45,18 +51,33 @@ struct ContentView: View {
Text("Send")
#endif
}
Button(action: {
Task {
try await sendReaction(type: "👍")
}
}) {
Text("👍")
}
}
.padding(.bottom, 10)
.padding(.horizontal, 12)
.task {
let room = await room()
title = "Room: \(room.roomID)"
for await message in await room.messages.subscribe(bufferingPolicy: .unbounded) {
title = await "Room: \(room().roomID)"
}
.task {
for await message in await room().messages.subscribe(bufferingPolicy: .unbounded) {
withAnimation {
messages.insert(message, at: 0)
}
}
}
.task {
for await reaction in await room().reactions.subscribe(bufferingPolicy: .unbounded) {
withAnimation {
reactions.append(reaction.type == "like" ? "👍" : "🤷")
}
}
}
}
}

Expand All @@ -68,6 +89,13 @@ struct ContentView: View {
}
newMessage = ""
}

func sendReaction(type: String) async throws {
try await room().reactions.send(params: .init(type: "like"))
withAnimation {
reactions.append("👍")
}
}
}

struct MessageBasicView: View {
Expand All @@ -86,7 +114,6 @@ struct MessageBasicView: View {
Spacer()
}
}
.padding(.leading, 5)
.listRowSeparator(.hidden)
}
}
Expand Down
57 changes: 56 additions & 1 deletion Example/AblyChatExample/Mocks/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ actor MockRoom: Room {
}

nonisolated var reactions: any RoomReactions {
fatalError("Not yet implemented")
MockRoomReactions(clientID: "AblyTest", roomID: roomID)
}

nonisolated var typing: any Typing {
Expand Down Expand Up @@ -141,3 +141,58 @@ struct MockMessageSubscription: Sendable, AsyncSequence, AsyncIteratorProtocol {
self
}
}

actor MockRoomReactions: RoomReactions {
let clientID: String
let roomID: String
let channel: RealtimeChannel

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.channel = MockRealtimeChannel()
}

func send(params: RoomReactionParams) async throws {
_ = Reaction(type: "like",
metadata: [:],
headers: [:],
createdAt: Date(),
clientID: clientID,
isSelf: true)
}

func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<Reaction> {
.init(mockAsyncSequence: MockReactionSubscription(clientID: clientID, roomID: roomID))
}

func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {
fatalError("Not yet implemented")
}
}

struct MockReactionSubscription: Sendable, AsyncSequence, AsyncIteratorProtocol {
typealias Element = Reaction

let clientID: String
let roomID: String

public init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
}

public mutating func next() async -> Element? {
try? await Task.sleep(nanoseconds: 1 * 1_000_000_000)
return Reaction(type: "like",
metadata: [:],
headers: [:],
createdAt: Date(),
clientID: self.clientID,
isSelf: false)
}

public func makeAsyncIterator() -> Self {
self
}
}
10 changes: 9 additions & 1 deletion Sources/AblyChat/RoomReactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ public protocol RoomReactions: AnyObject, Sendable, EmitsDiscontinuities {
}

public struct RoomReactionParams: Sendable {
public init() {}
public var type: String
public var metadata: ReactionMetadata?
public var headers: ReactionHeaders?

public init(type: String, metadata: ReactionMetadata? = nil, headers: ReactionHeaders? = nil) {
self.type = type
self.metadata = metadata
self.headers = headers
}
}

0 comments on commit 5688ef2

Please sign in to comment.