/
browser.R
52 lines (46 loc) · 1.42 KB
/
browser.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
globals <- new.env()
#' Browser base class
#'
#' @description
#' Base class for browsers like Chrome, Chromium, etc. Defines the interface
#' used by various browser implementations. It can represent a local browser
#' process or one running remotely.
#'
#' @details
#' The \code{initialize()} method of an implementation should set private$host
#' and private$port. If the process is local, the \code{initialize()} method
#' should also set private$process.
#'
#' @export
Browser <- R6Class("Browser",
public = list(
# Returns TRUE if the browser is running locally, FALSE if it's remote.
#' @description Is local browser?
#' Returns TRUE if the browser is running locally, FALSE if it's remote.
is_local = function() !is.null(private$process),
#' @description Browser process
get_process = function() private$process,
#' @description Is the process alive?
is_alive = function() private$process$is_alive(),
#' @description Browser Host
get_host = function() private$host,
#' @description Browser port
get_port = function() private$port,
#' @description Close the browser
close = function() {
if (self$is_local() && private$process$is_alive()) {
private$process$signal(tools::SIGTERM)
}
}
),
private = list(
process = NULL,
host = NULL,
port = NULL,
finalize = function(e) {
if (self$is_local()) {
self$close()
}
}
)
)