Skip to content

Commit

Permalink
Creating subscription on each subscribe.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Sep 14, 2024
1 parent 640bcfd commit 9e771bc
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions Example/AblyChatExample/Mocks/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,29 @@ actor MockMessages: Messages {
let roomID: String
let channel: RealtimeChannel

private var mockSubscription: MockSubscription<Message>
private var mockSubscription: MockSubscription<Message>!

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.channel = MockRealtimeChannel()
self.mockSubscription = MockSubscription<Message>(randomElement: {
}

private func createSubscription() {
mockSubscription = MockSubscription<Message>(randomElement: {
Message(timeserial: "\(Date().timeIntervalSince1970)",
clientID: MockStrings.names.randomElement()!,
roomID: roomID,
text: MockStrings.randomPhrase(),
createdAt: Date(),
metadata: [:],
headers: [:])
clientID: MockStrings.names.randomElement()!,
roomID: self.roomID,
text: MockStrings.randomPhrase(),
createdAt: Date(),
metadata: [:],
headers: [:])
}, interval: 3)
}

func subscribe(bufferingPolicy: BufferingPolicy) async -> MessageSubscription {
MessageSubscription(mockAsyncSequence: mockSubscription) {_ in
createSubscription() // TODO: https://github.com/ably-labs/ably-chat-swift/issues/44
return MessageSubscription(mockAsyncSequence: mockSubscription) {_ in
MockMessagesPaginatedResult(clientID: self.clientID, roomID: self.roomID)
}
}
Expand All @@ -110,6 +114,9 @@ actor MockMessages: Messages {
}

func send(params: SendMessageParams) async throws -> Message {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
let message = Message(timeserial: "\(Date().timeIntervalSince1970)",
clientID: clientID,
roomID: roomID,
Expand All @@ -131,23 +138,29 @@ actor MockRoomReactions: RoomReactions {
let roomID: String
let channel: RealtimeChannel

private var mockSubscription: MockSubscription<Reaction>
private var mockSubscription: MockSubscription<Reaction>!

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.channel = MockRealtimeChannel()
self.mockSubscription = MockSubscription<Reaction>(randomElement: {
}

private func createSubscription() {
mockSubscription = MockSubscription<Reaction>(randomElement: {
Reaction(type: ReactionType.allCases.randomElement()!.rawValue,
metadata: [:],
headers: [:],
createdAt: Date(),
clientID: clientID,
clientID: self.clientID,
isSelf: false)
}, interval: 1)
}

func send(params: SendReactionParams) async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
let reaction = Reaction(type: params.type,
metadata: [:],
headers: [:],
Expand All @@ -158,7 +171,8 @@ actor MockRoomReactions: RoomReactions {
}

func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<Reaction> {
.init(mockAsyncSequence: mockSubscription)
createSubscription()
return .init(mockAsyncSequence: mockSubscription)
}

func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {
Expand All @@ -171,13 +185,16 @@ actor MockTyping: Typing {
let roomID: String
let channel: RealtimeChannel

private var mockSubscription: MockSubscription<TypingEvent>
private var mockSubscription: MockSubscription<TypingEvent>!

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.channel = MockRealtimeChannel()
self.mockSubscription = MockSubscription<TypingEvent>(randomElement: {
}

private func createSubscription() {
mockSubscription = MockSubscription<TypingEvent>(randomElement: {
TypingEvent(currentlyTyping: [
MockStrings.names.randomElement()!,
MockStrings.names.randomElement()!
Expand All @@ -186,18 +203,25 @@ actor MockTyping: Typing {
}

func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<TypingEvent> {
.init(mockAsyncSequence: mockSubscription)
createSubscription()
return .init(mockAsyncSequence: mockSubscription)
}

func get() async throws -> Set<String> {
Set(MockStrings.names.prefix(2))
}

func start() async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(TypingEvent(currentlyTyping: [clientID]))
}

func stop() async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(TypingEvent(currentlyTyping: [clientID]))
}

Expand All @@ -210,12 +234,15 @@ actor MockPresence: Presence {
let clientID: String
let roomID: String

private var mockSubscription: MockSubscription<PresenceEvent>
private var mockSubscription: MockSubscription<PresenceEvent>!

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.mockSubscription = MockSubscription<PresenceEvent>(randomElement: {
}

private func createSubscription() {
mockSubscription = MockSubscription<PresenceEvent>(randomElement: {
PresenceEvent(action: [.enter, .leave].randomElement()!,
clientID: MockStrings.names.randomElement()!,
timestamp: Date(),
Expand Down Expand Up @@ -248,13 +275,19 @@ actor MockPresence: Presence {
}

func enter() async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(PresenceEvent(action: .enter,
clientID: clientID,
timestamp: Date(),
data: nil))
}

func enter(data: PresenceData) async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(PresenceEvent(action: .enter,
clientID: clientID,
timestamp: Date(),
Expand All @@ -270,25 +303,33 @@ actor MockPresence: Presence {
}

func leave() async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(PresenceEvent(action: .leave,
clientID: clientID,
timestamp: Date(),
data: nil))
}

func leave(data: PresenceData) async throws {
guard let mockSubscription = mockSubscription else {
fatalError("Call `subscribe` first.")
}
mockSubscription.emit(PresenceEvent(action: .leave,
clientID: clientID,
timestamp: Date(),
data: data))
}

func subscribe(event: PresenceEventType) -> Subscription<PresenceEvent> {
.init(mockAsyncSequence: mockSubscription)
createSubscription()
return .init(mockAsyncSequence: mockSubscription)
}

func subscribe(events: [PresenceEventType]) -> Subscription<PresenceEvent> {
.init(mockAsyncSequence: mockSubscription)
createSubscription()
return .init(mockAsyncSequence: mockSubscription)
}

func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {
Expand Down

0 comments on commit 9e771bc

Please sign in to comment.