Skip to content

Commit ae60677

Browse files
authored
Merge pull request #1 from fabianfett/feature/psubscribe
Support psubscribe
2 parents 6ff4ffb + 707b74a commit ae60677

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

Sources/Redis/RedisClient.swift

+50-6
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@ open class RedisClient : RedisCommandTarget {
206206
var subscribedChannels = Set<String>()
207207
var subscribedPatterns = Set<String>()
208208

209-
private var subscribeListeners = EventLoopEventListenerSet<(String, Int)>()
210-
private var messageListeners = EventLoopEventListenerSet<(String, String)>()
209+
private var subscribeListeners = EventLoopEventListenerSet<(String, Int)>()
210+
private var psubscribeListeners = EventLoopEventListenerSet<(String, Int)>()
211+
private var messageListeners =
212+
EventLoopEventListenerSet<(String, String, String?)>()
211213

212214
open func subscribe(_ channels: String...) {
213215
_subscribe(channels: channels)
@@ -282,7 +284,7 @@ open class RedisClient : RedisCommandTarget {
282284

283285
let newPatterns = Set(patterns).subtracting(subscribedPatterns)
284286
if !newPatterns.isEmpty {
285-
subscribedPatterns.formUnion(newChannels)
287+
subscribedPatterns.formUnion(newPatterns)
286288

287289
if state.isConnected {
288290
let call = RedisCommandCall([ "PSUBSCRIBE" ] + newPatterns,
@@ -308,7 +310,35 @@ open class RedisClient : RedisCommandTarget {
308310
}
309311

310312
@discardableResult
313+
open func onPSubscribe(_ cb: @escaping ( String, Int ) -> Void) -> Self {
314+
if eventLoop.inEventLoop {
315+
psubscribeListeners.append(cb)
316+
}
317+
else {
318+
eventLoop.execute {
319+
self.psubscribeListeners.append(cb)
320+
}
321+
}
322+
return self
323+
}
324+
325+
326+
@discardableResult
327+
/// Executes the callback when the pub/sub system receives a message.
328+
/// The callback parameters are channel and message.
311329
open func onMessage(_ cb: @escaping ( String, String ) -> Void) -> Self {
330+
return onMessage { (channel, message, _) in
331+
cb(channel, message)
332+
}
333+
}
334+
335+
/// Executes the callback when the pub/sub system receives a message.
336+
/// The callback parameters are channel, message and the pattern in case
337+
/// of a pattern subscription.
338+
@discardableResult
339+
open func onMessage(_ cb: @escaping ( String, String, String? )
340+
-> Void) -> Self
341+
{
312342
if eventLoop.inEventLoop {
313343
messageListeners.append(cb)
314344
}
@@ -423,13 +453,27 @@ open class RedisClient : RedisCommandTarget {
423453
if let channel = channel.stringValue,
424454
let message = message.stringValue
425455
{
426-
return messageListeners.emit( ( channel, message ) )
456+
return messageListeners.emit( ( channel, message, nil ) )
457+
}
458+
case ( "pmessage", let pattern, let channel ):
459+
if items.count > 3,
460+
let pattern = pattern.stringValue,
461+
let channel = channel.stringValue,
462+
let message = items[3].stringValue
463+
{
464+
return messageListeners.emit( ( channel, message, pattern ) )
427465
}
428-
case ( "subscribe", let channel, let count ):
466+
case ( "subscribe" , let channel, let count ),
467+
( "psubscribe", let channel, let count ):
429468
if let channel = channel.stringValue, let count = count.intValue {
430-
return subscribeListeners.emit( (channel, count ) )
469+
return subscribeListeners.emit( ( channel, count ) )
470+
}
471+
case ( "psubscribe", let pattern, let count ):
472+
if let pattern = pattern.stringValue, let count = count.intValue {
473+
return psubscribeListeners.emit( ( pattern, count ) )
431474
}
432475
case ( "unsubscribe", _, _ ): return
476+
case ( "punsubscribe", _, _ ): return
433477
default: break
434478
}
435479
}

0 commit comments

Comments
 (0)