Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elm: define subscription port with no parameters

Tags:

elm

elm-port

Is there a way to define a subscription port with no parameters in Elm?

Something like:

port updateTime : () -> Sub msg

With this code, I'm getting the error that "Port 'updateTime' has an invalid type"

With the code:

port updateTime : (String -> msg) -> Sub msg

It's working but I don't need to send nothing from the javascript function to Elm.

like image 901
Buda Gavril Avatar asked Jan 16 '17 15:01

Buda Gavril


1 Answers

The closest you can get is probably something like this: Create the port similar to your examples but with the first parameters as (() -> msg):

port updateTime : (() -> msg) -> Sub msg

Assuming you have an UpdateTime Msg that accepts no parameters,

type Msg
    = ...
    | UpdateTime

You can then tie into the updateTime subscription like this:

subscriptions : Model -> Sub Msg
subscriptions model =
    updateTime (always UpdateTime)

Now, on the javascript side, you'll have to pass null as the only parameter.

app.ports.updateTime.send(null);

Update for Elm 0.19

In Elm 0.19 you had to be explicit about which Elm modules exposed ports, so you may have to update your module definition to include port module.

port module Main exposing (main)
like image 186
Chad Gilbert Avatar answered Nov 06 '22 10:11

Chad Gilbert