-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make watcher more simpler and more self-contained #149
Changes from all commits
64c114c
5483021
fd304fb
52935ed
6d6da50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,53 +2,56 @@ | |
#' | ||
#' @param debug activate debug mode where output will be both printed to | ||
#' screen and captured. | ||
#' @param handler An ouptut handler object. | ||
#' @param frame When this frame terminates, the watcher will automatically close.` | ||
#' @return list containing four functions: `get_new`, `pause`, | ||
#' `unpause`, `close`. | ||
#' @keywords internal | ||
watchout <- function(debug = FALSE) { | ||
output <- character() | ||
prev <- character() | ||
|
||
con <- textConnection("output", "wr", local = TRUE) | ||
watchout <- function(handler = new_output_handler(), | ||
debug = FALSE, | ||
frame = parent.frame()) { | ||
con <- file("", "w+b") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
defer(frame = frame, { | ||
if (!test_con(con, isOpen)) { | ||
con_error('The connection has been closed') | ||
} | ||
sink() | ||
close(con) | ||
}) | ||
sink(con, split = debug) | ||
|
||
list( | ||
get_new = function(plot = FALSE, incomplete_plots = FALSE, | ||
text_callback = identity, graphics_callback = identity) { | ||
incomplete <- test_con(con, isIncomplete) | ||
if (incomplete) cat("\n") | ||
|
||
out <- list() | ||
|
||
if (plot) { | ||
out$graphics <- plot_snapshot(incomplete_plots) | ||
if (!is.null(out$graphics)) graphics_callback(out$graphics) | ||
} | ||
|
||
n0 <- length(prev) | ||
n1 <- length(output) | ||
if (n1 > n0) { | ||
new <- output[n0 + seq_len(n1 - n0)] | ||
prev <<- output | ||
|
||
out$text <- paste0(new, collapse = "\n") | ||
if (!incomplete) out$text <- paste0(out$text, "\n") | ||
|
||
text_callback(out$text) | ||
} | ||
# try() defaults to using stderr() so we need to explicitly override(#88) | ||
old <- options(try.outFile = con) | ||
defer(options(old), frame = frame) | ||
|
||
function(plot = TRUE, incomplete_plots = FALSE) { | ||
out <- list( | ||
if (plot) plot_snapshot(incomplete_plots), | ||
read_con(con) | ||
) | ||
if (!is.null(out[[1]])) { | ||
handler$graphics(out[[1]]) | ||
} | ||
if (!is.null(out[[2]])) { | ||
handler$text(out[[2]]) | ||
} | ||
|
||
compact(out) | ||
} | ||
} | ||
|
||
unname(out) | ||
}, | ||
pause = function() sink(), | ||
unpause = function() sink(con, split = debug), | ||
close = function() { | ||
if (!test_con(con, isOpen)) con_error('The connection has been closed') | ||
sink() | ||
close(con) | ||
output | ||
}, | ||
get_con = function() con | ||
) | ||
read_con <- function(con, buffer = 32 * 1024) { | ||
bytes <- raw() | ||
repeat { | ||
new <- readBin(con, "raw", n = buffer) | ||
if (length(new) == 0) break | ||
bytes <- c(bytes, new) | ||
} | ||
if (length(bytes) == 0) { | ||
NULL | ||
} else { | ||
rawToChar(bytes) | ||
} | ||
} | ||
|
||
test_con = function(con, test) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically a breaking change, but (unsurprisingly) I don't see any evidence that anyone has ever used it. It might be worth un-exporting in the next version to make it very clear that it's for internal use only.