Skip to content

Commit 6cec943

Browse files
committed
Format Realtime package
1 parent 7986ccb commit 6cec943

13 files changed

+497
-695
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ let package = Package(
2121
.library(name: "Storage", targets: ["Storage"]),
2222
.library(
2323
name: "Supabase",
24-
targets: ["Supabase", "Functions", "PostgREST", "GoTrue", "Realtime", "Storage"]),
24+
targets: ["Supabase", "Functions", "PostgREST", "GoTrue", "Realtime", "Storage"]
25+
),
2526
],
2627
dependencies: [
2728
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),

Sources/Realtime/Defaults.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,36 @@
2121
import Foundation
2222

2323
/// A collection of default values and behaviors used across the Client
24-
public class Defaults {
25-
24+
public enum Defaults {
2625
/// Default timeout when sending messages
2726
public static let timeoutInterval: TimeInterval = 10.0
2827

2928
/// Default interval to send heartbeats on
3029
public static let heartbeatInterval: TimeInterval = 30.0
3130

32-
/// Default maximum amount of time which the system may delay heartbeat events in order to minimize power usage
31+
/// Default maximum amount of time which the system may delay heartbeat events in order to
32+
/// minimize power usage
3333
public static let heartbeatLeeway: DispatchTimeInterval = .milliseconds(10)
3434

3535
/// Default reconnect algorithm for the socket
3636
public static let reconnectSteppedBackOff: (Int) -> TimeInterval = { tries in
37-
return tries > 9 ? 5.0 : [0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.5, 1.0, 2.0][tries - 1]
37+
tries > 9 ? 5.0 : [0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.5, 1.0, 2.0][tries - 1]
3838
}
3939

4040
/** Default rejoin algorithm for individual channels */
4141
public static let rejoinSteppedBackOff: (Int) -> TimeInterval = { tries in
42-
return tries > 3 ? 10 : [1, 2, 5][tries - 1]
42+
tries > 3 ? 10 : [1, 2, 5][tries - 1]
4343
}
4444

4545
public static let vsn = "2.0.0"
4646

4747
/// Default encode function, utilizing JSONSerialization.data
4848
public static let encode: (Any) -> Data = { json in
49-
return
50-
try! JSONSerialization
49+
try! JSONSerialization
5150
.data(
5251
withJSONObject: json,
53-
options: JSONSerialization.WritingOptions())
52+
options: JSONSerialization.WritingOptions()
53+
)
5454
}
5555

5656
/// Default decode function, utilizing JSONSerialization.jsonObject
@@ -60,13 +60,15 @@ public class Defaults {
6060
try? JSONSerialization
6161
.jsonObject(
6262
with: data,
63-
options: JSONSerialization.ReadingOptions())
63+
options: JSONSerialization.ReadingOptions()
64+
)
6465
else { return nil }
6566
return json
6667
}
6768

68-
public static let heartbeatQueue: DispatchQueue = DispatchQueue(
69-
label: "com.phoenix.socket.heartbeat")
69+
public static let heartbeatQueue: DispatchQueue = .init(
70+
label: "com.phoenix.socket.heartbeat"
71+
)
7072
}
7173

