git » jacl.git » master » tree

[master] / jacl-client.R

#!/usr/bin/env Rscript

# Copyright (c) 2020-2021 Alan Dipert <alan@dipert.org>
# Part of the JACL project: https://tailrecursion.com/JACL/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

library(R6)
library(chromote)
library(jsonlite)

invisibly <- function(expr) {
  force(expr)
  invisible()
}

first_id <- function(r) {
  ts <- r$Target$getTargets()$targetInfos
  stopifnot(length(ts) > 0)
  r$Target$getTargets()$targetInfos[[1]]$targetId
}

# TODO figure out how to use Log instead of deprecated Console
invisibly({
  rc <- ChromeRemote$new(host = "localhost", port = 9222)
  r <- Chromote$new(browser = rc)
  tid <- first_id(r)
  b <- r$new_session(targetId = tid)
  # The below is for use with headless chrome:
  #b <- ChromoteSession$new()
  #b$Page$navigate("http://localhost:8000")
  b$Console$messageAdded(function (msg) { 
    cat(msg$message$text)
    cat('\n> ')
  })
  b$Console$enable()
})

BufferedInput <- R6::R6Class("BufferedInput",
  public = list(
    initialize = function(f, callback) {
      fd <- file(f, "rb", blocking = FALSE)
      schedule <- function() {
        private$scheduled <- later::later(function() {
          callback(private$chunk)
          private$chunk <- character(0)
        }, delay = 0.01, loop = self$child_loop)
      }
      read_chunk <- function() {
        # select(2) on stdin here instead of polling would be cool
        chars <- rawToChar(readBin(fd, "raw", 8192))
        if (nchar(chars)) {
          if (!is.null(private$scheduled)) private$scheduled()
          private$chunk <- paste0(private$chunk, chars)
          schedule()
        }
        later::later(read_chunk, 0.01, loop = self$child_loop)
      }
      read_chunk()
    },
    child_loop = later::create_loop(autorun = FALSE)
  ),
  private = list(
    chunk = character(0),
    scheduled = NULL
  )
)

cat('> ')

bi <- BufferedInput$new("stdin", function(chunk) {
  json_str <- toJSON(chunk, auto_unbox = TRUE)
  code <- paste0('replInputStream.writeEach(', json_str, ')')
  b$Runtime$evaluate(code)
})

while(TRUE) {
  later::run_now(Inf, loop = bi$child_loop)
}