Skip to content

Instantly share code, notes, and snippets.

@tshm
Created May 6, 2016 15:40
Show Gist options
  • Select an option

  • Save tshm/1ea30b12ed183e3d5d1f77ffdc6da8a4 to your computer and use it in GitHub Desktop.

Select an option

Save tshm/1ea30b12ed183e3d5d1f77ffdc6da8a4 to your computer and use it in GitHub Desktop.

Revisions

  1. tshm created this gist May 6, 2016.
    28 changes: 28 additions & 0 deletions elm-node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /** allow to use Elm from nodejs...
    */
    const vm = require('vm')
    const fs = require('fs')

    function log( o ) {
    console.log( o )
    }

    /** loads Elm compiled javascript
    * and returns Elm object
    */
    function loadElm( path ) {
    log('exports called.')
    const data = fs.readFileSync( path )
    const context = { console, setInterval, setTimeout, setImmediate }
    vm.runInNewContext( data, context, path )
    return context.Elm
    }

    /** main
    */
    const Elm = loadElm('./elm.js')
    const app = Elm.worker( Elm.Main, {})

    app.ports.time.subscribe(log)

    log( Elm )