-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
component function passes start as parameter, update examples accordi…
…ngly
- Loading branch information
Showing
10 changed files
with
326 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
import { Behavior, accum, Stream, combine } from "@funkia/hareactive"; | ||
import { elements, fgo, component } from "../../../src"; | ||
import { accum, Stream, combine } from "@funkia/hareactive"; | ||
import { elements, component } from "../../../src"; | ||
const { div, button } = elements; | ||
|
||
type On = { | ||
incrementClick: Stream<any>; | ||
decrementClick: Stream<any>; | ||
}; | ||
|
||
const counter = component<On>( | ||
fgo(function*({ incrementClick, decrementClick }) { | ||
const increment = incrementClick.mapTo(1); | ||
const decrement = decrementClick.mapTo(-1); | ||
const changes = combine(increment, decrement); | ||
const count = yield accum((n, m) => n + m, 0, changes); | ||
const counter = component<On>((on, start) => { | ||
const increment = on.incrementClick.mapTo(1); | ||
const decrement = on.decrementClick.mapTo(-1); | ||
const changes = combine(increment, decrement); | ||
const count = start(accum((n, m) => n + m, 0, changes)); | ||
|
||
return div([ | ||
"Counter ", | ||
count, | ||
" ", | ||
button({ class: "btn btn-default" }, " + ").use({ | ||
incrementClick: "click" | ||
}), | ||
" ", | ||
button({ class: "btn btn-default" }, " - ").use({ | ||
decrementClick: "click" | ||
}) | ||
]); | ||
}) | ||
); | ||
return div([ | ||
"Counter ", | ||
count, | ||
" ", | ||
button({ class: "btn btn-default" }, " + ").use({ | ||
incrementClick: "click" | ||
}), | ||
" ", | ||
button({ class: "btn btn-default" }, " - ").use({ | ||
decrementClick: "click" | ||
}) | ||
]); | ||
}); | ||
|
||
export const main2 = counter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,56 @@ | ||
import { | ||
Behavior, | ||
combine, | ||
map, | ||
accum, | ||
scan, | ||
Stream | ||
} from "@funkia/hareactive"; | ||
import { Behavior, map, accum, scan, Stream } from "@funkia/hareactive"; | ||
|
||
import { elements, fgo, list, component } from "../../../src"; | ||
const { br, div, button, h1, ul } = elements; | ||
import { elements, list, component } from "../../../src"; | ||
const { br, li, button, h1, ul } = elements; | ||
|
||
const add = (n: number, m: number) => n + m; | ||
const apply = <A, B>(f: (a: A) => B, a: A) => f(a); | ||
|
||
type CounterModelInput = { | ||
incrementClick: Stream<any>; | ||
decrementClick: Stream<any>; | ||
type CounterOn = { | ||
delta: Stream<number>; | ||
}; | ||
|
||
type CounterOutput = { | ||
count: Behavior<number>; | ||
}; | ||
|
||
const counter = () => | ||
component<CounterModelInput, CounterOutput>( | ||
fgo(function*(on) { | ||
const increment = on.incrementClick.mapTo(1); | ||
const decrement = on.decrementClick.mapTo(-1); | ||
const count = yield accum(add, 0, combine(increment, decrement)); | ||
|
||
return div([ | ||
"Counter ", | ||
count, | ||
" ", | ||
button({ class: "btn btn-default" }, " + ").use({ | ||
incrementClick: "click" | ||
}), | ||
" ", | ||
button({ class: "btn btn-default" }, " - ").use({ | ||
decrementClick: "click" | ||
}) | ||
]).output({ count }); | ||
}) | ||
); | ||
component<CounterOn, CounterOutput>((on, start) => { | ||
const count = start(accum(add, 0, on.delta)); | ||
|
||
return li([ | ||
"Counter ", | ||
count, | ||
" ", | ||
button({ class: "btn btn-default" }, " + ").use((o) => ({ | ||
delta: o.click.mapTo(1) | ||
})), | ||
" ", | ||
button({ class: "btn btn-default" }, " - ").use((o) => ({ | ||
delta: o.click.mapTo(-1) | ||
})) | ||
]).output({ count }); | ||
}); | ||
|
||
type ListOn = { | ||
addCounter: Stream<Event>; | ||
}; | ||
|
||
const counterList = component<ListOn>( | ||
fgo(function*({ addCounter }) { | ||
const nextId: Stream<number> = yield scan(add, 2, addCounter.mapTo(1)); | ||
const appendCounterFn = map( | ||
(id) => (ids: number[]) => ids.concat([id]), | ||
nextId | ||
); | ||
const counterIds = yield accum<(a: number[]) => number[], number[]>( | ||
apply, | ||
[0], | ||
appendCounterFn | ||
); | ||
return [ | ||
h1("Counters"), | ||
button({ class: "btn btn-primary" }, "Add counter").use({ | ||
addCounter: "click" | ||
}), | ||
br, | ||
ul(list(counter, counterIds)) | ||
]; | ||
}) | ||
); | ||
const counterList = component<ListOn>(({ addCounter }, start) => { | ||
const nextId = start(scan(add, 2, addCounter.mapTo(1))); | ||
const appendCounterFn = map( | ||
(id) => (ids: number[]) => ids.concat([id]), | ||
nextId | ||
); | ||
const counterIds = start(accum(apply, [0], appendCounterFn)); | ||
return [ | ||
h1("Counters"), | ||
button({ class: "btn btn-primary" }, "Add counter").use({ | ||
addCounter: "click" | ||
}), | ||
br, | ||
ul(list(counter, counterIds)) | ||
]; | ||
}); | ||
|
||
export const main3 = counterList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.