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

Allow enable() and disable() methods to be used when auto_events is TRUE #156

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# chromote (development version)

* chromote sessions allow you to call `enable()` methods manually event when
`auto_events` is `FALSE`. If an `enable()` method is called, chromote will
not call it again automatically, until you manually `disable()` it (#144).
# chromote 0.2.0

## Breaking changes
Expand Down
8 changes: 8 additions & 0 deletions R/chromote_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,14 @@ ChromoteSession <- R6Class(
register_event_listener = function(event, callback = NULL, timeout = NULL) {
self$check_active()
private$event_manager$register_event_listener(event, callback, timeout)
},

manually_enable = function(domain) {
private$event_manager$manually_enable(domain)
},

manually_disable = function(domain) {
private$event_manager$manually_disable(domain)
}
)
)
Expand Down
24 changes: 21 additions & 3 deletions R/event_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ EventManager <- R6Class("EventManager",
is.function(domain$enable)
})

private$manually_enabled <- rep(FALSE, length(session$protocol))
names(private$manually_enabled) <- names(session$protocol)

private$event_callbacks <- fastmap()
},

Expand Down Expand Up @@ -44,6 +47,15 @@ EventManager <- R6Class("EventManager",
p
},


manually_enable = function(domain) {
private$manually_enabled[[domain]] <- TRUE
},

manually_disable = function(domain) {
private$manually_enabled[[domain]] <- FALSE
},

invoke_event_callbacks = function(event, params) {
callbacks <- private$event_callbacks$get(event)
if (is.null(callbacks) || callbacks$size() == 0)
Expand All @@ -58,11 +70,13 @@ EventManager <- R6Class("EventManager",
private$event_callbacks$remove(event)
}
),

private = list(
# The ChromoteSession or Chromote object that owns this EventManager.
session = NULL,
event_callbacks = NULL,
# Keeps track of whether a domain is manually enabled or not. If it is, we opt out of
# auto events.
manually_enabled = NULL,
# For keeping count of the number of callbacks for each domain; if
# auto_events is TRUE, then when the count goes from 0 to 1 or 1 to 0 for
# a given domain, it will automatically enable or disable events for that
Expand Down Expand Up @@ -124,10 +138,13 @@ EventManager <- R6Class("EventManager",
# method.)
if (private$session$get_auto_events() &&
private$event_callback_counts[[domain]] == 1 &&
isTRUE(private$event_enable_domains[[domain]]))
isTRUE(private$event_enable_domains[[domain]]) &&
!isTRUE(private$manually_enabled[[domain]]))
{
private$session$debug_log("Enabling events for ", domain)
private$session[[domain]]$enable()
# We are not manually enabling this domain, so make sure that this continues to be FALSE.
private$manually_enabled[[domain]] <- FALSE
}

invisible(private$event_callback_counts[[domain]])
Expand All @@ -141,7 +158,8 @@ EventManager <- R6Class("EventManager",
# enable events for this domain.
if (private$session$get_auto_events() &&
private$event_callback_counts[[domain]] == 0 &&
isTRUE(private$event_enable_domains[[domain]]))
isTRUE(private$event_enable_domains[[domain]]) &&
!isTRUE(private$manually_enabled[[domain]]))
{
private$session$debug_log("Disabling events for ", domain)
private$session[[domain]]$disable()
Expand Down
15 changes: 13 additions & 2 deletions R/protocol.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ get_items <- function(domain, type = c("commands", "events")) {
command_to_function <- function(command, domain_name, env) {
new_function(
args = gen_command_args(command$parameters),
body = gen_command_body(paste0(domain_name, ".", command$name), command$parameters),
body = gen_command_body(domain_name, command$name, command$parameters),
env = env
)
# TODO:
Expand Down Expand Up @@ -79,7 +79,8 @@ gen_command_args <- function(params) {

# Returns a function body for a command.
# method_name is something like "Browser.getVersion"
gen_command_body <- function(method_name, params) {
gen_command_body <- function(domain_name, command_name, params) {
method_name <- paste0(domain_name, ".", command_name)

# Construct expressions for checking missing args
required_params <- params[!fetch_key_l(params, "optional", default = FALSE)]
Expand All @@ -105,6 +106,15 @@ gen_command_body <- function(method_name, params) {
expr({})
}

# If a domain is manualy enabled or disabled, opt out of auto events for that domain
enable_event_expr <- if (identical(command_name, "enable")) {
expr({private$manually_enable(!!domain_name)})
} else if (identical(command_name, "disable")) {
expr({private$manually_disable(!!domain_name)})
} else {
expr({})
}

# Construct parameters for message
param_list <- lapply(params, function(param) {
as.symbol(param$name)
Expand All @@ -125,6 +135,7 @@ gen_command_body <- function(method_name, params) {
if (!identical(wait_, TRUE) && !identical(wait_, FALSE))
stop("`wait_` must be TRUE or FALSE.")

!!!enable_event_expr

# Check for missing non-optional args
!!!check_missing_exprs
Expand Down
15 changes: 5 additions & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1070,13 +1070,9 @@ Currently setting custom headers requires a little extra work because it require

```R
b <- ChromoteSession$new()
# Currently need to manually enable Network domain notifications. Calling
# b$Network$enable() would do it, but calling it directly will bypass the
# callback counting and the notifications could get automatically disabled by a
# different Network event. We'll enable notifications for the Network domain by
# listening for a particular event. We'll also store a callback that will
# decrement the callback counter, so that we can disable notifications ater.
disable_network_notifications <- b$Network$responseReceived(function (msg) NULL)
# Currently need to manually enable Network domain notifications.
b$Network$enable()

b$Network$setExtraHTTPHeaders(headers = list(
foo = "bar",
header1 = "value1"
Expand All @@ -1097,9 +1093,8 @@ b$Page$navigate("http://scooterlabs.com/echo")
b$screenshot(show = TRUE)


# Disable extra headers entirely, by decrementing Network callback counter,
# which will disable Network notifications.
disable_network_notifications()
# Disable extra headers entirely, by disabling Network notifications.
b$Network$disable()
```

### Custom User-Agent
Expand Down