Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Is it possible to add your own javascript functions to a bundle? #854

Answered by anfelor
anfelor asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, it is!

I use the Haskell code:

foreign import javascript unsafe "sendMessage" sendMessage' :: JSVal -> IO ()
foreign import javascript   safe "requestMessage" requestMessage' :: JSVal -> IO JSVal

sendMessage :: ToJSON a => a -> IO ()
sendMessage = sendMessage' . jsonToJSVal

requestMessage :: (ToJSON a, FromJSON b) => a -> IO (Maybe b)
requestMessage m = do
  msg <- requestMessage' $ jsonToJSVal m
  case jsonFromJSVal msg of
    Right r -> pure $ Just r
    Left _ -> pure Nothing

and modify the <project>.req.mjs file to be:

var closure = null;

onmessage = function(msg) {
    if(closure !== null) {
        var cl = closure
        closure = null;
        cl(msg.data);
    } else {

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by anfelor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant