Skip to content

Commit a04cb3c

Browse files
committed
Add Javascript template code
1 parent 7932557 commit a04cb3c

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

packages/cycle-scripts/template/config/javascript.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ module.exports = {
22
xstream: {
33
run: '@cycle/run',
44
import: 'import xs from \'xstream\'',
5-
stream: 'xs'
5+
stream: 'xs',
6+
fold: 'fold',
7+
merge: 'xs.merge',
8+
mapTo: 'mapTo'
69
},
710
rxjs: {
811
run: '@cycle/rxjs-run',
912
import: 'import Rx from \'rxjs/Rx\'',
10-
stream: 'Rx.Observable'
13+
stream: 'Rx.Observable',
14+
fold: 'scan',
15+
merge: 'Rx.Observable.merge',
16+
mapTo: 'mapTo'
1117
},
1218
most: {
1319
run: '@cycle/most-run',
1420
import: 'import * as most from \'most\'',
15-
stream: 'most'
21+
stream: 'most',
22+
fold: 'scan',
23+
merge: 'most.merge',
24+
mapTo: 'constant'
1625
}
1726
}
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
module.exports = replacements => `${replacements.import}
22
33
export function App (sources) {
4-
const vtree$ = ${replacements.stream}.of(
5-
<div>My Awesome Cycle.js app</div>
6-
)
4+
const action$ = intent(sources.DOM)
5+
const model$ = model(action$)
6+
const vdom$ = view(model$)
7+
78
const sinks = {
8-
DOM: vtree$
9+
DOM: vdom$
910
}
1011
return sinks
1112
}
13+
14+
const initalState = { count: 0 }
15+
16+
function intent(DOM) {
17+
const add$ = DOM.select('.add').events('click')
18+
.${replacements.mapTo}(prevState => ({ ...prevState, count: prevState.count + 1 }))
19+
20+
const subtract$ = DOM.select('.subtract').events('click')
21+
.${replacements.mapTo}(prevState => ({ ...prevState, count: prevState.count - 1 }))
22+
23+
return ${replacements.merge}(add$, subtract$)
24+
}
25+
26+
function model(action$) {
27+
return action$
28+
.${replacements.fold}((state, reducer) => reducer(state), initalState)
29+
}
30+
31+
function view(state$) {
32+
return state$
33+
.map(s => s.count)
34+
.map(count =>
35+
<div>
36+
<h2>My Awesome Cycle.js app</h2>
37+
<span>{ 'Counter: ' + count }</span>
38+
<button type='button' className='add'>Increase</button>
39+
<button type='button' className='subtract'>Decrease</button>
40+
</div>
41+
)
42+
}
1243
`

0 commit comments

Comments
 (0)