|
1 | 1 | module.exports = replacements => `${replacements.import}
|
2 |
| -import {Sources, Sinks} from './interfaces' |
| 2 | +${replacements.typeImport} |
| 3 | +import { DOMSource, VNode } from '@cycle/dom' |
| 4 | +import { Sources, Sinks, Reducer } from './interfaces' |
3 | 5 |
|
4 |
| -export function App(sources : Sources) : Sinks { |
5 |
| - const vtree$ = ${replacements.stream}.of( |
6 |
| - <div>My Awesome Cycle.js app</div> |
7 |
| - ) |
| 6 | +export type AppState = { |
| 7 | + count : number; |
| 8 | +} |
| 9 | +export type AppReducer = (prevState : AppState) => AppState |
| 10 | +
|
| 11 | +export function App({ DOM } : Sources) : Sinks { |
| 12 | + const action$ : ${replacements.streamType}<AppReducer> = intent(DOM) |
| 13 | + const model$ : ${replacements.streamType}<AppState> = model(action$) |
| 14 | + const vdom$ : ${replacements.streamType}<VNode> = view(model$) |
8 | 15 |
|
9 | 16 | return {
|
10 |
| - DOM: vtree$ |
| 17 | + DOM: vdom$ |
11 | 18 | }
|
12 | 19 | }
|
| 20 | +
|
| 21 | +const initalState : AppState = { count: 0 } |
| 22 | +
|
| 23 | +function intent(DOM : DOMSource) : ${replacements.streamType}<AppReducer> { |
| 24 | + const add$ = DOM.select('.add').events('click') |
| 25 | + .${replacements.mapTo}(prevState => ({ ...prevState, count: prevState.count + 1 })) |
| 26 | +
|
| 27 | + const subtract$ = DOM.select('.subtract').events('click') |
| 28 | + .${replacements.mapTo}(prevState => ({ ...prevState, count: prevState.count - 1 })) |
| 29 | +
|
| 30 | + return ${replacements.merge}(add$, subtract$) |
| 31 | +} |
| 32 | +
|
| 33 | +function model(action$ : ${replacements.streamType}<AppReducer>) : ${replacements.streamType}<AppState> { |
| 34 | + return action$ |
| 35 | + .${replacements.fold}((state, reducer) => reducer(state), initalState) |
| 36 | +} |
| 37 | +
|
| 38 | +function view(state$ : ${replacements.streamType}<AppState>) : ${replacements.streamType}<VNode> { |
| 39 | + return state$ |
| 40 | + .map(s => s.count) |
| 41 | + .map(count => |
| 42 | + <div> |
| 43 | + <h2>My Awesome Cycle.js app</h2> |
| 44 | + <span>{ 'Counter: ' + count }</span> |
| 45 | + <button type='button' className='add'>Increase</button> |
| 46 | + <button type='button' className='subtract'>Decrease</button> |
| 47 | + </div> |
| 48 | + ) |
| 49 | +} |
13 | 50 | `
|
0 commit comments