项目作者: gcanti

项目描述 :
A porting to TypeScript featuring fp-ts, rxjs6 and React
高级语言: TypeScript
项目地址: git://github.com/gcanti/elm-ts.git
创建时间: 2017-03-22T16:24:21Z
项目社区:https://github.com/gcanti/elm-ts

开源协议:MIT License

下载


elm-ts

A porting of The Elm Architecture to TypeScript featuring fp-ts, RxJS and React.

Installation

  1. npm i elm-ts fp-ts rxjs react

Note: fp-ts, rxjs and react are peer dependencies

Differences from Elm

  • no ports
  • React instead of virtual-dom (pluggable)
  • Navigation is based on history

React

  1. import * as React from 'elm-ts/lib/React'
  2. import { render } from 'react-dom'
  3. import * as component from './examples/Counter'
  4. const main = React.program(component.init, component.update, component.view)
  5. React.run(main, dom => render(dom, document.getElementById('app')!))

How to derive decoders from io-ts codecs

  1. import * as t from 'io-ts'
  2. import { failure } from 'io-ts/lib/PathReporter'
  3. function fromCodec<A>(codec: t.Decoder<unknown, A>): Decoder<A> {
  4. return flow(
  5. codec.decode,
  6. E.mapLeft(errors => failure(errors).join('\n'))
  7. )
  8. }

Enable debugger in development mode

For Html (and its specializations) programs:

  1. import { programWithDebugger } from 'elm-ts/lib/Debug/Html'
  2. import * as React from 'elm-ts/lib/React'
  3. import { render } from 'react-dom'
  4. import * as component from './examples/Counter'
  5. const program = process.env.NODE_ENV === 'production' ? React.program : programWithDebugger
  6. const main = program(component.init, component.update, component.view)
  7. React.run(main, dom => render(dom, document.getElementById('app')!))

For Navigation (and its specializations) programs:

  1. import { programWithDebuggerWithFlags } from 'elm-ts/lib/Debug/Navigation'
  2. import * as Navigation from 'elm-ts/lib/Navigation'
  3. import * as React from 'elm-ts/lib/React'
  4. import { render } from 'react-dom'
  5. import * as component from './examples/Navigation'
  6. const program = process.env.NODE_ENV === 'production' ? Navigation.programWithFlags : programWithDebuggerWithFlags
  7. const main = program(component.locationToMsg, component.init, component.update, component.view)
  8. React.run(main(component.flags), dom => render(dom, document.getElementById('app')!))

Stop the application

If you need to stop the application for any reason, you can use the withStop combinator:

  1. import { withStop } from 'elm-ts/lib/Html'
  2. import * as React from 'elm-ts/lib/React'
  3. import { render } from 'react-dom'
  4. import { fromEvent } from 'rxjs'
  5. import * as component from './examples/Counter'
  6. const stopSignal$ = fromEvent(document.getElementById('stop-btn'), 'click')
  7. const program = React.program(component.init, component.update, component.view)
  8. const main = withStop(stopSignal$)(program)
  9. React.run(main, dom => render(dom, document.getElementById('app')!))

The combinator takes a Program and stops consuming it when the provided Observable emits a value.

In case you want to enable the debugger, you have to use some specific functions from the Debug sub-module:

  1. // instead of `programWithDebuggerWithFlags`
  2. import { programWithDebuggerWithFlagsWithStop } from 'elm-ts/lib/Debug/Navigation'
  3. import { withStop } from 'elm-ts/lib/Html'
  4. import * as Navigation from 'elm-ts/lib/Navigation'
  5. import * as React from 'elm-ts/lib/React'
  6. import { render } from 'react-dom'
  7. import * as component from './examples/Navigation'
  8. const stopSignal$ = fromEvent(document.getElementById('stop-btn'), 'click')
  9. const program =
  10. process.env.NODE_ENV === 'production'
  11. ? Navigation.programWithFlags
  12. : programWithDebuggerWithFlagsWithStop(stopSignal$)
  13. const main = withStop(stopSignal$)(program(component.locationToMsg, component.init, component.update, component.view))
  14. React.run(main(component.flags), dom => render(dom, document.getElementById('app')!))

Examples

Documentation