Please help — how to start writing an Effect Manager module?


I’m experimenting with a new approach to bridging Elm and React Native / Expo. I have some basic rendering working, but now I need to add events handling, especially touch events. I want to build on Dom.LowLevel.onDocument, with an API resembling the Mouse package. The low-level JS handling of the stuff I know how to do (I’ll be intercepting document.addEventListener() calls), but I’m having trouble with the Elm side — how should I write an Effect Manager module? I started reading the Mouse package and trying to understand it, but it showed up to be much more complex than I expected, and I’m getting lost :frowning: especially once I saw the effect module Mouse preamble on the first line, I realized that’s something different than the usual stuff…

Could someone please help me?

Ideally, I’d like to have an API like follows, for the first low-level prototype implementation of the package:

touchEvents : Json.Decode.Decoder msg -> Sub msg

How should I start to approach writing such a module? How could I write even a simplest possible kind of an effects subscriptions module?

Or, alternatively, could you explain to me how the Mouse module works, so that I could copy what I need from it?


Note: Please excuse any harshness in the tone of this reply. I want to be as direct as possible about this.

You shouldn’t write an effect manager. Effect managers are an internal implementation detail from the mechanisms that Elm’s core packages use to interact with the outside world. They are not meant to be a general use API, and it has been discussed fairly extensively in this forum that the fact that they can be compiled in Elm 0.18.0 is largely incidental.The ability to compile them outside of Elm’s core packages will be revoked in Elm 0.19.0. Their syntax, implementation, and even existence could fluctuate at any time without notice as we learn new things, and it is important to us that making any of those changes has a very controlled and optimized impact on the ecosystem.

The way to achieve what you want to do is to use ports. Ports are a stable API you can depend on, and are effectively the closest thing we will have to implementing a custom effect manager. If you find that approach to be unworkable for your use case then you should consider using PureScript or Reason.

If you have questions about your particular use of ports, please open up as many new topics as you would like.