2323 *
2424 */
2525
26- import { Dispatcher , Cmd , noCmd , Sub , onAnimationFrame } from 'tea-cup-core' ;
26+ import { Cmd , Dispatcher , noCmd , rafCmd , Sub } from 'tea-cup-core' ;
2727import * as React from 'react' ;
2828
2929export interface Model {
3030 readonly started : boolean ;
3131 readonly t : number ;
32+ readonly t2 : number ;
3233 readonly fps : number ;
3334 readonly animText : string ;
3435}
3536
36- export type Msg = { type : 'raf' ; t : number } | { type : 'toggle' } | { type : 'text-changed' ; text : string } ;
37+ export type Msg =
38+ | { type : 'raf' ; t : number }
39+ | { type : 'toggle' }
40+ | { type : 'text-changed' ; text : string }
41+ | { type : 'raf2' ; t : number } ;
3742
3843export function init ( ) {
3944 return noCmd < Model , Msg > ( {
4045 started : false ,
4146 t : 0 ,
47+ t2 : 0 ,
4248 fps : 0 ,
4349 animText : 'This text gets animated...' ,
4450 } ) ;
@@ -65,6 +71,9 @@ export function view(dispatch: Dispatcher<Msg>, model: Model) {
6571 />
6672 </ div >
6773 < span > Time = { Math . round ( model . t ) } </ span >
74+ < br />
75+ < span > t2 = { Math . round ( model . t2 ) } </ span >
76+ < br />
6877 < button onClick = { ( _ ) => dispatch ( { type : 'toggle' } ) } > { model . started ? 'Stop' : 'Start' } </ button >
6978 { fps }
7079 { anim }
@@ -101,18 +110,43 @@ function viewAnim(text: String, t: number) {
101110export function update ( msg : Msg , model : Model ) : [ Model , Cmd < Msg > ] {
102111 switch ( msg . type ) {
103112 case 'toggle' :
104- return noCmd ( { ...model , started : ! model . started } ) ;
105- case 'raf' :
106- const delta = msg . t - model . t ;
107- const fps = delta === 0
108- ? model . fps
109- : 1000 / delta ;
110- return noCmd ( {
113+ const newModel : Model = {
111114 ...model ,
112- t : msg . t ,
113- fps : fps ,
114- } ) ;
115+ started : ! model . started ,
116+ } ;
117+ return [
118+ newModel ,
119+ newModel . started
120+ ? Cmd . batch ( [
121+ rafCmd ( ( t : number ) => ( { type : 'raf' , t } as Msg ) ) ,
122+ rafCmd ( ( t : number ) => ( { type : 'raf2' , t } as Msg ) ) ,
123+ ] )
124+ : Cmd . none ( ) ,
125+ ] ;
126+ case 'raf' : {
127+ const delta = msg . t - model . t ;
128+ const fps = delta === 0 ? model . fps : 1000 / delta ;
129+ const cmd : Cmd < Msg > = model . started ? rafCmd ( ( t : number ) => ( { type : 'raf' , t } as Msg ) ) : Cmd . none ( ) ;
130+ return [
131+ {
132+ ...model ,
133+ t : msg . t ,
134+ fps : fps ,
135+ } ,
136+ cmd ,
137+ ] ;
138+ }
115139
140+ case 'raf2' : {
141+ const cmd : Cmd < Msg > = model . started ? rafCmd ( ( t : number ) => ( { type : 'raf2' , t } as Msg ) ) : Cmd . none ( ) ;
142+ return [
143+ {
144+ ...model ,
145+ t2 : msg . t ,
146+ } ,
147+ cmd ,
148+ ] ;
149+ }
116150 case 'text-changed' :
117151 return noCmd ( {
118152 ...model ,
@@ -121,12 +155,6 @@ export function update(msg: Msg, model: Model): [Model, Cmd<Msg>] {
121155 }
122156}
123157
124- export function subscriptions ( model : Model ) {
125- if ( model . started ) {
126- return onAnimationFrame ( ( t : number ) => {
127- return { type : 'raf' , t : t } as Msg ;
128- } ) ;
129- } else {
130- return Sub . none < Msg > ( ) ;
131- }
158+ export function subscriptions ( model : Model ) : Sub < Msg > {
159+ return Sub . none ( ) ;
132160}
0 commit comments