introducing elm to a js app€¦ · 2 billion questions answered 100,000 lines of elm code 0...

76
Introducing Elm to a JS App @rtfeldman

Upload: others

Post on 19-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Introducing Elm to a JS App

@rtfeldman

Page 2: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

2014 201620152013

Page 3: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

2 billion questions answered

100,000 lines of elm code

0 runtime exceptions

IN PRODUCTION

Page 4: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

compiles to JavaScript

functional programming language

DIFFERENT SEMANTICS

Page 5: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

no null or undefinedall values are immutablefunctions have no side effects

GUARANTEES

Page 6: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm in productionGOAL

Page 7: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

lots of JS in production

STATUS QUO

Page 8: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

lots of JS in production

elm in production

Page 9: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

INTEROP

Page 10: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

MAINTAIN GUARANTEES

while interoperating with JS?

how can Elm

Page 11: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION
Page 12: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Ellie

Page 13: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm forRENDERING

Page 14: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Page

Elm App JS App

Page 15: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 16: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 17: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION
Page 18: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION
Page 19: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

lots of JS in production

elm in production

Page 20: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

PLANTA SEED

WATCH ITGROW

Page 21: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

the elm architecture

INTERACTIVITY

Page 22: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

VIEW FUNCTION

ARGUMENTS → VIRTUAL DOM

Page 23: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

Page 24: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Interoperating with JavaScript

Elm code talks to JavaScriptthe way it talks to servers...by sending and receiving data!

Page 25: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.js

Page 26: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.ports.dataFromElm.subscribe(

Page 27: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.ports.dataFromElm.subscribe((data) =>

Page 28: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.ports.dataFromElm.subscribe((data) => { console.log("Elm says: " + data);});

Page 29: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm App

Cmd

JS App

Page 30: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

JS App

"hi!"

Page 31: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

JS App

"hi!"

"hi!"

Page 32: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.ports.dataFromElm.subscribe((data) => { console.log("Elm says: " + data);}); "hi!"

Page 33: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.MyApp.embed(elem);

app.ports.dataFromElm.subscribe((data) => { // we can do whatever we want here!});

Page 34: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Page

Elm App JS App

Page 35: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 36: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

JS

Page 37: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm app

JS App

Page 38: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Sending datafrom JS to Elm

Page 39: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

Initialization Flags

var elem = document.getElementById("elm-app");var app = Elm.Main.embed(elem, "yo!");

viewupdate Model

Page 40: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Back-and-forthcommunication

Page 41: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

app.js

var elem = document.getElementById("elm-app");var app = Elm.Main.embed(elem);

app.ports.dataToElm.send("hey!");

Page 42: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

Page 43: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

subscriptions

Page 44: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

JS App

subscriptions

"hey!"

Page 45: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Elm App JS Appdata

Page 46: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Page

Elm App JS App

Page 47: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm app

JS App

Page 48: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

var menu = Elm.Menu.embed(menuElem);var sidebar = Elm.Sidebar.embed(sbElem);

var footer = Elm.Footer.embed(footerElem);

MULTIPLE EMBEDSapp.js

Page 49: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Page

Elm App

JS App

Elm App Elm App

Page 50: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

lazy-loaded elm app

Page 51: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 52: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 53: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm

Page 54: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm forBUSINESS LOGIC

Page 55: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

// app.ports.foo.send( … )// app.ports.bar.subscribe( … )

app.js

var app = Elm.MyApp.worker();

Page 56: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

DOM

Elm App JS App

Page 57: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

elm forDROP-IN COMPONENTS

Page 58: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

import Elm from 'react-elm-components';import {Chat} from '../dist/elm/chatroom.js';function render() { return <Elm src={Chat} />;}

app.jsx

Page 59: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

var React = require('react');module.exports = React.createClass({ initialize: function(node) { if (node === null) return;

var app = this.props.src.embed(node, this.props.flags);

if (typeof this.props.ports !== 'undefined') { this.props.ports(app.ports); }

}, shouldComponentUpdate: function(prevProps) { return false; }, render: function () { return React.createElement('div', { ref: this.initialize }); }});

react-elm-components

Page 60: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

JS Wrapper Component

Elm App

Page 61: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

the mostPOWERFUL IDEA

Page 62: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

how far can weRUN WITH THIS?

Page 63: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

data

Page 64: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

viewupdate Model

Msg VDOM

Elm Runtime

Cmd

Elm App JS Appdata

Page 65: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

here's what I want you to do

here's what I want you to know

Page 66: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

Elm App JS Appdata

{ "type": "FOLLOW_USER", "payload": "rtfeldman"}

GUARANTEES INTACT

Page 67: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

guide.elm-lang.org

RESOURCES

How to Use Elm at Workelm-lang.org/blog

Page 68: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

pragmaticstudio.com

Page 69: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION
Page 70: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

frontendmasters.com

Page 71: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

humblespark.comElm Training

Luke Westbycreator of Ellie

Page 72: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

lots of JS in production

elm in production

Page 73: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION
Page 74: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

PLANTA SEED

Page 75: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

WATCH IT GROW

Page 76: Introducing Elm to a JS App€¦ · 2 billion questions answered 100,000 lines of elm code 0 runtime exceptions IN PRODUCTION

@rtfeldman