Skip to content

Commit

Permalink
refactor: realtime (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Nov 3, 2023
1 parent 0b9c0ff commit 4ae8d20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
24 changes: 12 additions & 12 deletions Sources/Realtime/RealtimeChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ public class RealtimeChannel {
/// - return: Push event
@discardableResult
public func subscribe(
callback: ((RealtimeSubscribeStates, Error?) -> Void)? = nil,
timeout: TimeInterval? = nil
timeout: TimeInterval? = nil,
callback: ((RealtimeSubscribeStates, Error?) -> Void)? = nil
) -> RealtimeChannel {
guard !joinedOnce else {
fatalError(
Expand Down Expand Up @@ -460,7 +460,7 @@ public class RealtimeChannel {
presence.state
}

public func track(payload: Payload, opts: Payload = [:]) async -> ChannelResponse {
public func track(_ payload: Payload, opts: Payload = [:]) async -> ChannelResponse {
await send(
type: .presence,
payload: [
Expand Down Expand Up @@ -489,11 +489,11 @@ public class RealtimeChannel {
/// self?.print("RealtimeChannel \(message.topic) has closed"
/// }
///
/// - parameter callback: Called when the RealtimeChannel closes
/// - parameter handler: Called when the RealtimeChannel closes
/// - return: Ref counter of the subscription. See `func off()`
@discardableResult
public func onClose(_ callback: @escaping ((Message) -> Void)) -> RealtimeChannel {
on(ChannelEvent.close, filter: ChannelFilter(), callback: callback)
public func onClose(_ handler: @escaping ((Message) -> Void)) -> RealtimeChannel {
on(ChannelEvent.close, filter: ChannelFilter(), handler: handler)
}

/// Hook into when the RealtimeChannel is closed. Automatically handles retain
Expand Down Expand Up @@ -530,11 +530,11 @@ public class RealtimeChannel {
/// self?.print("RealtimeChannel \(message.topic) has errored"
/// }
///
/// - parameter callback: Called when the RealtimeChannel closes
/// - parameter handler: Called when the RealtimeChannel closes
/// - return: Ref counter of the subscription. See `func off()`
@discardableResult
public func onError(_ callback: @escaping ((_ message: Message) -> Void)) -> RealtimeChannel {
on(ChannelEvent.error, filter: ChannelFilter(), callback: callback)
public func onError(_ handler: @escaping ((_ message: Message) -> Void)) -> RealtimeChannel {
on(ChannelEvent.error, filter: ChannelFilter(), handler: handler)
}

/// Hook into when the RealtimeChannel receives an Error. Automatically handles
Expand Down Expand Up @@ -581,16 +581,16 @@ public class RealtimeChannel {
/// stuff" will keep on printing on the "event"
///
/// - parameter event: Event to receive
/// - parameter callback: Called with the event's message
/// - parameter handler: Called with the event's message
/// - return: Ref counter of the subscription. See `func off()`
@discardableResult
public func on(
_ event: String,
filter: ChannelFilter,
callback: @escaping ((Message) -> Void)
handler: @escaping ((Message) -> Void)
) -> RealtimeChannel {
var delegated = Delegated<Message, Void>()
delegated.manuallyDelegate(with: callback)
delegated.manuallyDelegate(with: handler)

return on(event, filter: filter, delegated: delegated)
}
Expand Down
23 changes: 13 additions & 10 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -655,19 +655,22 @@ public class RealtimeClient: PhoenixTransportDelegate {
return channel
}

/// Removes the Channel from the socket. This does not cause the channel to
/// inform the server that it is leaving. You should call channel.leave()
/// prior to removing the Channel.
///
/// Example:
///
/// channel.leave()
/// socket.remove(channel)
///
/// - parameter channel: Channel to remove
/// Unsubscribes and removes a single channel
public func remove(_ channel: RealtimeChannel) {
channel.unsubscribe()
off(channel.stateChangeRefs)
channels.removeAll(where: { $0.joinRef == channel.joinRef })

if channels.isEmpty {
disconnect()
}
}

/// Unsubscribes and removes all channels
public func removeAllChannels() {
for channel in channels {
remove(channel)
}
}

/// Removes `onOpen`, `onClose`, `onError,` and `onMessage` registrations.
Expand Down

0 comments on commit 4ae8d20

Please sign in to comment.