@@ -32,43 +32,59 @@ export type AsyncTask = Task & {
3232 _zone : Zone ;
3333} ;
3434
35+ // Delay of 50 ms to reset currentEventTracingZone.
36+ const RESET_TRACING_ZONE_DELAY = 50 ;
37+
3538export class InteractionTracker {
36- constructor ( ) {
39+ // Allows to track several events triggered by the same user interaction in the right Zone.
40+ private currentEventTracingZone ?: Zone ;
3741
42+ constructor ( ) {
3843 const runTask = Zone . prototype . runTask ;
39- Zone . prototype . runTask = function (
44+ Zone . prototype . runTask = (
4045 task : AsyncTask ,
4146 applyThis : unknown ,
4247 applyArgs : unknown
43- ) {
48+ ) => {
4449 const time = Date . now ( ) ;
4550
4651 console . warn ( 'Running task' ) ;
52+ console . log ( task ) ;
4753 console . log ( task . zone ) ;
4854
49- let taskZone = this ;
55+ let taskZone = Zone . current ;
5056 if ( isTrackedElement ( task ) ) {
5157 console . log ( 'Click detected' ) ;
5258
53- const traceId = randomTraceId ( ) ;
54- const tracingZone = Zone . root . fork ( {
55- name : traceId ,
56- properties : {
57- isTracingZone : true ,
58- traceId,
59- } ,
60- } ) ;
59+ if ( this . currentEventTracingZone === undefined ) {
60+ const traceId = randomTraceId ( ) ;
61+ this . currentEventTracingZone = Zone . root . fork ( {
62+ name : traceId ,
63+ properties : {
64+ isTracingZone : true ,
65+ traceId,
66+ } ,
67+ } ) ;
68+
69+ // Timeout to reset currentEventTracingZone to allow the creation of a new
70+ // zone for a new user interaction.
71+ Zone . root . run ( ( ) =>
72+ setTimeout (
73+ ( ) => ( this . currentEventTracingZone = undefined ) ,
74+ RESET_TRACING_ZONE_DELAY
75+ )
76+ ) ;
77+
78+ console . log ( 'New zone:' ) ;
79+ console . log ( this . currentEventTracingZone ) ;
80+ }
6181
6282 // Change the zone task.
63- task . _zone = tracingZone ;
64- taskZone = tracingZone ;
65- console . log ( 'New zone:' ) ;
66- console . log ( taskZone ) ;
67- } else {
83+ task . _zone = this . currentEventTracingZone ;
84+ taskZone = this . currentEventTracingZone ;
85+ } else if ( task . zone && task . zone . get ( 'isTracingZone' ) ) {
6886 // If we already are in a tracing zone, just run the task in our tracing zone.
69- if ( task . zone && task . zone . get ( 'isTracingZone' ) ) {
70- taskZone = task . zone ;
71- }
87+ taskZone = task . zone ;
7288 }
7389 try {
7490 return runTask . call ( taskZone as { } , task , applyThis , applyArgs ) ;
0 commit comments