7274
/// Represents the multiple states that a Channel can be in
@@ -81,7 +83,7 @@ public enum ChannelState: String {
8183

8284
/// Represents the different events that can be sent through
8385
/// a channel regarding a Channel's lifecycle.
84-
public struct ChannelEvent {
86+
public enum ChannelEvent {
8587
public static let heartbeat = "heartbeat"
8688
public static let join = "phx_join"
8789
public static let leave = "phx_leave"

Sources/Realtime/Delegated.swift

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
/// instead of added as a dependency to reduce the number of packages that
2424
/// ship with SwiftPhoenixClient
2525
public struct Delegated<Input, Output> {
26-
2726
private(set) var callback: ((Input) -> Output?)?
2827

2928
public init() {}
@@ -33,31 +32,29 @@ public struct Delegated<Input, Output> {
3332
with callback: @escaping (Target, Input) -> Output
3433
) {
3534
self.callback = { [weak target] input in
36-
guard let target = target else {
35+
guard let target else {
3736
return nil
3837
}
3938
return callback(target, input)
4039
}
4140
}
4241

4342
public func call(_ input: Input) -> Output? {
44-
return self.callback?(input)
43+
callback?(input)
4544
}
4645

4746
public var isDelegateSet: Bool {
48-
return callback != nil
47+
callback != nil
4948
}
50-
5149
}
5250

5351
extension Delegated {
54-
5552
public mutating func stronglyDelegate<Target: AnyObject>(
5653
to target: Target,
5754
with callback: @escaping (Target, Input) -> Output
5855
) {
5956
self.callback = { input in
60-
return callback(target, input)
57+
callback(target, input)
6158
}
6259
}
6360

@@ -66,49 +63,40 @@ extension Delegated {
6663
}
6764

6865
public mutating func removeDelegate() {
69-
self.callback = nil
66+
callback = nil
7067
}
71-
7268
}
7369

7470
extension Delegated where Input == Void {
75-
7671
public mutating func delegate<Target: AnyObject>(
7772
to target: Target,
7873
with callback: @escaping (Target) -> Output
7974
) {
80-
self.delegate(to: target, with: { target, voidInput in callback(target) })
75+
delegate(to: target, with: { target, _ in callback(target) })
8176
}
8277

8378
public mutating func stronglyDelegate<Target: AnyObject>(
8479
to target: Target,
8580
with callback: @escaping (Target) -> Output
8681
) {
87-
self.stronglyDelegate(to: target, with: { target, voidInput in callback(target) })
82+
stronglyDelegate(to: target, with: { target, _ in callback(target) })
8883
}
89-
9084
}
9185

9286
extension Delegated where Input == Void {
93-
9487
public func call() -> Output? {
95-
return self.call(())
88+
call(())
9689
}
97-
9890
}
9991

10092
extension Delegated where Output == Void {
101-
10293
public func call(_ input: Input) {
103-
self.callback?(input)
94+
callback?(input)
10495
}
105-
10696
}
10797

10898
extension Delegated where Input == Void, Output == Void {
109-
11099
public func call() {
111-
self.call(())
100+
call(())
112101
}
113-
114102
}

Sources/Realtime/HeartbeatTimer.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import Foundation
2828
*/
2929

3030
class HeartbeatTimer {
31+
// ----------------------------------------------------------------------
3132

32-
//----------------------------------------------------------------------
3333
// MARK: - Dependencies
34-
//----------------------------------------------------------------------
34+
35+
// ----------------------------------------------------------------------
3536
// The interval to wait before firing the Timer
3637
let timeInterval: TimeInterval
3738

@@ -44,13 +45,15 @@ class HeartbeatTimer {
4445
// UUID which specifies the Timer instance. Verifies that timers are different
4546
let uuid: String = UUID().uuidString
4647

47-
//----------------------------------------------------------------------
48+
// ----------------------------------------------------------------------
49+
4850
// MARK: - Properties
49-
//----------------------------------------------------------------------
51+
52+
// ----------------------------------------------------------------------
5053
// The underlying, cancelable, resettable, timer.
51-
private var temporaryTimer: DispatchSourceTimer? = nil
54+
private var temporaryTimer: DispatchSourceTimer?
5255
// The event handler that is called by the timer when it fires.
53-
private var temporaryEventHandler: (() -> Void)? = nil
56+
private var temporaryEventHandler: (() -> Void)?
5457

5558
/**
5659
Create a new HeartbeatTimer
@@ -89,7 +92,8 @@ class HeartbeatTimer {
8992
timer.schedule(
9093
deadline: DispatchTime.now() + self.timeInterval,
9194
repeating: self.timeInterval,
92-
leeway: self.leeway)
95+
leeway: self.leeway
96+
)
9397

9498
// Start the timer
9599
timer.resume()
@@ -111,7 +115,7 @@ class HeartbeatTimer {
111115
True if the Timer exists and has not been cancelled. False otherwise
112116
*/
113117
var isValid: Bool {
114-
guard let timer = self.temporaryTimer else { return false }
118+
guard let timer = temporaryTimer else { return false }
115119
return !timer.isCancelled
116120
}
117121

@@ -121,12 +125,12 @@ class HeartbeatTimer {
121125
*/
122126
func fire() {
123127
guard isValid else { return }
124-
self.temporaryEventHandler?()
128+
temporaryEventHandler?()
125129
}
126130
}
127131

128132
extension HeartbeatTimer: Equatable {
129133
static func == (lhs: HeartbeatTimer, rhs: HeartbeatTimer) -> Bool {
130-
return lhs.uuid == rhs.uuid
134+
lhs.uuid == rhs.uuid
131135
}
132136
}

Sources/Realtime/Message.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ import Foundation
2222

2323
/// Data that is received from the Server.
2424
public class Message {
25-
2625
/// Reference number. Empty if missing
2726
public let ref: String
2827

2928
/// Join Reference number
30-
internal let joinRef: String?
29+
let joinRef: String?
3130

3231
/// Message topic
3332
public let topic: String
@@ -51,7 +50,7 @@ public class Message {
5150
/// message.payload["status"]
5251
/// ```
5352
public var status: String? {
54-
return rawPayload["status"] as? String
53+
rawPayload["status"] as? String
5554
}
5655

5756
init(
@@ -64,23 +63,22 @@ public class Message {
6463
self.ref = ref
6564
self.topic = topic
6665
self.event = event
67-
self.rawPayload = payload
66+
rawPayload = payload
6867
self.joinRef = joinRef
6968
}
7069

7170
init?(json: [Any?]) {
7271
guard json.count > 4 else { return nil }
73-
self.joinRef = json[0] as? String
74-
self.ref = json[1] as? String ?? ""
72+
joinRef = json[0] as? String
73+
ref = json[1] as? String ?? ""
7574

7675
if let topic = json[2] as? String,
7776
let event = json[3] as? String,
7877
let payload = json[4] as? Payload
7978
{
80-
8179
self.topic = topic
8280
self.event = event
83-
self.rawPayload = payload
81+
rawPayload = payload
8482
} else {
8583
return nil
8684
}

0 commit comments

Comments
 (0)