git » jacl.git » commit 0367158

repl client cleanup

author Alan Dipert
2019-12-28 07:00:08 UTC
committer Alan Dipert
2019-12-28 07:00:08 UTC
parent c8f5dc23570975052a04b449745877fdc4903078

repl client cleanup

repl-client/jacl-client.R +19 -38

diff --git a/repl-client/jacl-client.R b/repl-client/jacl-client.R
index 122cfe2..d5f36c4 100755
--- a/repl-client/jacl-client.R
+++ b/repl-client/jacl-client.R
@@ -22,61 +22,42 @@ invisibly({
 
 BufferedInput <- R6::R6Class("BufferedInput",
   public = list(
-    initialize = function(f, callback, window_ms) {
-      private$fd <- file(f, "rb", blocking = FALSE)
-      private$callback <- callback
-      private$window_ms <- window_ms
+    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() {
-        # TODO select() on stdin here would be cool
-        chars <- rawToChar(readBin(private$fd, "raw", 8192))
+        # 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)
-          private$schedule()
+          schedule()
         }
-        later::later(read_chunk, 0.01, loop = private$child_loop)
+        later::later(read_chunk, 0.01, loop = self$child_loop)
       }
       read_chunk()
     },
-    run = function() {
-      while(TRUE) {
-        later::run_now(Inf, loop = private$child_loop)
-      }
-    }
+    child_loop = later::create_loop(autorun = FALSE)
   ),
   private = list(
-    window_ms = 50,
-    schedule = function() {
-      private$scheduled <- later::later(function() {
-        private$callback(private$chunk)
-        private$chunk <- character(0)
-      }, delay = private$window_ms/1000, loop = private$child_loop)
-    },
     chunk = character(0),
-    scheduled = NULL,
-    callback = NULL,
-    fd = NULL,
-    child_loop = later::create_loop(autorun = FALSE)
+    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)
-}, 10)
-bi$run()
+})
 
-#cat('> ')
-#f <- file("stdin", "rb", blocking = FALSE)
-#
-#while (isOpen(f)) {
-#  str <- rawToChar(readBin(f, "raw", 8192))
-#  cat("str=", str, "\n")
-#  #char <- rawToChar(readBin(f, "raw", 1))
-#  #cat("char=",char,"\n")
-#  #json_str <- toJSON(char, auto_unbox = TRUE)
-#  #code <- paste0('replInputStream.writeEach(', json_str, ')')
-#  #b$Runtime$evaluate(code)
-#}
\ No newline at end of file
+while(TRUE) {
+  later::run_now(Inf, loop = bi$child_loop)
+}
\ No newline at end of file