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

How to wait for the runtime to be ready? #110

Closed
stla opened this issue Jul 18, 2023 · 4 comments
Closed

How to wait for the runtime to be ready? #110

stla opened this issue Jul 18, 2023 · 4 comments

Comments

@stla
Copy link
Contributor

stla commented Jul 18, 2023

Hello,

I have the code below. When I extract all the code from the functions and I run it, this works. But when I run these functions and the example at the end, I get: Assertion failed: you need to wait for the runtime to be ready. How could I wait for the runtime to be ready?

giacSession <- function(chromePath = find_chrome()) {
  chrm <- Chrome$new(
    path = chromePath,
    args = "--disable-gpu --headless --remote-debugging-port=9222"
  )
  chromote <- Chromote$new(browser = chrm)
  session  <- ChromoteSession$new(parent = chromote)
  ids <- session$Page$navigate("about:blank")
  script <- paste0(readLines("giacwasm.js"), collapse = "\n")
  . <- session$Runtime$evaluate(script)
  . <- session$Runtime$evaluate("
var UI = {
  Datestart: Date.now(), 
  ready: false, 
  warnpy: false
};
Module.onRuntimeInitialized = function() {
  UI.ready = true; 
;};
")
  . <- session$Runtime$evaluate(
    "var docaseval = Module.cwrap('caseval', 'string', ['string']);"
  )
  session
}

giacEvaluate <- function(session, command) {
  evaluate <- session$Runtime$evaluate(
    sprintf("docaseval('%s')", command)
  )
  if(evaluate$result$type != "string") {
    stop("An error occured.")
  }  
  evaluate$result$value
}

closeGiacSession <- function(session) {
  session$close()
}

# exemple ####
session <- giacSession()
giacEvaluate(session, "2 + 3/7")
@stla
Copy link
Contributor Author

stla commented Jul 18, 2023

I can use Sys.sleep(5) but is it a serious way ?

@stla
Copy link
Contributor Author

stla commented Jul 18, 2023

Better:

  ready <- session$Runtime$evaluate("UI.ready")$result$value
  while(!ready) {
    ready <- session$Runtime$evaluate("UI.ready")$result$value
  }

@gadenbuie
Copy link
Member

Hi @stla, I'm fairly certain you're running into issues resulting from $Runtime$evaluate() being evaluated asynchronously. I'd recommend taking a look at the section on Synchronous vs Asynchronous Usage in the README, although collecting the result via $result might also be enough.

On the whole, it seems like you've solved this, but if not please feel free to re-open the issue with a new comment.

@stla
Copy link
Contributor Author

stla commented Aug 12, 2023

This is some C code compiled in JavaScript. I don't remember the name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants