KEMBAR78
Forward JS 2017 | SF | Write applications as State Machines | PDF
Use Elm and reuse it everywhere
@udnisap
§ The Problem
§ Model Applications as state machines
§ Extract application logic as an engine
§ Use ELM to code the application logic
§ Turn based real time card game
§ Offline
§ Online multiplayer
§ Multiple Clients
§ Android/IOS
§ Web
§ Reuse Logic
§ Plan for change
UI
Business Logic
/Engine
Persistence
Cloud Persistence
Business Logic
IOS
Business Logic
Persistence
Android
Business Logic
Persistence
Web
Business Logic
Persistence
Bot
Business Logic
Persistence
§ No Side effects
§ No Mutations (Observable)
§ Returns the same result
§ Explicit Dependencies
§ Clean
§ Understandable
§ Testable
§ Can be easily portable to any
framework
§ Takes Function as an arguments and data structure
§ Operates on the data structure
§ Examples
§ Map
§ Reduce
§ Takes a function
§ Takes an initial value
§ Run the function on the list
for each elements
§ Return the final value
ITS NOT GOING TO WORK
§ Caller needs to provide everything for the method
§ Dependencies
§ Relevant Data structures
§ Callee ask everything it needs from the caller
§ We need a wrapper around these pure functions that
encapsulate STATE
Can we actually write the application with functions?
State
- todo : [] // list of todos
- filter :‘all’ // current active filter
Actions as effects
- addTodo
- ChangeFilter
- MarkTodo
todos: [
{
name: ‘learn js’,
status: ‘todo’
}],
filter: ‘all’
todos: [],
filter: ‘all’
NewTodo
‘learn js’
todos: [
{
name: ‘learn js’,
status: ‘complete’
}],
filter: ‘all’
ChangeFilter
‘todo’
todos: [
{
name: ‘learn js’,
status: ‘todo’
}],
filter: ‘todo’
todos: [
{
name: ‘learn js’,
status: ‘complete’
}],
filter: ‘todo’
MarkTodo
‘learn js’
‘complete’
ChangeFilter
‘todo’
MarkTodo
‘learn js’
‘complete’
Current
State
Action
Engine
Next
State
Initial
State
Action1 Action2 Action3
Action1 Action2 Action3
Initial
State
State 1 State 2
Engine
Action1 Action2 Action3
Initial
State
State 1 State 2
Engine
Engine
Initial
State
Action1 Action2 Action3
§ A JS module that provides an interface to compute states
based on actions
§ All your business/core logic is in that transition function
§ It does not depends on any framework
§ All those are pure functions.
§ Next state can be calculated just by passing the current state
with the action
§ It can be done on the server / client
§ All client are self sufficient to compute the next state
§ Works offline
ServerClient
UI
Engine Engine
A delightful language for reliable webapps.
Generate JavaScript with great performance and no
runtime exceptions.
§ Functional
§ Immutable
§ Pure
§ Statically Typed
§ Performance
§ Javascript Interop
§ NO SUPPRISES à NO RUNTIME EXCEPTIONS
§ Exceptional Compiler support for errors
§ Mostly used a view layer
§ UI Rendering performant than React
§ But it is best suited with the logic
§ Write an Business logic engine in Elm
§ Provide interface to Javascript to be used in other
applications
§ Export it as a NPM module
JS Wrapper
• initialize(state)
• fireAction(action)
• subscribe(function(state){
//update
})
•
Incoming
Port
FireAction
getState
Outgoing
Port
onError
OnSuccess
Encoders/
Decoders
Elm
Application
§ Statically typed
§ Immutable
§ No NULL values
§ No Undefined
§ Card can only be of the given
types
§ Its not like string
§ Parameterized Types
§ Convert ELM to JSON
§ Throws errors at the gate
§ Can combine encoders
Convert JSON to elm objects
§ Throws errors at the gate
§ Can combine
§ Elm compiles to ES5
§ Import as a normal module
§ Export with the wrapper
§ Structure application as a state machine
§ Modularize it to a single package
§ Hydrate and De-hydrate the engine when needed
§ To run the app offline
§ To do optimistic updates
§ Write the engine/core logic in Elm
§ Compile to JS and export as a module.
§ Use encoders and decoders to convert messages and state though ports
§ Make Impossible states impossible
https://www.youtube.com/watch?v=IcgmSRJHu_8
§ Use Effects as Data
https://www.youtube.com/watch?v=6EdXaWfoslc
Forward JS 2017 | SF | Write applications as State Machines
Forward JS 2017 | SF | Write applications as State Machines

