ghcjs / jsaddle

JavaScript interface that works with GHCJS or GHC
116 stars 62 forks source link

jsaddle missing sync message handler #107

Open dmjio opened 4 years ago

dmjio commented 4 years ago

Receiving the following when attempting to use jsaddle w/ miso and ghcid. The webpage never reloads after the buffer is edited. Could be a configuration problem on my part. Posting here in case anyone else knows the fix.

CallStack (from HasCallStack):
  error, called at src/Language/Javascript/JSaddle/WebSockets.hs:129:40 in jsaddle-warp-0.9.6.0-KtjEoVsob1zE4MwtcuErz8:Language.Javascript.JSaddle.WebSockets

Haskell code used.

runApp f =                                                                                            
  Warp.runSettings (Warp.setPort 8080 (Warp.setTimeout 3600 Warp.defaultSettings)) =<<                
    JSaddle.jsaddleOr defaultConnectionOptions (f >> syncPoint) JSaddle.jsaddleApp 

main :: IO ()                                                                                         
main = runApp $ startApp App {..}                                                                     
  where                                                                                               
    initialAction = SayHelloWorld -- initial action to be executed on application load                
    model  = 0                    -- initial model                                                    
    update = updateModel          -- update function                                                  
    view   = viewModel            -- view function                                                    
    events = defaultEvents        -- default delegated events                                         
    subs   = []                   -- empty subscription list                                          
    mountPoint = Nothing          -- mount point for application (Nothing defaults to 'body')         

After calling nix-shell --run reload which invokes ghcid -T ':main'

with (import ./default.nix);                                                                          
dev.env.overrideAttrs (old: {                                                                         
  shellHook = ''                                                                                      
    function reload () {                                                                              
      ${pkgs.haskell.packages.ghc865.ghcid}/bin/ghcid -T ':main'                                       
    }                                                                                                 
  '';                                                                                                 
})                                                                                                    
Tehnix commented 4 years ago

Currently running into this issue also, from Miso HEAD. Is there a known version that works before this?


Some additional information (don't know if it helps, but might as well):

The console log will report the following after triggering a reload using the config in the comment above:

jsaddle.js:150 POST http://localhost:8080/sync/-631705263071168198 500 (Internal Server Error)
(anonymous) @ jsaddle.js:150
func @ jsaddle.js:151
eval @ VM21:32
VM56:1 Uncaught SyntaxError: Unexpected token S in JSON at position 0
    at JSON.parse (<anonymous>)
    at jsaddle.js:151
    at func (jsaddle.js:151)
    at HTMLBodyElement.eval (eval at processBatch (jsaddle.js:84), <anonymous>:32:7)

Which points to xhr.send(JSON.stringify({"tag": "Callback", "contents": [lastResults[0], lastResults[1], nFunction, nFunctionInFunc, nThis, args]}));, which I think should map to jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs#L152 (since it is between case "NewSyncCallback" and case "FreeCallback").

No big changes has happened in this file in a while, so I doubt that's the root cause.

Looking at the original location in question jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs#L129, the only "recent" changes I can see are the triple equality check added 2 months ago (https://github.com/ghcjs/jsaddle/commit/50950b532643645d3ad58f21d8280f1404e284e8) and a fix to use two websockets to support macOS 11 months ago (https://github.com/ghcjs/jsaddle/commit/e6ba1fc06e455d4060f1b82db751c7d0fa2bb2e9).


This leads me to suspect that it's not a bug introduced in JSAddle necessarily, but maybe GHCJS or the browser changed, and it has to be accommodated?

polux commented 4 years ago

Did any of you manage to work around this since this bug was filed?

smelc commented 4 years ago

I worked around this issue using entr to watch file changes, to trigger reloading the midori browser (a midori tab can reloaded from the command line):

#!/usr/bin/bash

# Install entr if missing
which entr || sudo apt install entr
# Install midori if missing
which midori || sudo snap install midori

function midori_listen() {
  # That's the whole point of this script. It listens to changes to all versioned .hs files:
  # and pings midori
  git ls-files "*.hs" | entr -s "midori -p -e tab-reload"
}

midori_listen &

# Start midori if not yet there:
[[ $(pgrep midori) == "0" ]] || (midori "http://localhost:8080" &)

# Regenerate js upon .hs saving, usual miso business:
nix-shell --run reload