Skip to content

refactor: realtime #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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