-
Notifications
You must be signed in to change notification settings - Fork 123
/
Ticker.kt
40 lines (31 loc) · 979 Bytes
/
Ticker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ticker
import kotlinx.browser.window
import react.*
import react.dom.*
interface TickerProps : RProps {
var startFrom: Int
}
interface TickerState : RState {
var secondsElapsed: Int
}
class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) {
override fun TickerState.init(props: TickerProps) {
secondsElapsed = props.startFrom
}
var timerID: Int? = null
override fun componentDidMount() {
timerID = window.setInterval({
// actually, the operation is performed on a state's copy, so it stays effectively immutable
setState { secondsElapsed += 1 }
}, 1000)
}
override fun componentWillUnmount() {
window.clearInterval(timerID!!)
}
override fun RBuilder.render() {
+"This app has been running for ${state.secondsElapsed} seconds."
}
}
fun RBuilder.ticker(startFrom: Int = 0) = child(Ticker::class) {
attrs.startFrom = startFrom
}