PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/R/pubsub.R

http://github.com/bwlewis/rredis
R | 48 lines | 44 code | 3 blank | 1 comment | 10 complexity | e878db44384bc1a730065e005629ae22 MD5 | raw file
 1`redisSubscribe` <- function(channels, pattern=FALSE)
 2{
 3  cmd <- 'SUBSCRIBE'
 4  channels <- as.list(channels)
 5  channels <- lapply(channels, charToRaw)
 6  if(pattern) {
 7    cmd <- 'PSUBSCRIBE'
 8    if(length(channels)>1)
 9      warning("Pattern subscription with multiple arguments")
10  }
11  x <- do.call('.redisCmd', c(list(.raw(cmd)),channels))
12  len <- length(channels) - 1L
13  if(len > 0L)
14    x <- c(x, replicate(len, .getResponse(), simplify=FALSE))
15  x
16}
17
18`redisUnsubscribe` <- function(channels, pattern=FALSE)
19{
20  cmd <- 'UNSUBSCRIBE'
21  channels <- as.list(channels)
22  channels <- lapply(channels, charToRaw)
23  if(pattern){
24    cmd <- 'PUNSUBSCRIBE'
25    if(length(channels)>1) warning("Pattern subscription with multiple arguments")
26  }
27  x <- do.call('.redisCmd', c(list(.raw(cmd)),channels))
28  len <- length(channels) - 1L
29  if(len > 0L)
30    x <- c(x, replicate(len, .getResponse(), simplify=FALSE))
31  x
32}
33
34`redisPublish` <- function(channel, message)
35{
36  do.call('.redisCmd', list(.raw('PUBLISH'),.raw(channel),message))
37}
38
39# Callback handler
40`redisMonitorChannels` <- function()
41{
42  x <- .getResponse()
43  if(length(x)!=3 && x[[1]] != "message") return(x)
44  if(exists(x[[2]],mode="function")) {
45    return(do.call(x[[2]],as.list(x[[3]])))
46  }
47  x
48}