简体   繁体   中英

Elm Signals in Vanilla JavaScript?

I know it's possible to implement signals / observables / streams in vanilla JS, because Elm compiles back to JS.

Can anyone off an example of a signal translated to JS? It must use a callback structure right? For example, what does Elm translate this to?

    main =
       Signal.map show Mouse.position

In a very crude way you could think of this as equivalent to:

document.onmousemove = handleMouseMove;

function handleMouseMove(event) {
    //...extract x and y from event somehow
    show([x, y])
}

But there is a lot more happening in Elm, it is a lot more similar to observable objects in RXjs https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md

The simple code you put does not translate to a simple callback structure in JS. The Elm runtime is a little bit more complex than that. However, if you really want to get a better understanding, you can take a look at the Native implementation of Signal .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM