20
20
21
21
import Foundation
22
22
23
- /// Represnts pushing data to a `Channel` through the `Socket`
23
+ /// Represents pushing data to a `Channel` through the `Socket`
24
24
public class Push {
25
25
/// The channel sending the Push
26
26
public weak var channel : RealtimeChannel ?
@@ -44,7 +44,7 @@ public class Push {
44
44
var timeoutWorkItem : DispatchWorkItem ?
45
45
46
46
/// Hooks into a Push. Where .receive("ok", callback(Payload)) are stored
47
- var receiveHooks : [ PushStatus : [ Delegated < Message , Void > ] ]
47
+ var receiveHooks : [ PushStatus : [ @ Sendable ( Message ) -> Void ] ]
48
48
49
49
/// True if the Push has been sent
50
50
var sent : Bool
@@ -121,61 +121,25 @@ public class Push {
121
121
@discardableResult
122
122
public func receive(
123
123
_ status: PushStatus ,
124
- callback: @escaping ( ( Message ) -> Void )
124
+ callback: @escaping @ Sendable ( Message ) -> Void
125
125
) -> Push {
126
- var delegated = Delegated < Message , Void > ( )
127
- delegated. manuallyDelegate ( with: callback)
128
-
129
- return receive ( status, delegated: delegated)
130
- }
131
-
132
- /// Receive a specific event when sending an Outbound message. Automatically
133
- /// prevents retain cycles. See `manualReceive(status:, callback:)` if you
134
- /// want to handle this yourself.
135
- ///
136
- /// Example:
137
- ///
138
- /// channel
139
- /// .send(event:"custom", payload: ["body": "example"])
140
- /// .delegateReceive("error", to: self) { payload in
141
- /// print("Error: ", payload)
142
- /// }
143
- ///
144
- /// - parameter status: Status to receive
145
- /// - parameter owner: The class that is calling .receive. Usually `self`
146
- /// - parameter callback: Callback to fire when the status is recevied
147
- @discardableResult
148
- public func delegateReceive< Target: AnyObject > (
149
- _ status: PushStatus ,
150
- to owner: Target ,
151
- callback: @escaping ( ( Target , Message ) -> Void )
152
- ) -> Push {
153
- var delegated = Delegated < Message , Void > ( )
154
- delegated. delegate ( to: owner, with: callback)
155
-
156
- return receive ( status, delegated: delegated)
157
- }
158
-
159
- /// Shared behavior between `receive` calls
160
- @discardableResult
161
- func receive( _ status: PushStatus , delegated: Delegated < Message , Void > ) -> Push {
162
126
// If the message has already been received, pass it to the callback immediately
163
127
if hasReceived ( status: status) , let receivedMessage {
164
- delegated . call ( receivedMessage)
128
+ callback ( receivedMessage)
165
129
}
166
130
167
131
if receiveHooks [ status] == nil {
168
132
/// Create a new array of hooks if no previous hook is associated with status
169
- receiveHooks [ status] = [ delegated ]
133
+ receiveHooks [ status] = [ callback ]
170
134
} else {
171
135
/// A previous hook for this status already exists. Just append the new hook
172
- receiveHooks [ status] ? . append ( delegated )
136
+ receiveHooks [ status] ? . append ( callback )
173
137
}
174
138
175
139
return self
176
140
}
177
141
178
- /// Resets the Push as it was after it was first tnitialized .
142
+ /// Resets the Push as it was after it was first initialised .
179
143
func reset( ) {
180
144
cancelRefEvent ( )
181
145
ref = nil
@@ -189,7 +153,7 @@ public class Push {
189
153
/// - parameter status: Status which was received, e.g. "ok", "error", "timeout"
190
154
/// - parameter response: Response that was received
191
155
private func matchReceive( _ status: PushStatus , message: Message ) {
192
- receiveHooks [ status] ? . forEach { $0. call ( message) }
156
+ receiveHooks [ status] ? . forEach { $0 ( message) }
193
157
}
194
158
195
159
/// Reverses the result on channel.on(ChannelEvent, callback) that spawned the Push
@@ -225,14 +189,14 @@ public class Push {
225
189
226
190
/// If a response is received before the Timer triggers, cancel timer
227
191
/// and match the received event to it's corresponding hook
228
- channel. delegateOn ( refEvent, filter: ChannelFilter ( ) , to : self ) { ( self , message) in
229
- self . cancelRefEvent ( )
230
- self . cancelTimeout ( )
231
- self . receivedMessage = message
192
+ channel. on ( refEvent, filter: ChannelFilter ( ) ) { [ weak self] message in
193
+ self ? . cancelRefEvent ( )
194
+ self ? . cancelTimeout ( )
195
+ self ? . receivedMessage = message
232
196
233
197
/// Check if there is event a status available
234
198
guard let status = message. status else { return }
235
- self . matchReceive ( status, message: message)
199
+ self ? . matchReceive ( status, message: message)
236
200
}
237
201
238
202
/// Setup and start the Timeout timer.
0 commit comments