port apiSession: Task String SessionId
import Ports
import Json.Decode as Decode
import Task exposing (andThen)
-- get an API session from JS land and make an http request using it
-- given some path and a decoder to decipher the result:
apiRequest : String -> Decoder a -> Task ApiError a
apiRequest path decoder =
let
headers sessId =
[ ("Content-Type", "application/json")
, ("MyApp-SessionId", sessId)
]
req sessId = Http.send Http.defaultSettings
{ verb = "POST"
, headers = headers sessId
, url = path
}
decodeResponse res = Decode.decodeString decoder -- ...handle error etc
in
Ports.apiSession `andThen` req `andThen` decodeResponse
Elm.Main.ports.apiSession = function(){
return new Promise(function(resolve,reject){
var sess = localStorage.getItem("sessionId");
if(!sess) reject("NO_SESSION");
else resolve(sess);
});
}
var app = Elm.Main.fullscreen();
Elm.Main.ports.apiSession = function (value, cb) {
var sess = localStorage.getItem(value);
if (!sess) {
cb("NO_SESSION"); // first argument error
} else {
cb(null, sess);
}
}
port apiSession: String -> Task Decode.Value Decode.Value
regular ports only let you send data off or receive data back, not both.
But isn't that covered by the error handling of tasks? The same way as when you use the "built-in" tasks like Http? Http gets around the general issue of "what happens if this never returns" using the Http.Error value Timeout. Maybe something similar could be used for Task Ports.
App.ports.myTaskFunc = function(val){
return (val + 2) //equivalent to returning a Promise that is resolved to (val+2) immediately
}
App.ports.myTaskFunc = function(val){
return new Promise(function(resolve){
resolve(val+2)
reject("err") // this is ignored since we've already resolved.
});
}
App.ports.myTaskFunc = function(val){
return Promise.resolve(val + 2)
}
App.ports.myTaskFunc = function(val,resolve,reject){
resolve(val+2);
reject("err") // this is ignored since we've already resolved.
}
App.ports.myTaskFunc = function(val, cb) {
cb(null, val + 2);
}
Good point, I had node in the back of my mind but somehow forgot what they did!
--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/TjWoacZobWw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
port random : Int -> Task Never (List Int)
app.ports.random.handle(function (n, callback) {
var array = new Uint32Array(n);
window.crypto.getRandomValues(array);
callback(null, array);
});
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
get : (Result Error Json.Decode.Value -> msg) -> String -> Cmd msg
getTask : String -> Task Error Json.Decode.Value
port get : Json.Encode.Value -> Task Task.PortError Json.Decode.Value
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.