2020-11-23 15:44:45 +03:00
|
|
|
module Route exposing (..)
|
|
|
|
|
|
|
|
import Air exposing (call, callBI, fold, next, par, relayEvent, seq, set)
|
|
|
|
import Json.Encode as Encode
|
2020-11-23 16:31:31 +03:00
|
|
|
import Model exposing (Model, Route(..))
|
2020-11-23 15:44:45 +03:00
|
|
|
import Port exposing (sendAir)
|
|
|
|
import Url.Parser exposing ((</>), Parser, map, oneOf, s, string)
|
|
|
|
|
|
|
|
|
|
|
|
routeParser : Parser (Route -> a) a
|
|
|
|
routeParser =
|
|
|
|
oneOf
|
|
|
|
[ map Peer (s "peer" </> string)
|
|
|
|
, map Page string
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-11-23 16:31:31 +03:00
|
|
|
parse url =
|
|
|
|
Maybe.withDefault (Page "") <| Url.Parser.parse routeParser url
|
|
|
|
|
|
|
|
|
2020-11-23 15:44:45 +03:00
|
|
|
routeCommand : Model -> Route -> Cmd msg
|
|
|
|
routeCommand m r =
|
|
|
|
case r of
|
|
|
|
Page _ ->
|
|
|
|
let
|
|
|
|
clientId =
|
|
|
|
set "clientId" <| Encode.string m.peerId
|
|
|
|
|
|
|
|
relayId =
|
|
|
|
set "relayId" <| Encode.string m.relayId
|
|
|
|
|
|
|
|
air =
|
|
|
|
seq
|
|
|
|
(callBI "relayId" ( "dht", "neighborhood" ) [ "clientId" ] (Just "peers"))
|
2020-11-23 17:29:56 +03:00
|
|
|
(par
|
|
|
|
(relayEvent "peers_discovered" [ "relayId", "peers" ])
|
|
|
|
(fold "peers" "p" <|
|
|
|
|
par
|
|
|
|
(seq
|
|
|
|
(callBI "p" ( "dht", "neighborhood" ) [ "clientId" ] (Just "morePeers[]"))
|
|
|
|
(relayEvent "peers_discovered" [ "p", "morePeers" ])
|
|
|
|
)
|
|
|
|
(next "p")
|
|
|
|
)
|
2020-11-23 15:44:45 +03:00
|
|
|
)
|
|
|
|
in
|
|
|
|
sendAir (relayId <| clientId <| air)
|
|
|
|
|
|
|
|
Peer _ ->
|
|
|
|
Cmd.none
|