Skip to content
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

Use updated version of later and websocket with auto-running child event loop #24

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: chromote
Title: Headless Chrome Web Browser Interface
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R: c(
person("Winston", "Chang", role = c("aut", "cre"), email = "[email protected]"),
person(given = "RStudio", role = c("cph", "fnd"))
Expand All @@ -11,10 +11,10 @@ Encoding: UTF-8
LazyData: true
Imports:
jsonlite,
websocket,
websocket (>= 1.1.0.9002),
processx,
R6,
later (>= 0.8.0.9003),
later (>= 0.8.0.9004),
promises (>= 1.0.1.9002),
magrittr,
rlang,
Expand All @@ -23,6 +23,7 @@ Suggests:
showimage
Remotes:
r-lib/later,
rstudio/websocket,
rstudio/promises
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.2
43 changes: 3 additions & 40 deletions R/chromote.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ Chromote <- R6Class(

private$command_callbacks <- fastmap()

private$parent_loop <- current_loop()

# Use a private event loop to drive the websocket
private$child_loop <- create_loop(autorun = FALSE)
private$child_loop <- create_loop(parent = current_loop())

with_loop(private$child_loop, {
private$ws <- WebSocket$new(
Expand Down Expand Up @@ -76,7 +74,6 @@ Chromote <- R6Class(
private$event_manager <- EventManager$new(self)
private$is_active_ <- TRUE

private$schedule_child_loop()
self$wait_for(p)

private$register_default_event_listeners()
Expand Down Expand Up @@ -349,43 +346,9 @@ Chromote <- R6Class(
sessions = list(),

# =========================================================================
# Event loop for the websocket and the parent event loop
# Private event loop for the websocket
# =========================================================================
child_loop = NULL,
parent_loop = NULL,
child_loop_is_scheduled = FALSE,

schedule_child_loop = function() {
# Make sure that if this function is called multiple times, there aren't
# multiple streams of overlapping callbacks.
if (private$child_loop_is_scheduled)
return()

# If the websocket has closed, there's no reason to run the child loop
# anymore.
if (private$ws$readyState() == 3) {
self$debug_log("Websocket state is closed.")
private$is_active_ <- FALSE
return()
}

# This tells the parent loop to schedule one run of the child
# (private) loop.
later(private$run_child_loop, 0.01, loop = private$parent_loop)

private$child_loop_is_scheduled <- TRUE
},

run_child_loop = function() {
private$child_loop_is_scheduled <- FALSE

tryCatch(
run_now(loop = private$child_loop),
finally = {
private$schedule_child_loop()
}
)
}
child_loop = NULL
)
)

Expand Down