I'm a total PureScript newbie and need a bit of help figuring out why a FFI function modeled with the Aff monad doesn't seem to be working for me.
The expected behavior is to log a message "keyMessage" to the console after 1000ms.
Instead, the program just hangs indefinitely after the following output:
Compiling Main
* Build successful.
Waiting for message...
Main.purs:
module Main where
import Prelude
import Control.Monad.Aff (Aff, Fiber, launchAff)
import Control.Monad.Aff.Console (log)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Console (CONSOLE)
main :: forall e. Eff (console :: CONSOLE, to :: TIMEOUT | e) (Fiber (console :: CONSOLE, to :: TIMEOUT | e) Unit)
main = launchAff do
log "Waiting for message..."
m <- message "key"
log m
foreign import data TIMEOUT :: Effect
foreign import message :: forall e. String -> Aff (to :: TIMEOUT | e) String
Main.js:
'use strict';
exports.message = function(key) {
return function(errback, callback) {
var timeout = setTimeout(function() {
callback(key + 'Message');
}, 1000);
return function() {
return function (cancelErrback, cancelCallback) {
clearTimeout(timeout);
return cancelCallback();
};
};
};
};
Thanks in advance for your insights!
If you're using the latest major version of purescript-aff (v4 or later) then the runtime representation of Aff has changed, and you can't create it directly using the errback/callback function style anymore.
Take a look at the https://pursuit.purescript.org/packages/purescript-aff/4.0.2/docs/Control.Monad.Aff.Compat module, in particular the EffFnAff type / fromEffFnAff functions for an explanation of how the equivalent thing works now.
Alternatively you can also construct Affs with makeAff, but that would require reformulating your FFI code a bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With