/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. `redisUnsubscribe` <- function(channels, pattern=FALSE)
  18. {
  19. cmd <- 'UNSUBSCRIBE'
  20. channels <- as.list(channels)
  21. channels <- lapply(channels, charToRaw)
  22. if(pattern){
  23. cmd <- 'PUNSUBSCRIBE'
  24. if(length(channels)>1) warning("Pattern subscription with multiple arguments")
  25. }
  26. x <- do.call('.redisCmd', c(list(.raw(cmd)),channels))
  27. len <- length(channels) - 1L
  28. if(len > 0L)
  29. x <- c(x, replicate(len, .getResponse(), simplify=FALSE))
  30. x
  31. }
  32. `redisPublish` <- function(channel, message)
  33. {
  34. do.call('.redisCmd', list(.raw('PUBLISH'),.raw(channel),message))
  35. }
  36. # Callback handler
  37. `redisMonitorChannels` <- function()
  38. {
  39. x <- .getResponse()
  40. if(length(x)!=3 && x[[1]] != "message") return(x)
  41. if(exists(x[[2]],mode="function")) {
  42. return(do.call(x[[2]],as.list(x[[3]])))
  43. }
  44. x
  45. }