@@ -53,6 +53,39 @@ export namespace Event {
53
53
}
54
54
}
55
55
56
+ /**
57
+ * Given an event, returns another event which debounces calls and defers the listeners to a later task via a shared
58
+ * `setTimeout`. The event is converted into a signal (`Event<void>`) to avoid additional object creation as a
59
+ * result of merging events and to try prevent race conditions that could arise when using related deferred and
60
+ * non-deferred events.
61
+ *
62
+ * This is useful for deferring non-critical work (eg. general UI updates) to ensure it does not block critical work
63
+ * (eg. latency of keypress to text rendered).
64
+ *
65
+ * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
66
+ * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
67
+ * returned event causes this utility to leak a listener on the original event.
68
+ */
69
+ export function defer ( event : Event < unknown > , disposable ?: DisposableStore ) : Event < void > {
70
+ return debounce < unknown , void > ( event , ( ) => void 0 , 0 , undefined , undefined , disposable ) ;
71
+ }
72
+
73
+ /**
74
+ * Debounces an event, firing after some delay (default=0) with an array of all event original objects.
75
+ *
76
+ * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
77
+ * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
78
+ * returned event causes this utility to leak a listener on the original event.
79
+ */
80
+ export function accumulate < T > ( event : Event < T > , delay : number = 0 , disposable ?: DisposableStore ) : Event < T [ ] > {
81
+ return Event . debounce < T , T [ ] > ( event , ( last , e ) => {
82
+ if ( ! last ) {
83
+ return [ e ] ;
84
+ }
85
+ last . push ( e ) ;
86
+ return last ;
87
+ } , delay , undefined , undefined , disposable ) ;
88
+ }
56
89
57
90
/**
58
91
* Given an event, returns another event which only fires once.
0 commit comments