@@ -62,6 +62,10 @@ export interface ISessionMiddleware {
62
62
( session : Session , next : Function ) : void ;
63
63
}
64
64
65
+ export interface IWatchableHandler {
66
+ ( context : IRecognizeContext , callback : ( err : Error , value : any ) => void ) : void ;
67
+ }
68
+
65
69
export class Session extends events . EventEmitter {
66
70
private msgSent = false ;
67
71
private _isReset = false ;
@@ -89,6 +93,7 @@ export class Session extends events.EventEmitter {
89
93
userData : this . userData ,
90
94
conversationData : this . conversationData ,
91
95
privateConversationData : this . privateConversationData ,
96
+ dialogData : this . dialogData ,
92
97
localizer : this . localizer ,
93
98
logger : this . logger ,
94
99
dialogStack : ( ) => { return this . dialogStack ( ) ; } ,
@@ -603,6 +608,62 @@ export class Session extends events.EventEmitter {
603
608
}
604
609
}
605
610
611
+ //-----------------------------------------------------
612
+ // Watch Statements
613
+ //-----------------------------------------------------
614
+
615
+ /** Enables/disables the watch statment for a given variable. */
616
+ public watch ( variable : string , enable = true ) : this {
617
+ let name = variable . toLowerCase ( ) ;
618
+ if ( ! this . userData . hasOwnProperty ( consts . Data . DebugWatches ) ) {
619
+ this . userData [ consts . Data . DebugWatches ] = { } ;
620
+ }
621
+ if ( watchableHandlers . hasOwnProperty ( name ) ) {
622
+ var entry = watchableHandlers [ name ] ;
623
+ this . userData [ consts . Data . DebugWatches ] [ entry . name ] = enable ;
624
+ } else {
625
+ throw new Error ( "Invalid watch statement. '" + variable + " isn't watchable" ) ;
626
+ }
627
+ return this ;
628
+ }
629
+
630
+ /** Returns the list of enabled watch statements for the session. */
631
+ public watchList ( ) : string [ ] {
632
+ var watches : string [ ] = [ ] ;
633
+ if ( this . userData . hasOwnProperty ( consts . Data . DebugWatches ) ) {
634
+ for ( let name in this . userData [ consts . Data . DebugWatches ] ) {
635
+ if ( this . userData [ consts . Data . DebugWatches ] [ name ] ) {
636
+ watches . push ( name ) ;
637
+ }
638
+ }
639
+ }
640
+ return watches ;
641
+ }
642
+
643
+ /** Adds or retrieves a watchable variable from the session. */
644
+ static watchable ( variable : string , handler ?: IWatchableHandler ) : IWatchableHandler {
645
+ if ( handler ) {
646
+ watchableHandlers [ variable . toLowerCase ( ) ] = { name : variable , handler : handler } ;
647
+ } else {
648
+ let entry = watchableHandlers [ variable . toLowerCase ( ) ] ;
649
+ if ( entry ) {
650
+ handler = entry . handler ;
651
+ }
652
+ }
653
+ return handler ;
654
+ }
655
+
656
+ /** Returns the list of watchable variables. */
657
+ static watchableList ( ) : string [ ] {
658
+ let variables : string [ ] = [ ] ;
659
+ for ( let name in watchableHandlers ) {
660
+ if ( watchableHandlers . hasOwnProperty ( name ) ) {
661
+ variables . push ( watchableHandlers [ name ] . name ) ;
662
+ }
663
+ }
664
+ return variables ;
665
+ }
666
+
606
667
//-----------------------------------------------------
607
668
// PRIVATE HELPERS
608
669
//-----------------------------------------------------
@@ -637,12 +698,36 @@ export class Session extends events.EventEmitter {
637
698
}
638
699
639
700
private onFinishBatch ( cb : Function ) : void {
640
- this . logger . flush ( ( err ) => {
641
- this . sendingBatch = false ;
701
+ // Dump watchList
702
+ var ctx = this . toRecognizeContext ( ) ;
703
+ async . each ( this . watchList ( ) , ( variable , cb ) => {
704
+ let entry = watchableHandlers [ variable ] ;
705
+ if ( entry && entry . handler ) {
706
+ try {
707
+ entry . handler ( ctx , ( err , value ) => {
708
+ if ( ! err ) {
709
+ this . logger . dump ( variable , value ) ;
710
+ }
711
+ cb ( err ) ;
712
+ } ) ;
713
+ } catch ( e ) {
714
+ cb ( e ) ;
715
+ }
716
+ } else {
717
+ cb ( new Error ( "'" + variable + "' isn't watchable." ) ) ;
718
+ }
719
+ } , ( err ) => {
720
+ // Flush logs
642
721
if ( err ) {
643
- console . error ( err ) ;
722
+ this . logger . error ( this . dialogStack ( ) , err ) ;
644
723
}
645
- cb ( ) ;
724
+ this . logger . flush ( ( err ) => {
725
+ this . sendingBatch = false ;
726
+ if ( err ) {
727
+ console . error ( err ) ;
728
+ }
729
+ cb ( ) ;
730
+ } ) ;
646
731
} ) ;
647
732
}
648
733
@@ -774,3 +859,15 @@ export class Session extends events.EventEmitter {
774
859
return this . message . sourceEvent ;
775
860
}
776
861
}
862
+
863
+ // Initialize default list of watchable variables.
864
+ let watchableHandlers : { [ name : string ] : { name : string ; handler : IWatchableHandler ; } ; } = {
865
+ 'userdata' : { name : 'userData' , handler : ( ctx , cb ) => cb ( null , ctx . userData ) } ,
866
+ 'conversationdata' : { name : 'conversationData' , handler : ( ctx , cb ) => cb ( null , ctx . conversationData ) } ,
867
+ 'privateconversationdata' : { name : 'privateConversationData' , handler : ( ctx , cb ) => cb ( null , ctx . privateConversationData ) } ,
868
+ 'dialogdata' : { name : 'dialogData' , handler : ( ctx , cb ) => cb ( null , ctx . dialogData ) } ,
869
+ 'dialogstack' : { name : 'dialogStack' , handler : ( ctx , cb ) => cb ( null , ctx . dialogStack ( ) ) } ,
870
+ 'preferredlocale' : { name : 'preferredLocale' , handler : ( ctx , cb ) => cb ( null , ctx . preferredLocale ( ) ) } ,
871
+ 'libraryname' : { name : 'libraryName' , handler : ( ctx , cb ) => cb ( null , ctx . libraryName ) }
872
+ } ;
873
+
0 commit comments