Skip to content

Instantly share code, notes, and snippets.

@shamansir
Last active December 13, 2017 13:37
Show Gist options
  • Save shamansir/1e9b22851f1c5d97ad9e66682f95b754 to your computer and use it in GitHub Desktop.
Save shamansir/1e9b22851f1c5d97ad9e66682f95b754 to your computer and use it in GitHub Desktop.
Elm v0.18 ports with headless program (send value to JS once)
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"jinjor/elm-time-travel": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<script src="./whatever.js"></script>
<script>
var app = Elm.NodeRepl21.worker();
console.log(Elm);
app.ports.outcome.subscribe(function(msg) {
console.log('got from port', msg);
});
app.ports.income.send(true);
</script>
</head>
<body>
</body>
</html>
elm-package install && elm-make ./whatever.elm --debug --output whatever.js
port module NodeRepl21 exposing (..)
import Html exposing (Html, div, text, span)
import Html
import Dict
import Platform exposing (..)
import Json.Decode
f_o_o_b_a_r_122 = (div [] [])
f_o_o_b_a_r_123 = (div [])
f_o_o_b_a_r_124 = (Html.a)
f_o_o_b_a_r_125 = (div)
f_o_o_b_a_r_126 = (text)
f_o_o_b_a_r_127 = (Html.map)
f_o_o_b_a_r_128 = (Html.beginnerProgram)
f_o_o_b_a_r_129 = (Just)
f_o_o_b_a_r_130 = (Nothing)
f_o_o_b_a_r_131 = ("Nothing")
f_o_o_b_a_r_132 = (1)
valuesRec =
{ f_o_o_b_a_r_122 = f_o_o_b_a_r_122
, f_o_o_b_a_r_123 = f_o_o_b_a_r_123
, f_o_o_b_a_r_124 = f_o_o_b_a_r_124
, f_o_o_b_a_r_125 = f_o_o_b_a_r_125
, f_o_o_b_a_r_126 = f_o_o_b_a_r_126
, f_o_o_b_a_r_127 = f_o_o_b_a_r_127
, f_o_o_b_a_r_128 = f_o_o_b_a_r_128
, f_o_o_b_a_r_129 = f_o_o_b_a_r_129
, f_o_o_b_a_r_130 = f_o_o_b_a_r_130
, f_o_o_b_a_r_131 = f_o_o_b_a_r_131
, f_o_o_b_a_r_132 = f_o_o_b_a_r_132
}
stringValueComponent : String -> Html a
stringValueComponent value =
span [] [ text value ]
intValueComponent : Int -> Html a
intValueComponent value =
span [] [ text <| toString value ]
filterRecord fn val list =
List.filter
(fn >> (==) val)
list
-- getStrFromRec : { f_o_o_b_a_r_131 } -> String -> String
-- getStrFromRec r field =
-- case field of
-- "f_o_o_b_a_r_131" -> r.f_o_o_b_a_r_131
valueToHtml : String -> String -> Html a
valueToHtml varName compType =
case compType of
"String" -> stringValueComponent valuesRec.f_o_o_b_a_r_131
_ -> div [] []
port outcome : List String -> Cmd msg
port income : (Bool -> msg) -> Sub msg
type alias Model = Maybe (List String)
-- type Anything a = AnInt Int | ABool Bool | AString String | AFunction1 (a -> a) | Other a
-- valueToComponent : String -> Anything a -> Html b
-- valueToComponent componentType value =
-- case componentType of
-- "String" -> stringValueComponent (AString value)
-- _ -> div [] []
-- values : List a
-- values =
-- [ f_o_o_b_a_r_122
-- , f_o_o_b_a_r_123
-- , f_o_o_b_a_r_124
-- , f_o_o_b_a_r_125
-- , f_o_o_b_a_r_126
-- , f_o_o_b_a_r_127
-- , f_o_o_b_a_r_128
-- , f_o_o_b_a_r_129
-- , f_o_o_b_a_r_130
-- ]
valuesAsStrings : List String
valuesAsStrings =
[ toString f_o_o_b_a_r_122
, toString f_o_o_b_a_r_123
, toString f_o_o_b_a_r_124
, toString f_o_o_b_a_r_125
, toString f_o_o_b_a_r_126
, toString f_o_o_b_a_r_127
, toString f_o_o_b_a_r_128
, toString f_o_o_b_a_r_129
, toString f_o_o_b_a_r_130
]
update : Bool -> Model -> (Model, Cmd msg)
update _ _ =
(Just valuesAsStrings, outcome valuesAsStrings)
main : Program Never Model Bool
main =
program
{ init = (Nothing, Cmd.none)
, update = update
, subscriptions = (\_ -> income (\_ -> True))
}
port module NodeRepl21 exposing (..)
import Html exposing (Html, div, text, span)
import Html
import Dict
import Platform exposing (..)
import Json.Decode
f_o_o_b_a_r_122 = (div [] [])
f_o_o_b_a_r_123 = (div [])
f_o_o_b_a_r_124 = (Html.a)
f_o_o_b_a_r_125 = (div)
f_o_o_b_a_r_126 = (text)
f_o_o_b_a_r_127 = (Html.map)
f_o_o_b_a_r_128 = (Html.beginnerProgram)
f_o_o_b_a_r_129 = (Just)
f_o_o_b_a_r_130 = (Nothing)
f_o_o_b_a_r_131 = ("Nothing")
f_o_o_b_a_r_132 = (1)
valuesRec =
{ f_o_o_b_a_r_122 = f_o_o_b_a_r_122
, f_o_o_b_a_r_123 = f_o_o_b_a_r_123
, f_o_o_b_a_r_124 = f_o_o_b_a_r_124
, f_o_o_b_a_r_125 = f_o_o_b_a_r_125
, f_o_o_b_a_r_126 = f_o_o_b_a_r_126
, f_o_o_b_a_r_127 = f_o_o_b_a_r_127
, f_o_o_b_a_r_128 = f_o_o_b_a_r_128
, f_o_o_b_a_r_129 = f_o_o_b_a_r_129
, f_o_o_b_a_r_130 = f_o_o_b_a_r_130
, f_o_o_b_a_r_131 = f_o_o_b_a_r_131
, f_o_o_b_a_r_132 = f_o_o_b_a_r_132
}
stringValueComponent : String -> Html a
stringValueComponent value =
span [] [ text value ]
intValueComponent : Int -> Html a
intValueComponent value =
span [] [ text <| toString value ]
filterRecord fn val list =
List.filter
(fn >> (==) val)
list
getStrFromRec : { f_o_o_b_a_r_131 } -> String -> String
getStrFromRec r field =
case field of
"f_o_o_b_a_r_131" -> r.f_o_o_b_a_r_131
valueToHtml : String -> String -> Html a
valueToHtml varName compType =
case compType of
"String" -> stringValueComponent valuesRec.f_o_o_b_a_r_131
_ -> div [] []
port outcome : List String -> Cmd msg
port income : (Bool -> msg) -> Sub msg
type alias Model = Maybe (List String)
-- type Anything a = AnInt Int | ABool Bool | AString String | AFunction1 (a -> a) | Other a
-- valueToComponent : String -> Anything a -> Html b
-- valueToComponent componentType value =
-- case componentType of
-- "String" -> stringValueComponent (AString value)
-- _ -> div [] []
-- values : List a
-- values =
-- [ f_o_o_b_a_r_122
-- , f_o_o_b_a_r_123
-- , f_o_o_b_a_r_124
-- , f_o_o_b_a_r_125
-- , f_o_o_b_a_r_126
-- , f_o_o_b_a_r_127
-- , f_o_o_b_a_r_128
-- , f_o_o_b_a_r_129
-- , f_o_o_b_a_r_130
-- ]
valuesAsStrings : List String
valuesAsStrings =
[ toString f_o_o_b_a_r_122
, toString f_o_o_b_a_r_123
, toString f_o_o_b_a_r_124
, toString f_o_o_b_a_r_125
, toString f_o_o_b_a_r_126
, toString f_o_o_b_a_r_127
, toString f_o_o_b_a_r_128
, toString f_o_o_b_a_r_129
, toString f_o_o_b_a_r_130
]
update : Bool -> Model -> (Model, Cmd msg)
update _ _ =
(Just valuesAsStrings, outcome valuesAsStrings)
main : Program Never Model Bool
main =
program
{ init = (Nothing, Cmd.none)
, update = update
, subscriptions = (\_ -> income (\_ -> True))
}
@mLuby
Copy link

mLuby commented Feb 10, 2017

Thanks for posting this! Seems better suited for a repo than a gist. And it's not very clear what it does…

@shamansir
Copy link
Author

It seems I messed it up @ 21 Dec, please see the first revision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment