[elm-discuss] Re: Task ports: A proposal to make it easier to integrate JS with Elm.

2017-04-14 Thread Conner Ruhl
What is the process for requesting these sort of features? Is there one?

On Saturday, August 13, 2016 at 10:31:07 AM UTC-5, James Wilson wrote:
>
> The problem
>
> ports as they stand are fundamentally incompatible with Tasks. Being 
> backed by Cmd's, they are harder to compose. A frustration of mine is that 
> often we are directed to "just use ports" when a proper interface to some 
> native API is not yet available, but this leads to our Msg types growing 
> and more significant changes being required when eventually the proper 
> interface is made available.
>
> Also, many JS interop things I find myself wanting to do are fundamentally 
> one-shot functions which I expect a result back into Elm from immediately, 
> or otherwise just want to compose with other Task based things. Some 
> examples that come to mind of one-shot tasks you may want to compose rather 
> than use the streaming interface that ports provide:
>
>- Getting items from local/sessionStorage
>- .. really, most things involving working with the Web API that arent 
>yet implemented in Elm.
>- Embedding JS widgets into Elm elements
>- Using a JS library for doing things like hashing passwords or 
>obtaining some data back from some custom service
>- Interacting with things like Electron for creating apps that can run 
>in the desktop and interact with the filesystem etc.
>
>
> The solution
>
> Task ports. The idea is that these are defined the same way that Ports in 
> elm currently are, but they return a Task type rather than a Cmd or Sub 
> type. On the JS Side, we attach a function to the Elm app that returns a 
> Promise, and on the Elm side we wait for the Promise returned to reject or 
> resolve, and marhsall the error or result from the promise into the error 
> or result type required by the Task type of the port.
>
> Let's see how this might work:
>
>
> *Ports.elm:*
>
> port apiSession: Task String SessionId
>
>
>
> *Main.elm:*
>
> 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
>
>
> *App.js:*
>
> 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();
>
>
>
>
> Here, we use a tiny bit of JS to access localStorage and pull out a 
> session ID. This function is used whenever the apiRequest Task is performed 
> in Elm, and composes nicely into our apiRequest without the need for a 
> complicated effect manager or threading a sessionId through everywhere just 
> because we need to get it from a Cmd based port.
>
> One of the nice things about this is that there is minimal refactoring to 
> do for those things that do eventually receive coverage in the Elm Web API 
> - you're just swapping out Tasks for other Tasks. As the Web API will 
> always be changing, I think that having a nice way to make JS polyfills 
> like this will always have some value, let alone for interacting with 
> libraries written in JS that haven't or won't ever be ported to Elm.
>
> Elm would continue to make the same guarantees as with other ports; if the 
> task port can't marshall the response back into Elm an error would be 
> thrown along the same lines as is currently done via ports.
>
> Summary
>
> - regular ports only let you send data off or receive data back, not both.
> - Cmd's and Sub's are not composable
> - Task based ports allow you to create a new Task that is backed by JS
> - Task based ports allow for better composition and less friction when the 
> backing JS is eventually implemented in Elm.
>
> I'd love to hear what people think about this. Perhaps I'm missing some 
> big issues with the idea for instance, or maybe it's an awesome idea :) 
> What do you all think?
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Elm Visual Debugger/IDE Idea

2016-12-09 Thread Conner Ruhl
Yeah, that sounds like a fun project. Collapsing similar messages (like 
ticks) would be another small improvement.


On Wednesday, December 7, 2016 at 1:21:10 AM UTC-6, Will White wrote:
>
> Hi Conner. I'm interested in working on the debugger in this way, for 
> starters:
>
> https://github.com/elm-lang/elm-make/issues/138
>
>  Having a great visualisation of what's going on could come after, and by 
> then I'd like to help with it, but it might be just fine as it is. Would 
> you like to help with the issue I've raised above? I just wanted to make 
> you aware of it, given your interest in the debugger.
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Elm Visual Debugger/IDE Idea

2016-12-06 Thread Conner Ruhl
I'm mainly trying to see if people would find this useful or potentially 
fun to work on :)


On Tuesday, December 6, 2016 at 10:42:41 AM UTC-6, Conner Ruhl wrote:
>
> Hello!
>
> Recently I've been thinking a lot about the debugger... The potential for 
> time travel and replaying program state across browsers is absolutely 
> massive. It's been the most sellable quality of Elm in my experience.
>
> I've been on the hunt for an open source project and I'm considering 
> attempting to write a really slick visualizer based on the 0.18 debugger. 
> I'd love to see messages passing into update functions and the model 
> reacting in a highly visual way. Eventually it would be really cool if 
> there was a visualization of the entire program you could interact with to 
> make code changes.
>
> Anyway, I think the idea of prettying up the debugger into a tree-like 
> structure could be fun on its own. Could anyone see themselves using such a 
> project?
>
> Thanks
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Elm Visual Debugger/IDE Idea

2016-12-06 Thread Conner Ruhl
Hello!

Recently I've been thinking a lot about the debugger... The potential for 
time travel and replaying program state across browsers is absolutely 
massive. It's been the most sellable quality of Elm in my experience.

I've been on the hunt for an open source project and I'm considering 
attempting to write a really slick visualizer based on the 0.18 debugger. 
I'd love to see messages passing into update functions and the model 
reacting in a highly visual way. Eventually it would be really cool if 
there was a visualization of the entire program you could interact with to 
make code changes.

Anyway, I think the idea of prettying up the debugger into a tree-like 
structure could be fun on its own. Could anyone see themselves using such a 
project?

Thanks

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.