Skip to content

Commit

Permalink
debounce -> throttle
Browse files Browse the repository at this point in the history
Co-Authored-By: Garrick Aden-Buie <garrick@adenbuie.com>
  • Loading branch information
schloerke and gadenbuie committed Sep 4, 2024
1 parent 15c2ba1 commit 61ff563
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# learnr (development version)

- Added a new option, `tutorial.exercise.debounce`, to slow down successive exercise execution. This option should be set to the number of seconds a user will have to wait before their next code execution is performed. The option defaults to 1 second to deter rapid code executions. To disable this behavior, call `options(tutorial.exercise.debounce = 0)` within your setup chunk. (@internaut, #818)
- Added a new option, `tutorial.exercise.throttle`, to slow down successive exercise execution. This option should be set to the number of seconds a user will have to wait between performing code executions. The option defaults to 1 second to deter rapid code executions. To disable submission throttling, call `options(tutorial.exercise.throttle = 0)` within your setup chunk. (@internaut, #818)

- Removed dependency on ellipsis (@olivroy, #809)

Expand Down
8 changes: 4 additions & 4 deletions R/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ setup_exercise_handler <- function(exercise_rx, session) {
# setup reactive values for return
rv <- reactiveValues(triggered = 0, result = NULL)

# debounce option to slow down successive exercise execution requests
debounce_s <- getOption("tutorial.exercise.debounce", 1) # in seconds
if (is.numeric(debounce_s) && debounce_s > 0) {
exercise_rx <- debounce(exercise_rx, debounce_s * 1000) # in milliseconds
# throttle option to slow down successive exercise execution requests
throttle_s <- getOption("tutorial.exercise.throttle", 1) # in seconds
if (is.numeric(throttle_s) && throttle_s > 0) {
exercise_rx <- throttle(exercise_rx, throttle_s * 1000) # in milliseconds
}
# observe input
observeEvent(exercise_rx(), {
Expand Down
14 changes: 7 additions & 7 deletions vignettes/articles/exercises.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ This option can also be set either globally or per-chunk:
insert_snippet("exerciseeval")
```

It may be necessary to slow down successive exercise submissions, otherwise users may overwhelm your server by running exercise code chunks in fast repetition. You can do so globally by setting the option `tutorial.exercise.debounce` to an amount of time (in seconds) that is waited before the same user's next code execution is performed. This option is set to 1 second by default.
To prevent server overload caused by users rapidly clicking the "Run Code" button, you can set a delay between exercise code submissions using the `tutorial.exercise.throttle` option (in seconds). This will ensure that the same user can only start the evaluation of the same exercise per `tutorial.exercise.throttle` seconds. This option is set globally and the default is a throttle of of 1 second.

```{r snippet-exercisedebounce, eval = FALSE}
# Set exercise submission debounce time to 5 seconds
options("tutorial.exercise.debounce" = 5)
```{r snippet-exercisethrottle, eval = FALSE}
# Set exercise submission throttle time to 5 seconds
options("tutorial.exercise.throttle" = 5)
```

To disable of this behavior, you can set the option to 0:

```{r snippet-exercisedebounceoff, eval = FALSE}
# Disable exercise submission debounce
options("tutorial.exercise.debounce" = 0)
```{r snippet-exercisethrottleoff, eval = FALSE}
# Disable exercise submission throttle
options("tutorial.exercise.throttle" = 0)
```

## Exercise Setup {#exercise-setup}
Expand Down

0 comments on commit 61ff563

Please sign in to comment.