Skip to content

Commit 1c471df

Browse files
committed
add context method for nano objects
1 parent 8e4bbb8 commit 1c471df

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### New Features
44

5+
* `$context()` method added for creating new contexts from nano Objects with supported protocols (i.e. req, rep, surveyor, respondent).
56
* Added convenience auxiliary functions `is_nano()` and `is_aio()`.
67

78
#### Updates

R/nano.R

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,25 @@ nano <- function(protocol = c("pair", "bus", "req", "rep", "push", "pull",
128128
opt = opt,
129129
value = value)
130130

131-
if (protocol == "sub") {
132-
nano[["subscribe"]] <- function(topic = NULL) subscribe(socket,
133-
topic = topic)
134-
nano[["unsubscribe"]] <- function(topic = NULL) unsubscribe(socket,
135-
topic = topic)
136-
}
137-
if (protocol == "surveyor") {
138-
nano[["survey_time"]] <- function(time) survey_time(socket, time = time)
139-
}
131+
switch(protocol,
132+
req =,
133+
rep = {
134+
nano[["context"]] <- function() context(socket)
135+
},
136+
sub = {
137+
nano[["subscribe"]] <- function(topic = NULL) subscribe(socket,
138+
topic = topic)
139+
nano[["unsubscribe"]] <- function(topic = NULL) unsubscribe(socket,
140+
topic = topic)
141+
},
142+
surveyor = {
143+
nano[["context"]] <- function() context(socket)
144+
nano[["survey_time"]] <- function(time) survey_time(socket, time = time)
145+
},
146+
respondent = {
147+
nano[["context"]] <- function() context(socket)
148+
},
149+
NULL)
140150

141151
nano
142152

tests/tests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ nanotest(n1$socket[state] == "closed")
7575
n <- nano("req", listen = "inproc://testing")
7676
n1 <- nano("rep", dial = "inproc://testing")
7777
nanotest(n$socket_setopt("ms", "req:resend-time", 1000) == 0)
78-
ctx <- context(n$socket)
78+
ctx <- n$context()
7979
ctx1 <- context(n1$socket)
8080
nanotest(inherits(ctx, "nanoContext"))
8181
nanotest(inherits(ctx, "nano"))

0 commit comments

Comments
 (0)