How to use ports inside StartApp in Elm

In my application based on the StartApp package, I have a port for communication from within JS. I am currently calling this port using a mailbox

requestPalette :
  { address : Signal.Address String
  , signal : Signal String
  }
requestPalette = Signal.mailbox ""


requestPaletteFilter : Signal String
requestPaletteFilter =
  Signal.filter (String.isEmpty >> not) "" requestPalette.signal
  |> settledAfter (300 * Time.millisecond)


port request : Signal String
port request = requestPaletteFilter

and using it as follows:

[on "input" targetValue (\str -> Signal.message requestPalette.address str)

I wonder if there is a way to this inside the function updateinstead of sending a message from the view.

+4
source share
1 answer

This applies to knitting 0.16 (and earlier), in knitting 0.17 subscriptions were changed to ports

To send a signal to the mailbox from the update, you need to use it StartApp, unlike StartApp.Simple, since the first one allows you to use effects in the function update.

, , , , No-Op :

type Action
  = NoOp
  | SendRequest String

update - SendRequest. StartApp, , Effects.task, , , Action, Task.succeed NoOp.

update action model =
  case action of
    NoOp ->
      (model, Effects.none)
    SendRequest str ->
      let
        sendTask =
          Signal.send requestPalette.address str
            `Task.andThen` (\_ -> Task.succeed NoOp)
      in
        (model, sendTask |> Effects.task)

click address, :

  [ on "input" targetValue (Signal.message address << SendRequest) ]

. request javascript, .

+5

Source: https://habr.com/ru/post/1623929/


All Articles