Skip to content

Commit 9e771bc

Browse files
committed
Creating subscription on each subscribe.
1 parent 640bcfd commit 9e771bc

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

Example/AblyChatExample/Mocks/Mocks.swift

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,29 @@ actor MockMessages: Messages {
8282
let roomID: String
8383
let channel: RealtimeChannel
8484

85-
private var mockSubscription: MockSubscription<Message>
85+
private var mockSubscription: MockSubscription<Message>!
8686

8787
init(clientID: String, roomID: String) {
8888
self.clientID = clientID
8989
self.roomID = roomID
9090
self.channel = MockRealtimeChannel()
91-
self.mockSubscription = MockSubscription<Message>(randomElement: {
91+
}
92+
93+
private func createSubscription() {
94+
mockSubscription = MockSubscription<Message>(randomElement: {
9295
Message(timeserial: "\(Date().timeIntervalSince1970)",
93-
clientID: MockStrings.names.randomElement()!,
94-
roomID: roomID,
95-
text: MockStrings.randomPhrase(),
96-
createdAt: Date(),
97-
metadata: [:],
98-
headers: [:])
96+
clientID: MockStrings.names.randomElement()!,
97+
roomID: self.roomID,
98+
text: MockStrings.randomPhrase(),
99+
createdAt: Date(),
100+
metadata: [:],
101+
headers: [:])
99102
}, interval: 3)
100103
}
101104

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

112116
func send(params: SendMessageParams) async throws -> Message {
117+
guard let mockSubscription = mockSubscription else {
118+
fatalError("Call `subscribe` first.")
119+
}
113120
let message = Message(timeserial: "\(Date().timeIntervalSince1970)",
114121
clientID: clientID,
115122
roomID: roomID,
@@ -131,23 +138,29 @@ actor MockRoomReactions: RoomReactions {
131138
let roomID: String
132139
let channel: RealtimeChannel
133140

134-
private var mockSubscription: MockSubscription<Reaction>
141+
private var mockSubscription: MockSubscription<Reaction>!
135142

136143
init(clientID: String, roomID: String) {
137144
self.clientID = clientID
138145
self.roomID = roomID
139146
self.channel = MockRealtimeChannel()
140-
self.mockSubscription = MockSubscription<Reaction>(randomElement: {
147+
}
148+
149+
private func createSubscription() {
150+
mockSubscription = MockSubscription<Reaction>(randomElement: {
141151
Reaction(type: ReactionType.allCases.randomElement()!.rawValue,
142152
metadata: [:],
143153
headers: [:],
144154
createdAt: Date(),
145-
clientID: clientID,
155+
clientID: self.clientID,
146156
isSelf: false)
147157
}, interval: 1)
148158
}
149159

150160
func send(params: SendReactionParams) async throws {
161+
guard let mockSubscription = mockSubscription else {
162+
fatalError("Call `subscribe` first.")
163+
}
151164
let reaction = Reaction(type: params.type,
152165
metadata: [:],
153166
headers: [:],
@@ -158,7 +171,8 @@ actor MockRoomReactions: RoomReactions {
158171
}
159172

160173
func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<Reaction> {
161-
.init(mockAsyncSequence: mockSubscription)
174+
createSubscription()
175+
return .init(mockAsyncSequence: mockSubscription)
162176
}
163177

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

174-
private var mockSubscription: MockSubscription<TypingEvent>
188+
private var mockSubscription: MockSubscription<TypingEvent>!
175189

176190
init(clientID: String, roomID: String) {
177191
self.clientID = clientID
178192
self.roomID = roomID
179193
self.channel = MockRealtimeChannel()
180-
self.mockSubscription = MockSubscription<TypingEvent>(randomElement: {
194+
}
195+
196+
private func createSubscription() {
197+
mockSubscription = MockSubscription<TypingEvent>(randomElement: {
181198
TypingEvent(currentlyTyping: [
182199
MockStrings.names.randomElement()!,
183200
MockStrings.names.randomElement()!
@@ -186,18 +203,25 @@ actor MockTyping: Typing {
186203
}
187204

188205
func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<TypingEvent> {
189-
.init(mockAsyncSequence: mockSubscription)
206+
createSubscription()
207+
return .init(mockAsyncSequence: mockSubscription)
190208
}
191209

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

196214
func start() async throws {
215+
guard let mockSubscription = mockSubscription else {
216+
fatalError("Call `subscribe` first.")
217+
}
197218
mockSubscription.emit(TypingEvent(currentlyTyping: [clientID]))
198219
}
199220

200221
func stop() async throws {
222+
guard let mockSubscription = mockSubscription else {
223+
fatalError("Call `subscribe` first.")
224+
}
201225
mockSubscription.emit(TypingEvent(currentlyTyping: [clientID]))
202226
}
203227

@@ -210,12 +234,15 @@ actor MockPresence: Presence {
210234
let clientID: String
211235
let roomID: String
212236

213-
private var mockSubscription: MockSubscription<PresenceEvent>
237+
private var mockSubscription: MockSubscription<PresenceEvent>!
214238

215239
init(clientID: String, roomID: String) {
216240
self.clientID = clientID
217241
self.roomID = roomID
218-
self.mockSubscription = MockSubscription<PresenceEvent>(randomElement: {
242+
}
243+
244+
private func createSubscription() {
245+
mockSubscription = MockSubscription<PresenceEvent>(randomElement: {
219246
PresenceEvent(action: [.enter, .leave].randomElement()!,
220247
clientID: MockStrings.names.randomElement()!,
221248
timestamp: Date(),
@@ -248,13 +275,19 @@ actor MockPresence: Presence {
248275
}
249276

250277
func enter() async throws {
278+
guard let mockSubscription = mockSubscription else {
279+
fatalError("Call `subscribe` first.")
280+
}
251281
mockSubscription.emit(PresenceEvent(action: .enter,
252282
clientID: clientID,
253283
timestamp: Date(),
254284
data: nil))
255285
}
256286

257287
func enter(data: PresenceData) async throws {
288+
guard let mockSubscription = mockSubscription else {
289+
fatalError("Call `subscribe` first.")
290+
}
258291
mockSubscription.emit(PresenceEvent(action: .enter,
259292
clientID: clientID,
260293
timestamp: Date(),
@@ -270,25 +303,33 @@ actor MockPresence: Presence {
270303
}
271304

272305
func leave() async throws {
306+
guard let mockSubscription = mockSubscription else {
307+
fatalError("Call `subscribe` first.")
308+
}
273309
mockSubscription.emit(PresenceEvent(action: .leave,
274310
clientID: clientID,
275311
timestamp: Date(),
276312
data: nil))
277313
}
278314

279315
func leave(data: PresenceData) async throws {
316+
guard let mockSubscription = mockSubscription else {
317+
fatalError("Call `subscribe` first.")
318+
}
280319
mockSubscription.emit(PresenceEvent(action: .leave,
281320
clientID: clientID,
282321
timestamp: Date(),
283322
data: data))
284323
}
285324

286325
func subscribe(event: PresenceEventType) -> Subscription<PresenceEvent> {
287-
.init(mockAsyncSequence: mockSubscription)
326+
createSubscription()
327+
return .init(mockAsyncSequence: mockSubscription)
288328
}
289329

290330
func subscribe(events: [PresenceEventType]) -> Subscription<PresenceEvent> {
291-
.init(mockAsyncSequence: mockSubscription)
331+
createSubscription()
332+
return .init(mockAsyncSequence: mockSubscription)
292333
}
293334

294335
func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {

0 commit comments

Comments
 (0)