Skip to content

Commit 7908def

Browse files
jameshfisherhamchapman
authored andcommitted
use singleton pattern
1 parent ed939b3 commit 7908def

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Source/NativePusher.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ import Foundation
1010
Use the Pusher.nativePusher() method to get access to it.
1111
*/
1212
public class NativePusher {
13+
static let sharedInstance = NativePusher()
14+
1315
private static let PLATFORM_TYPE = "apns"
1416
private let CLIENT_API_V1_ENDPOINT = "https://nativepushclient-cluster1.pusher.com/client_api/v1"
1517

1618
private let URLSession = NSURLSession.sharedSession()
1719

1820
// Identifies a Pusher app.
1921
// This app should have push notifications enabled.
20-
private let pusherAppKey: String
22+
private var pusherAppKey: String?
23+
24+
public func setPusherAppKey(pusherAppKey: String) {
25+
self.pusherAppKey = pusherAppKey
26+
tryFlushOutbox()
27+
}
2128

2229
// The id issued to this app instance by Pusher.
2330
// We get it upon registration.
@@ -27,10 +34,8 @@ public class NativePusher {
2734
// Queued actions to perform when the client is registered.
2835
private var outbox: Array<(String, SubscriptionChange)>
2936

30-
// Expected to be initialized by instances of Pusher.
3137
// Normal clients should access the shared instance via Pusher.nativePusher().
32-
public init(pusherAppKey: String) {
33-
self.pusherAppKey = pusherAppKey
38+
public init() {
3439
clientId = nil
3540
outbox = []
3641
}
@@ -114,15 +119,19 @@ public class NativePusher {
114119
}
115120

116121
private func tryFlushOutbox() {
117-
if (self.clientId != nil && 0 < outbox.count) {
118-
let (interest,change) = outbox.removeAtIndex(0)
119-
modifySubscription(interest, change: change) {
120-
self.tryFlushOutbox()
122+
switch (self.pusherAppKey, self.clientId) {
123+
case (.Some(let pusherAppKey), .Some(let clientId)):
124+
if (self.pusherAppKey != nil && self.clientId != nil && 0 < outbox.count) {
125+
let (interest,change) = outbox.removeAtIndex(0)
126+
modifySubscription(pusherAppKey, clientId: clientId, interest: interest, change: change) {
127+
self.tryFlushOutbox()
128+
}
121129
}
130+
case _: break
122131
}
123132
}
124133

125-
private func modifySubscription(interest: String, change: SubscriptionChange, callback: (Void)->(Void)) {
134+
private func modifySubscription(pusherAppKey:String, clientId: String, interest: String, change: SubscriptionChange, callback: (Void)->(Void)) {
126135
let url = "\(CLIENT_API_V1_ENDPOINT)/clients/\(clientId)/interests/\(interest)"
127136
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
128137
switch (change) {
@@ -133,7 +142,7 @@ public class NativePusher {
133142
}
134143

135144
let params: [String: AnyObject] = [
136-
"app_key": self.pusherAppKey,
145+
"app_key": pusherAppKey,
137146
]
138147

139148
try! request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: [])

Source/PusherSwift.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public class Pusher {
1515
public let connection: PusherConnection
1616
private let key: String
1717

18-
private class var sharedNativePusher : NativePusher?
19-
2018
/**
2119
Initializes the Pusher client with an app key and any appropriate options.
2220

@@ -31,8 +29,7 @@ public class Pusher {
3129
let ws = WebSocket(url: NSURL(string: urlString)!)
3230
connection = PusherConnection(key: key, socket: ws, url: urlString, options: options)
3331
connection.createGlobalChannel()
34-
35-
sharedNativePusher = NativePusher(key)
32+
NativePusher.sharedInstance.setPusherAppKey(key)
3633
}
3734

3835
/**
@@ -103,8 +100,8 @@ public class Pusher {
103100
self.connection.connect()
104101
}
105102

106-
public func nativePusher() {
107-
return sharedNativePusher
103+
public func nativePusher() -> NativePusher {
104+
return NativePusher.sharedInstance
108105
}
109106
}
110107

0 commit comments

Comments
 (0)