It looks, like I can have 0 delay with setTimeout and it works. :)

const app = Elm.Main.embed(document.querySelector('#app')\);
setTimeout(function () {
  // Elm is ready to receive data
  app.ports.loadData.send(window.__DATA);
}, 0);


On Tuesday, June 7, 2016 at 1:57:00 AM UTC+1, Jaroslaw Zabiello wrote:
>
> I need to import some initial data from JS to Elm as fast as possible, 
> just after Elm app is embed to HTML page. The problem is I don't know how 
> much time I need for Elm to be ready for port communication from JS to Elm. 
> Definitely it can't be done just after mounting Elm to the page. 
>
> The one solution would be delaying the transmission with SetTimeout like:
>
>
> const elmDiv = document.querySelector('#app');
> const elmApp = Elm.Main.embed(elmDiv);
>
> setTimeout(function () {
>   elmApp.ports.loadData.send(window.__DATA);
> }, 1000);
>
>
> But I don't like to use  arbitrary, artificial delay in the communication.
>
> The another solution would be sending IsReady message from Elm to JS 
> first, subscribe to it, and from the subscription start sending data back 
> to Elm. 
>
> elmApp.ports.isReady.subscribe(function() {
>   elmApp.ports.loadData.send(window.__DATA);
> });
>
> Is there any other way? A kind of callback to notify Elm is mounted and 
> ready for receiving data?
>
> Elm.Main.embed(elmDiv, function () {
>    // Elm is ready for receiving data from JS
>    this.port.loadData.send(myData);
> })
>
>

-- 
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.

Reply via email to