@@ -82,25 +82,29 @@ actor MockMessages: Messages {
82
82
let roomID : String
83
83
let channel : RealtimeChannel
84
84
85
- private var mockSubscription : MockSubscription < Message >
85
+ private var mockSubscription : MockSubscription < Message > !
86
86
87
87
init ( clientID: String , roomID: String ) {
88
88
self . clientID = clientID
89
89
self . roomID = roomID
90
90
self . channel = MockRealtimeChannel ( )
91
- self . mockSubscription = MockSubscription < Message > ( randomElement: {
91
+ }
92
+
93
+ private func createSubscription( ) {
94
+ mockSubscription = MockSubscription < Message > ( randomElement: {
92
95
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: [ : ] )
99
102
} , interval: 3 )
100
103
}
101
104
102
105
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
104
108
MockMessagesPaginatedResult ( clientID: self . clientID, roomID: self . roomID)
105
109
}
106
110
}
@@ -110,6 +114,9 @@ actor MockMessages: Messages {
110
114
}
111
115
112
116
func send( params: SendMessageParams ) async throws -> Message {
117
+ guard let mockSubscription = mockSubscription else {
118
+ fatalError ( " Call `subscribe` first. " )
119
+ }
113
120
let message = Message ( timeserial: " \( Date ( ) . timeIntervalSince1970) " ,
114
121
clientID: clientID,
115
122
roomID: roomID,
@@ -131,23 +138,29 @@ actor MockRoomReactions: RoomReactions {
131
138
let roomID : String
132
139
let channel : RealtimeChannel
133
140
134
- private var mockSubscription : MockSubscription < Reaction >
141
+ private var mockSubscription : MockSubscription < Reaction > !
135
142
136
143
init ( clientID: String , roomID: String ) {
137
144
self . clientID = clientID
138
145
self . roomID = roomID
139
146
self . channel = MockRealtimeChannel ( )
140
- self . mockSubscription = MockSubscription < Reaction > ( randomElement: {
147
+ }
148
+
149
+ private func createSubscription( ) {
150
+ mockSubscription = MockSubscription < Reaction > ( randomElement: {
141
151
Reaction ( type: ReactionType . allCases. randomElement ( ) !. rawValue,
142
152
metadata: [ : ] ,
143
153
headers: [ : ] ,
144
154
createdAt: Date ( ) ,
145
- clientID: clientID,
155
+ clientID: self . clientID,
146
156
isSelf: false )
147
157
} , interval: 1 )
148
158
}
149
159
150
160
func send( params: SendReactionParams ) async throws {
161
+ guard let mockSubscription = mockSubscription else {
162
+ fatalError ( " Call `subscribe` first. " )
163
+ }
151
164
let reaction = Reaction ( type: params. type,
152
165
metadata: [ : ] ,
153
166
headers: [ : ] ,
@@ -158,7 +171,8 @@ actor MockRoomReactions: RoomReactions {
158
171
}
159
172
160
173
func subscribe( bufferingPolicy: BufferingPolicy ) -> Subscription < Reaction > {
161
- . init( mockAsyncSequence: mockSubscription)
174
+ createSubscription ( )
175
+ return . init( mockAsyncSequence: mockSubscription)
162
176
}
163
177
164
178
func subscribeToDiscontinuities( ) async -> Subscription < ARTErrorInfo > {
@@ -171,13 +185,16 @@ actor MockTyping: Typing {
171
185
let roomID : String
172
186
let channel : RealtimeChannel
173
187
174
- private var mockSubscription : MockSubscription < TypingEvent >
188
+ private var mockSubscription : MockSubscription < TypingEvent > !
175
189
176
190
init ( clientID: String , roomID: String ) {
177
191
self . clientID = clientID
178
192
self . roomID = roomID
179
193
self . channel = MockRealtimeChannel ( )
180
- self . mockSubscription = MockSubscription < TypingEvent > ( randomElement: {
194
+ }
195
+
196
+ private func createSubscription( ) {
197
+ mockSubscription = MockSubscription < TypingEvent > ( randomElement: {
181
198
TypingEvent ( currentlyTyping: [
182
199
MockStrings . names. randomElement ( ) !,
183
200
MockStrings . names. randomElement ( ) !
@@ -186,18 +203,25 @@ actor MockTyping: Typing {
186
203
}
187
204
188
205
func subscribe( bufferingPolicy: BufferingPolicy ) -> Subscription < TypingEvent > {
189
- . init( mockAsyncSequence: mockSubscription)
206
+ createSubscription ( )
207
+ return . init( mockAsyncSequence: mockSubscription)
190
208
}
191
209
192
210
func get( ) async throws -> Set < String > {
193
211
Set ( MockStrings . names. prefix ( 2 ) )
194
212
}
195
213
196
214
func start( ) async throws {
215
+ guard let mockSubscription = mockSubscription else {
216
+ fatalError ( " Call `subscribe` first. " )
217
+ }
197
218
mockSubscription. emit ( TypingEvent ( currentlyTyping: [ clientID] ) )
198
219
}
199
220
200
221
func stop( ) async throws {
222
+ guard let mockSubscription = mockSubscription else {
223
+ fatalError ( " Call `subscribe` first. " )
224
+ }
201
225
mockSubscription. emit ( TypingEvent ( currentlyTyping: [ clientID] ) )
202
226
}
203
227
@@ -210,12 +234,15 @@ actor MockPresence: Presence {
210
234
let clientID : String
211
235
let roomID : String
212
236
213
- private var mockSubscription : MockSubscription < PresenceEvent >
237
+ private var mockSubscription : MockSubscription < PresenceEvent > !
214
238
215
239
init ( clientID: String , roomID: String ) {
216
240
self . clientID = clientID
217
241
self . roomID = roomID
218
- self . mockSubscription = MockSubscription < PresenceEvent > ( randomElement: {
242
+ }
243
+
244
+ private func createSubscription( ) {
245
+ mockSubscription = MockSubscription < PresenceEvent > ( randomElement: {
219
246
PresenceEvent ( action: [ . enter, . leave] . randomElement ( ) !,
220
247
clientID: MockStrings . names. randomElement ( ) !,
221
248
timestamp: Date ( ) ,
@@ -248,13 +275,19 @@ actor MockPresence: Presence {
248
275
}
249
276
250
277
func enter( ) async throws {
278
+ guard let mockSubscription = mockSubscription else {
279
+ fatalError ( " Call `subscribe` first. " )
280
+ }
251
281
mockSubscription. emit ( PresenceEvent ( action: . enter,
252
282
clientID: clientID,
253
283
timestamp: Date ( ) ,
254
284
data: nil ) )
255
285
}
256
286
257
287
func enter( data: PresenceData ) async throws {
288
+ guard let mockSubscription = mockSubscription else {
289
+ fatalError ( " Call `subscribe` first. " )
290
+ }
258
291
mockSubscription. emit ( PresenceEvent ( action: . enter,
259
292
clientID: clientID,
260
293
timestamp: Date ( ) ,
@@ -270,25 +303,33 @@ actor MockPresence: Presence {
270
303
}
271
304
272
305
func leave( ) async throws {
306
+ guard let mockSubscription = mockSubscription else {
307
+ fatalError ( " Call `subscribe` first. " )
308
+ }
273
309
mockSubscription. emit ( PresenceEvent ( action: . leave,
274
310
clientID: clientID,
275
311
timestamp: Date ( ) ,
276
312
data: nil ) )
277
313
}
278
314
279
315
func leave( data: PresenceData ) async throws {
316
+ guard let mockSubscription = mockSubscription else {
317
+ fatalError ( " Call `subscribe` first. " )
318
+ }
280
319
mockSubscription. emit ( PresenceEvent ( action: . leave,
281
320
clientID: clientID,
282
321
timestamp: Date ( ) ,
283
322
data: data) )
284
323
}
285
324
286
325
func subscribe( event: PresenceEventType ) -> Subscription < PresenceEvent > {
287
- . init( mockAsyncSequence: mockSubscription)
326
+ createSubscription ( )
327
+ return . init( mockAsyncSequence: mockSubscription)
288
328
}
289
329
290
330
func subscribe( events: [ PresenceEventType ] ) -> Subscription < PresenceEvent > {
291
- . init( mockAsyncSequence: mockSubscription)
331
+ createSubscription ( )
332
+ return . init( mockAsyncSequence: mockSubscription)
292
333
}
293
334
294
335
func subscribeToDiscontinuities( ) async -> Subscription < ARTErrorInfo > {
0 commit comments