r/purescript • u/attilah • Jun 09 '18
How to call asynchronous functions from `createLifecycleComponent`'s eval function ?
I am using the library: https://github.com/doolse/purescript-reactnative
And I have been trying for hours to make a call to a JS function that returns a Promise from within an action evaluator.
My code looks like this:
1- Linking.js:
const RN = require('react-native'); exports.getInitialURLImpl = function(v) { return RN.Linking.getInitialURL(); } 2- Linking.purs:
foreign import data LINKING :: Effect foreign import getInitialURLImpl :: forall eff. Unit -> Eff eff (Promise String) getInitialURL :: forall eff. Aff (linking :: LINKING | eff) String getInitialURL = liftEff (getInitialURLImpl unit) >>= toAff 3- Main.purs:
initialState = {} data Action = HandleDeepLinking app :: forall p. ReactClass p app = createLifecycleComponent (didMount $ HandleDeepLinking) initialState render2 eval where eval (HandleDeepLinking) = void $ launchAff do x <- getInitialURL liftEff $ alert "test" (Just x) pure unit The above app
value does not typecheck. I don't know how to be able to access the result of a Promise from within the action evaluator.
Help!!!
1
Upvotes
1
u/dtwhitney Jun 10 '18
Hey I'm not sure about your exact use case, but just a few notes:
Efftype has become depricated (depricated is the wrong word, but maybe "less preferred"). So a bunch of libraries on Pursuit have been converted to the newEffecttype. It doesn't look thepurescript-react-nativehas made this transition, so when you are looking at docs on Pursuit (pursuit.purescript.org), make sure you've selected a version of the docs compatible withpurescritp-react-native, which is basically anything targeting PureScript 0.11. I will link to libraries compatible with 0.11purescript-affhttps://pursuit.purescript.org/packages/purescript-aff/4.1.1 The docs are a pretty good!purescript-aff-promise(https://pursuit.purescript.org/packages/purescript-aff-promise/1.0.0) a bit easier to deal with than going down the route you've chosen withEff eff (Promise String). YMMVpurescript-reactandpurescript-react-native, I do find them difficult to use and after writing the bulk of a fairly large app written in PureScript/React/React Native, my team made the choice not to use those libraries and instead roll our own. If you are open to that, I've got a "React Native in 5 minutes" gist here: https://gist.github.com/dwhitney/e2a040432040607ae519fdf05cbc27ad -- that will get you going and you can determine the "right" amount of type safety from there. Note that the same approach will work with React.Effecttype favored by PureScript 0.12 is so much easier to work with than theEfftype. I'd make the switch to it ASAP. Or if you can't - don't hesitate to useunsafeCoerceEffto make the row type inEffline up. I've wasted untold hours trying to get those damn rows to line up! Don't you waste those hours too, especially when the community has decided to move away from them! Save the row types forpurescript-runwhen you need it!functionalprogramming.slack.com#purescript channel is pretty active, and the people there are always very helpful and kind.