Forward JS 2017 | SF | Write applications as State Machines

  • 1.
    Use Elm andreuse it everywhere
  • 2.
  • 3.
    § The Problem §Model Applications as state machines § Extract application logic as an engine § Use ELM to code the application logic
  • 4.
    § Turn basedreal time card game § Offline § Online multiplayer § Multiple Clients § Android/IOS § Web § Reuse Logic § Plan for change
  • 5.
  • 6.
    Cloud Persistence Business Logic IOS BusinessLogic Persistence Android Business Logic Persistence Web Business Logic Persistence Bot Business Logic Persistence
  • 8.
    § No Sideeffects § No Mutations (Observable) § Returns the same result § Explicit Dependencies § Clean § Understandable § Testable § Can be easily portable to any framework
  • 9.
    § Takes Functionas an arguments and data structure § Operates on the data structure § Examples § Map § Reduce
  • 11.
    § Takes afunction § Takes an initial value § Run the function on the list for each elements § Return the final value
  • 13.
  • 14.
    § Caller needsto provide everything for the method § Dependencies § Relevant Data structures § Callee ask everything it needs from the caller § We need a wrapper around these pure functions that encapsulate STATE
  • 15.
    Can we actuallywrite the application with functions?
  • 16.
    State - todo :[] // list of todos - filter :‘all’ // current active filter Actions as effects - addTodo - ChangeFilter - MarkTodo
  • 17.
    todos: [ { name: ‘learnjs’, status: ‘todo’ }], filter: ‘all’ todos: [], filter: ‘all’ NewTodo ‘learn js’ todos: [ { name: ‘learn js’, status: ‘complete’ }], filter: ‘all’ ChangeFilter ‘todo’ todos: [ { name: ‘learn js’, status: ‘todo’ }], filter: ‘todo’ todos: [ { name: ‘learn js’, status: ‘complete’ }], filter: ‘todo’ MarkTodo ‘learn js’ ‘complete’ ChangeFilter ‘todo’ MarkTodo ‘learn js’ ‘complete’
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
    § A JSmodule that provides an interface to compute states based on actions § All your business/core logic is in that transition function § It does not depends on any framework § All those are pure functions.
  • 23.
    § Next statecan be calculated just by passing the current state with the action § It can be done on the server / client § All client are self sufficient to compute the next state § Works offline
  • 24.
  • 25.
    A delightful languagefor reliable webapps. Generate JavaScript with great performance and no runtime exceptions.
  • 26.
    § Functional § Immutable §Pure § Statically Typed § Performance § Javascript Interop § NO SUPPRISES à NO RUNTIME EXCEPTIONS § Exceptional Compiler support for errors
  • 27.
    § Mostly useda view layer § UI Rendering performant than React § But it is best suited with the logic
  • 28.
    § Write anBusiness logic engine in Elm § Provide interface to Javascript to be used in other applications § Export it as a NPM module
  • 29.
    JS Wrapper • initialize(state) •fireAction(action) • subscribe(function(state){ //update }) • Incoming Port FireAction getState Outgoing Port onError OnSuccess Encoders/ Decoders Elm Application
  • 30.
    § Statically typed §Immutable § No NULL values § No Undefined
  • 31.
    § Card canonly be of the given types § Its not like string
  • 32.
  • 33.
    § Convert ELMto JSON § Throws errors at the gate § Can combine encoders
  • 34.
    Convert JSON toelm objects § Throws errors at the gate § Can combine
  • 35.
    § Elm compilesto ES5 § Import as a normal module § Export with the wrapper
  • 36.
    § Structure applicationas a state machine § Modularize it to a single package § Hydrate and De-hydrate the engine when needed § To run the app offline § To do optimistic updates
  • 37.
    § Write theengine/core logic in Elm § Compile to JS and export as a module. § Use encoders and decoders to convert messages and state though ports § Make Impossible states impossible https://www.youtube.com/watch?v=IcgmSRJHu_8 § Use Effects as Data https://www.youtube.com/watch?v=6EdXaWfoslc