@@ -496,12 +496,20 @@ namespace microcode {
496496 }
497497
498498 private switchPage ( page : number ) {
499+ // need to make sure outstanding events are dropped and no new events
500+ this . eventQueue = [ ]
501+ this . allowEvents = false
502+ // now stop existing rules
499503 this . stopAllRules ( )
500504 // set up new rule closures
501505 this . currentPage = page
502506 this . program . pages [ this . currentPage ] . rules . forEach ( ( r , index ) => {
503507 this . ruleClosures . push ( new RuleClosure ( index , r , this ) )
504508 } )
509+ // start up timer-based rules (should these be events as well?)
510+ this . ruleClosures . forEach ( rc => rc . start ( true ) )
511+ // restart events
512+ this . allowEvents = true
505513 this . addEvent ( {
506514 kind : MicroCodeEventKind . StartPage ,
507515 } as StartPageEvent )
@@ -510,15 +518,15 @@ namespace microcode {
510518 kind : MicroCodeEventKind . StateUpdate ,
511519 updatedVars : Object . keys ( vars2tids ) ,
512520 } as StateUpdateEvent )
513- // start up timer-based rules
514- this . ruleClosures . forEach ( rc => rc . start ( true ) )
515521 }
516522
517523 public runAction ( ruleIndex : number , action : Tile , param : any ) {
518524 switch ( action ) {
519525 case Tid . TID_ACTUATOR_SWITCH_PAGE :
526+ // no switch if no param
520527 if ( param ) {
521- // no switch if no param
528+ // when switching, drop any outstanding events
529+ this . eventQueue = [ ]
522530 this . addEvent ( {
523531 kind : MicroCodeEventKind . SwitchPage ,
524532 index : param ,
@@ -619,10 +627,11 @@ namespace microcode {
619627 } )
620628 }
621629
630+ private allowEvents = false
622631 private eventQueueActive = false
623632 private eventQueue : MicroCodeEvent [ ] = [ ]
624633 public addEvent ( event : MicroCodeEvent ) {
625- if ( ! this . running ) return
634+ if ( ! this . running || ! this . allowEvents ) return
626635 this . eventQueue . push ( event )
627636 }
628637
0 commit comments