@@ -68,32 +68,49 @@ public enum RealtimeListenTypes: String {
68
68
case presence
69
69
}
70
70
71
+ /// Represents the broadcast and presence options for a channel.
71
72
public struct RealtimeChannelOptions {
72
- public var ack : Bool
73
- public var this : Bool
74
- public var key : String
73
+ /// Used to track presence payload across clients. Must be unique per client. If `nil`, the server
74
+ /// will generate one.
75
+ var presenceKey : String ?
76
+ /// Enables the client to receive their own`broadcast` messages
77
+ var broadcastSelf : Bool
78
+ /// Instructs the server to acknowledge the client's `broadcast` messages
79
+ var broadcastAcknowledge : Bool
75
80
76
- public init ( ack: Bool = false , this: Bool = false , key: String = " " ) {
77
- self . ack = ack
78
- self . this = this
79
- self . key = key
81
+ public init (
82
+ presenceKey: String ? = nil ,
83
+ broadcastSelf: Bool = false ,
84
+ broadcastAcknowledge: Bool = false
85
+ ) {
86
+ self . presenceKey = presenceKey
87
+ self . broadcastSelf = broadcastSelf
88
+ self . broadcastAcknowledge = broadcastAcknowledge
80
89
}
81
90
82
- var asDictionary : [ String : Any ] {
91
+ /// Parameters used to configure the channel
92
+ var params : [ String : [ String : Any ] ] {
83
93
[
84
94
" config " : [
85
- " broadcast " : [
86
- " ack " : ack,
87
- " self " : this,
88
- ] ,
89
95
" presence " : [
90
- " key " : key
96
+ " key " : presenceKey ?? " " ,
91
97
] ,
92
- ]
98
+ " broadcast " : [
99
+ " ack " : broadcastAcknowledge,
100
+ " self " : broadcastSelf,
101
+ ] ,
102
+ ] ,
93
103
]
94
104
}
95
105
}
96
106
107
+ /// Represents the different status of a push
108
+ public enum PushStatus : String {
109
+ case ok
110
+ case error
111
+ case timeout
112
+ }
113
+
97
114
public enum RealtimeSubscribeStates {
98
115
case subscribed
99
116
case timedOut
@@ -218,7 +235,7 @@ public class RealtimeChannel {
218
235
)
219
236
220
237
/// Handle when a response is received after join()
221
- joinPush. delegateReceive ( " ok " , to: self ) { ( self , _) in
238
+ joinPush. delegateReceive ( . ok , to: self ) { ( self , _) in
222
239
// Mark the RealtimeChannel as joined
223
240
self . state = ChannelState . joined
224
241
@@ -231,13 +248,13 @@ public class RealtimeChannel {
231
248
}
232
249
233
250
// Perform if RealtimeChannel errors while attempting to joi
234
- joinPush. delegateReceive ( " error " , to: self ) { ( self , _) in
251
+ joinPush. delegateReceive ( . error, to: self ) { ( self , _) in
235
252
self . state = . errored
236
253
if self . socket? . isConnected == true { self . rejoinTimer. scheduleTimeout ( ) }
237
254
}
238
255
239
256
// Handle when the join push times out when sending after join()
240
- joinPush. delegateReceive ( " timeout " , to: self ) { ( self , _) in
257
+ joinPush. delegateReceive ( . timeout, to: self ) { ( self , _) in
241
258
// log that the channel timed out
242
259
self . socket? . logItems (
243
260
" channel " , " timeout \( self . topic) \( self . joinRef ?? " " ) after \( self . timeout) s "
@@ -357,7 +374,7 @@ public class RealtimeChannel {
357
374
358
375
var accessTokenPayload : Payload = [ : ]
359
376
var config : Payload = [
360
- " postgres_changes " : bindings. value [ " postgres_changes " ] ? . map ( \. filter) ?? [ ]
377
+ " postgres_changes " : bindings. value [ " postgres_changes " ] ? . map ( \. filter) ?? [ ] ,
361
378
]
362
379
363
380
config [ " broadcast " ] = broadcast
@@ -373,7 +390,7 @@ public class RealtimeChannel {
373
390
rejoin ( )
374
391
375
392
joinPush
376
- . delegateReceive ( " ok " , to: self ) { ( self , message) in
393
+ . delegateReceive ( . ok , to: self ) { ( self , message) in
377
394
if self . socket? . accessToken != nil {
378
395
self . socket? . setAuth ( self . socket? . accessToken)
379
396
}
@@ -388,7 +405,7 @@ public class RealtimeChannel {
388
405
let bindingsCount = clientPostgresBindings. count
389
406
var newPostgresBindings : [ Binding ] = [ ]
390
407
391
- for i in 0 ..< bindingsCount {
408
+ for i in 0 ..< bindingsCount {
392
409
let clientPostgresBinding = clientPostgresBindings [ i]
393
410
394
411
let event = clientPostgresBinding. filter [ " event " ]
@@ -399,9 +416,9 @@ public class RealtimeChannel {
399
416
let serverPostgresFilter = serverPostgresFilters [ i]
400
417
401
418
if serverPostgresFilter [ " event " , as: String . self] == event,
402
- serverPostgresFilter [ " schema " , as: String . self] == schema,
403
- serverPostgresFilter [ " table " , as: String . self] == table,
404
- serverPostgresFilter [ " filter " , as: String . self] == filter
419
+ serverPostgresFilter [ " schema " , as: String . self] == schema,
420
+ serverPostgresFilter [ " table " , as: String . self] == table,
421
+ serverPostgresFilter [ " filter " , as: String . self] == filter
405
422
{
406
423
newPostgresBindings. append (
407
424
Binding (
@@ -426,12 +443,12 @@ public class RealtimeChannel {
426
443
}
427
444
callback ? ( . subscribed, nil )
428
445
}
429
- . delegateReceive ( " error " , to: self ) { _, message in
446
+ . delegateReceive ( . error, to: self ) { _, message in
430
447
let values = message. payload. values. map { " \( $0) " }
431
448
let error = RealtimeError ( values. isEmpty ? " error " : values. joined ( separator: " , " ) )
432
449
callback ? ( . channelError, error)
433
450
}
434
- . delegateReceive ( " timeout " , to: self ) { _, _ in
451
+ . delegateReceive ( . timeout, to: self ) { _, _ in
435
452
callback ? ( . timedOut, nil )
436
453
}
437
454
@@ -646,7 +663,7 @@ public class RealtimeChannel {
646
663
/// "event" and nothing is printed if the channel receives "other_event".
647
664
///
648
665
/// - parameter event: Event to unsubscribe from
649
- /// - paramter ref: Ref counter returned when subscribing. Can be omitted
666
+ /// - parameter ref: Ref counter returned when subscribing. Can be omitted
650
667
public func off( _ type: String , filter: [ String : String ] = [ : ] ) {
651
668
bindings. withValue {
652
669
$0 [ type. lowercased ( ) ] = $0 [ type. lowercased ( ) , default: [ ] ] . filter { bind in
@@ -716,7 +733,7 @@ public class RealtimeChannel {
716
733
" topic " : subTopic,
717
734
" payload " : payload,
718
735
" event " : event as Any ,
719
- ]
736
+ ] ,
720
737
]
721
738
722
739
do {
@@ -731,7 +748,7 @@ public class RealtimeChannel {
731
748
guard let httpResponse = response as? HTTPURLResponse else {
732
749
return . error
733
750
}
734
- if 200 ..< 300 ~= httpResponse. statusCode {
751
+ if 200 ..< 300 ~= httpResponse. statusCode {
735
752
return . ok
736
753
}
737
754
@@ -747,8 +764,8 @@ public class RealtimeChannel {
747
764
)
748
765
749
766
if let type = payload [ " type " ] as? String , type == " broadcast " ,
750
- let config = self . params [ " config " ] as? [ String : Any ] ,
751
- let broadcast = config [ " broadcast " ] as? [ String : Any ]
767
+ let config = self . params [ " config " ] as? [ String : Any ] ,
768
+ let broadcast = config [ " broadcast " ] as? [ String : Any ]
752
769
{
753
770
let ack = broadcast [ " ack " ] as? Bool
754
771
if ack == nil || ack == false {
@@ -758,10 +775,10 @@ public class RealtimeChannel {
758
775
}
759
776
760
777
push
761
- . receive ( " ok " ) { _ in
778
+ . receive ( . ok ) { _ in
762
779
continuation. resume ( returning: . ok)
763
780
}
764
- . receive ( " timeout " ) { _ in
781
+ . receive ( . timeout) { _ in
765
782
continuation. resume ( returning: . timedOut)
766
783
}
767
784
}
@@ -811,12 +828,14 @@ public class RealtimeChannel {
811
828
// Perform the same behavior if successfully left the channel
812
829
// or if sending the event timed out
813
830
leavePush
814
- . receive ( " ok " , delegated: onCloseDelegate)
815
- . receive ( " timeout " , delegated: onCloseDelegate)
831
+ . receive ( . ok , delegated: onCloseDelegate)
832
+ . receive ( . timeout, delegated: onCloseDelegate)
816
833
leavePush. send ( )
817
834
818
835
// If the RealtimeChannel cannot send push events, trigger a success locally
819
- if !canPush { leavePush. trigger ( " ok " , payload: [ : ] ) }
836
+ if !canPush {
837
+ leavePush. trigger ( . ok, payload: [ : ] )
838
+ }
820
839
821
840
// Return the push so it can be bound to
822
841
return leavePush
@@ -908,10 +927,12 @@ public class RealtimeChannel {
908
927
if let bindId = bind. id. flatMap ( Int . init) {
909
928
let ids = message. payload [ " ids " , as: [ Int ] . self] ?? [ ]
910
929
return ids. contains ( bindId)
911
- && ( bindEvent == " * "
912
- || bindEvent
930
+ && (
931
+ bindEvent == " * "
932
+ || bindEvent
913
933
== message. payload [ " data " , as: [ String : Any ] . self] ? [ " type " , as: String . self] ?
914
- . lowercased ( ) )
934
+ . lowercased ( )
935
+ )
915
936
}
916
937
917
938
return bindEvent == " * "
0 commit comments