@@ -24,7 +24,7 @@ import Swift
2424/// Container class of bindings to the channel
2525struct Binding {
2626 // The event that the Binding is bound to
27- let event : String
27+ let event : ChannelEvent
2828
2929 // The reference number of the Binding
3030 let ref : Int
@@ -57,8 +57,8 @@ struct Binding {
5757import Foundation
5858
5959public class Channel {
60- /// The topic of the Channel. e.g. "rooms: friends"
61- public let topic : String
60+ /// The topic of the Channel. e.g. `.table( "rooms", " friends")`
61+ public let topic : ChannelTopic
6262
6363 /// The params sent when joining the channel
6464 public var params : [ String : Any ] {
@@ -100,7 +100,7 @@ public class Channel {
100100 /// - parameter topic: Topic of the Channel
101101 /// - parameter params: Optional. Parameters to send when joining.
102102 /// - parameter socket: Socket that the channel is a part of
103- init ( topic: String , params: [ String : Any ] = [ : ] , socket: RealtimeClient ) {
103+ init ( topic: ChannelTopic , params: [ String : Any ] = [ : ] , socket: RealtimeClient ) {
104104 state = ChannelState . closed
105105 self . topic = topic
106106 self . params = params
@@ -217,7 +217,7 @@ public class Channel {
217217 // Perform when the join reply is received
218218 delegateOn ( ChannelEvent . reply, to: self ) { ( self , message) in
219219 // Trigger bindings
220- self . trigger ( event: self . replyEventName ( message. ref) ,
220+ self . trigger ( event: ChannelEvent . channelReply ( message. ref) ,
221221 payload: message. payload,
222222 ref: message. ref,
223223 joinRef: message. joinRef)
@@ -340,13 +340,13 @@ public class Channel {
340340 /// Example:
341341 ///
342342 /// let channel = socket.channel("topic")
343- /// let ref1 = channel.on("event" ) { [weak self] (message) in
343+ /// let ref1 = channel.on(.all ) { [weak self] (message) in
344344 /// self?.print("do stuff")
345345 /// }
346- /// let ref2 = channel.on("event" ) { [weak self] (message) in
346+ /// let ref2 = channel.on(.all ) { [weak self] (message) in
347347 /// self?.print("do other stuff")
348348 /// }
349- /// channel.off("event" , ref1)
349+ /// channel.off(.all , ref1)
350350 ///
351351 /// Since unsubscription of ref1, "do stuff" won't print, but "do other
352352 /// stuff" will keep on printing on the "event"
@@ -355,7 +355,7 @@ public class Channel {
355355 /// - parameter callback: Called with the event's message
356356 /// - return: Ref counter of the subscription. See `func off()`
357357 @discardableResult
358- public func on( _ event: String , callback: @escaping ( ( Message ) -> Void ) ) -> Int {
358+ public func on( _ event: ChannelEvent , callback: @escaping ( ( Message ) -> Void ) ) -> Int {
359359 var delegated = Delegated < Message , Void > ( )
360360 delegated. manuallyDelegate ( with: callback)
361361
@@ -371,23 +371,23 @@ public class Channel {
371371 /// Example:
372372 ///
373373 /// let channel = socket.channel("topic")
374- /// let ref1 = channel.delegateOn("event" , to: self) { (self, message) in
374+ /// let ref1 = channel.delegateOn(.all , to: self) { (self, message) in
375375 /// self?.print("do stuff")
376376 /// }
377- /// let ref2 = channel.delegateOn("event" , to: self) { (self, message) in
377+ /// let ref2 = channel.delegateOn(.all , to: self) { (self, message) in
378378 /// self?.print("do other stuff")
379379 /// }
380- /// channel.off("event" , ref1)
380+ /// channel.off(.all , ref1)
381381 ///
382382 /// Since unsubscription of ref1, "do stuff" won't print, but "do other
383- /// stuff" will keep on printing on the "event"
383+ /// stuff" will keep on printing on all "event" (*).
384384 ///
385385 /// - parameter event: Event to receive
386386 /// - parameter owner: Class registering the callback. Usually `self`
387387 /// - parameter callback: Called with the event's message
388388 /// - return: Ref counter of the subscription. See `func off()`
389389 @discardableResult
390- public func delegateOn< Target: AnyObject > ( _ event: String ,
390+ public func delegateOn< Target: AnyObject > ( _ event: ChannelEvent ,
391391 to owner: Target ,
392392 callback: @escaping ( ( Target , Message ) -> Void ) ) -> Int
393393 {
@@ -399,7 +399,7 @@ public class Channel {
399399
400400 /// Shared method between `on` and `manualOn`
401401 @discardableResult
402- private func on( _ event: String , delegated: Delegated < Message , Void > ) -> Int {
402+ private func on( _ event: ChannelEvent , delegated: Delegated < Message , Void > ) -> Int {
403403 let ref = bindingRef
404404 bindingRef = ref + 1
405405
@@ -414,19 +414,19 @@ public class Channel {
414414 /// Example:
415415 ///
416416 /// let channel = socket.channel("topic")
417- /// let ref1 = channel.on("event" ) { _ in print("ref1 event" }
418- /// let ref2 = channel.on("event" ) { _ in print("ref2 event" }
419- /// let ref3 = channel.on("other_event" ) { _ in print("ref3 other" }
420- /// let ref4 = channel.on("other_event" ) { _ in print("ref4 other" }
421- /// channel.off("event" , ref1)
422- /// channel.off("other_event" )
417+ /// let ref1 = channel.on(.insert ) { _ in print("ref1 event" }
418+ /// let ref2 = channel.on(.insert ) { _ in print("ref2 event" }
419+ /// let ref3 = channel.on(.update ) { _ in print("ref3 other" }
420+ /// let ref4 = channel.on(.update ) { _ in print("ref4 other" }
421+ /// channel.off(.insert , ref1)
422+ /// channel.off(.update )
423423 ///
424424 /// After this, only "ref2 event" will be printed if the channel receives
425- /// "event " and nothing is printed if the channel receives "other_event ".
425+ /// "insert " and nothing is printed if the channel receives "update ".
426426 ///
427427 /// - parameter event: Event to unsubscribe from
428428 /// - paramter ref: Ref counter returned when subscribing. Can be omitted
429- public func off( _ event: String , ref: Int ? = nil ) {
429+ public func off( _ event: ChannelEvent , ref: Int ? = nil ) {
430430 bindingsDel. removeAll { ( bind) -> Bool in
431431 bind. event == event && ( ref == nil || ref == bind. ref)
432432 }
@@ -437,14 +437,14 @@ public class Channel {
437437 /// Example:
438438 ///
439439 /// channel
440- /// .push("event" , payload: ["message": "hello")
440+ /// .push(.update , payload: ["message": "hello")
441441 /// .receive("ok") { _ in { print("message sent") }
442442 ///
443443 /// - parameter event: Event to push
444444 /// - parameter payload: Payload to push
445445 /// - parameter timeout: Optional timeout
446446 @discardableResult
447- public func push( _ event: String ,
447+ public func push( _ event: ChannelEvent ,
448448 payload: [ String : Any ] ,
449449 timeout: TimeInterval = Defaults . timeoutInterval) -> Push
450450 {
@@ -476,7 +476,7 @@ public class Channel {
476476 ///
477477 /// Example:
478478 ////
479- /// channel.leave ().receive("ok") { _ in { print("left") }
479+ /// channel.unsubscribe ().receive("ok") { _ in { print("left") }
480480 ///
481481 /// - parameter timeout: Optional timeout
482482 /// - return: Push that can add receive hooks
@@ -578,13 +578,13 @@ public class Channel {
578578 }
579579
580580 /// Triggers an event to the correct event bindings created by
581- //// `channel.on(" event" )`.
581+ //// `channel.on(event)`.
582582 ///
583583 /// - parameter event: Event to trigger
584584 /// - parameter payload: Payload of the event
585585 /// - parameter ref: Ref of the event. Defaults to empty
586586 /// - parameter joinRef: Ref of the join event. Defaults to nil
587- func trigger( event: String ,
587+ func trigger( event: ChannelEvent ,
588588 payload: [ String : Any ] = [ : ] ,
589589 ref: String = " " ,
590590 joinRef: String ? = nil )
@@ -597,12 +597,6 @@ public class Channel {
597597 trigger ( message)
598598 }
599599
600- /// - parameter ref: The ref of the event push
601- /// - return: The event name of the reply
602- func replyEventName( _ ref: String ) -> String {
603- return " chan_reply_ \( ref) "
604- }
605-
606600 /// The Ref send during the join message.
607601 var joinRef : String ? {
608602 return joinPush. ref
0 commit comments