Air ports

This commit is contained in:
dmitry 2020-11-23 14:27:33 +03:00
parent 823f1aecc4
commit 02cc8d3d8c
11 changed files with 17228 additions and 34 deletions

17098
package-lock.json generated

File diff suppressed because it is too large Load Diff

54
src/Air.elm Normal file
View File

@ -0,0 +1,54 @@
module Air exposing (..)
import Dict exposing (Dict)
import Json.Encode exposing (Value)
type Air
= Air (Dict String Value) String
call : String -> String -> List String -> Maybe String -> Air
call peerPart fnPart args res =
let
captureResult =
case res of
Just n ->
" " ++ n
Nothing ->
""
in
Air Dict.empty ("(call " ++ peerPart ++ " " ++ fnPart ++ " [" ++ String.join " " args ++ "]" ++ captureResult ++ ")\n")
event : String -> List String -> Air
event name args =
call "%init_peer_id%" ("(\"event\" \"" ++ name ++ "\")") args Nothing
combine : String -> Air -> Air -> Air
combine combName (Air ld ls) (Air rd rs) =
Air (Dict.union ld rd) ("(" ++ combName ++ "\n " ++ ls ++ "\n " ++ rs ++ ")\n")
seq =
combine "seq"
par =
combine "par"
xor =
combine "xor"
fold : String -> String -> Air -> Air
fold iter item (Air d s) =
Air d ("(fold " ++ iter ++ " " ++ item ++ "\n" ++ s ++ ")\n")
next : String -> Air
next item =
Air Dict.empty ("(next " ++ item ++ ")\n")

View File

@ -2,6 +2,7 @@ module Config exposing (..)
type alias Config =
{ peerId: String
, relayId: String
}

View File

@ -19,23 +19,26 @@ limitations under the License.
import Browser exposing (Document)
import Config exposing (Flags)
import Model exposing (Model, emptyModel)
import Msg exposing (Msg)
import Msg exposing (Msg(..))
import Subscriptions exposing (subscriptions)
import Update exposing (update)
import View exposing (view)
import Url
import Browser.Navigation as Navigation
main =
Browser.document
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlChange = UrlChange
, onUrlRequest = Request
}
init : Flags -> ( Model, Cmd Msg )
init flags =
init : Flags -> Url.Url -> Navigation.Key -> ( Model, Cmd Msg )
init flags _ _ =
let
( em, initCmd ) =
emptyModel flags

View File

@ -22,13 +22,14 @@ import Msg exposing (Msg(..))
type alias Model =
{ peerId : String
, relayId : String
}
emptyModel : Config -> ( Model, Cmd Msg )
emptyModel config =
( { peerId = config.peerId
, relayId = config.relayId
}
, Cmd.none
)

View File

@ -1,3 +1,12 @@
module Msg exposing (..)
import Url
import Browser exposing (UrlRequest)
import Port
type Msg = NoOp
| UrlChange Url.Url
| Request UrlRequest
| Event Port.ReceiveEvent
| RelayChanged String
| Click

22
src/Port.elm Normal file
View File

@ -0,0 +1,22 @@
port module Port exposing (..)
import Air exposing (Air(..))
import Dict exposing (Dict)
import Json.Encode exposing (Value)
type alias SendParticle = {script: String, data: Value}
type alias ReceiveEvent = {name: String, args: List Value}
port sendParticle: SendParticle -> Cmd msg
port eventReceiver: (ReceiveEvent -> msg) -> Sub msg
port relayChanged: (String -> msg) -> Sub msg
sendAir: Air -> Cmd msg
sendAir (Air dataDict script) =
let
data = Json.Encode.object <| Dict.toList dataDict
in
sendParticle {script = script, data = data}

View File

@ -18,10 +18,10 @@ limitations under the License.
import Model exposing (Model)
import Msg exposing (Msg(..))
import Port exposing (eventReceiver)
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[
[ eventReceiver Event
]

View File

@ -16,8 +16,10 @@ limitations under the License.
-}
import Air
import Model exposing (Model)
import Msg exposing (..)
import Port exposing (sendAir)
update : Msg -> Model -> ( Model, Cmd Msg )
@ -26,5 +28,23 @@ update msg model =
NoOp ->
( model, Cmd.none )
UrlChange u ->
( model, Cmd.none )
Request u ->
( model, Cmd.none )
Event { name, args } ->
let
a =
Debug.log "event in ELM" name
in
( model, Cmd.none )
Click ->
( model
, sendAir <| Air.event "hello" []
)
RelayChanged relayId ->
( { model | relayId = relayId }, Cmd.none )

View File

@ -19,6 +19,7 @@ limitations under the License.
import Browser exposing (Document)
import Html exposing (Html, div, header, text)
import Html.Attributes exposing (class, classList)
import Html.Events exposing (onClick)
import Model exposing (Model)
import Msg exposing (..)
import Palette exposing (classes)
@ -44,6 +45,6 @@ layout : List (Html Msg) -> Html Msg
layout elms =
div [classes "mw9 center"]
[div [classes "fl w-100 pa2"] ([
header [classes "w-100 bt bb b--black-10"] [text "Fluence Network Dashboard"]
header [classes "w-100 bt bb b--black-10", onClick Click] [text "Fluence Network Dashboard"]
] ++elms)]

View File

@ -25,7 +25,8 @@ import {Service, ServiceMultiple, ServiceOne} from "fluence/dist/service";
function genFlags(peerId: string): any {
return {
peerId: peerId
peerId: peerId,
relayId: relays[0].peerId
}
}
@ -39,28 +40,30 @@ function genFlags(peerId: string): any {
flags: flags
});
let service = new ServiceMultiple("custom")
service.registerFunction("func", (args: any[]) => {
console.log("call")
let eventService = new ServiceOne("event", (fnName, args: any[]) => {
console.log("event service called: " + fnName)
app.ports.eventReceiver.send({name: fnName, args})
return {}
})
registerService(service)
let serviceOne = new ServiceOne("customOne", (fnName, args: any[]) => {
console.log("call " + fnName)
return {}
})
registerService(serviceOne)
registerService(eventService)
// If the relay is ever changed, an event shall be sent to elm
let client = await Fluence.connect(relays[1].multiaddr, pid)
let script = `(call self_peer ("customOne" "some_func") [])`
app.ports.sendParticle.subscribe(async(part: any) => {
console.log("Going to build particle", part)
let jsonData = part.data;
let data = new Map()
data.set("self_peer", client.selfPeerIdStr)
let map = new Map<string, string>()
for (let v in jsonData) if(jsonData.hasOwnProperty(v)) {
map.set(v,jsonData[v])
}
let particle = await build(client.selfPeerId, script, data)
await client.sendParticle(particle)
let particle = await build(client.selfPeerId, part.script, map)
await client.sendParticle(particle)
})
})();