118
};
119
}
121
>
/**
122
>
* Given an event, returns another event which only fires once, and only when the condition is met.
123
>
*
124
>
* @param event The event source for the new event.
125
>
*/
126
>
export function onceIf<T>(event: Event<T>, condition: (e: T) => boolean): Event<T> {
127
return Event.once(Event.filter(event, condition));
128
}
130
>
/**
131
>
* Maps an event of one type into an event of another type using a mapping function, similar to how
132
>
* `Array.prototype.map` works.
133
>
*
134
>
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
135
>
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
136
>
* returned event causes this utility to leak a listener on the original event.
137
>
*
138
>
* @param event The event source for the new event.
139
>
* @param map The mapping function.
140
>
* @param disposable A disposable store to add the new EventEmitter to.
141
>
*/
142
>
export function map<I, O>(event: Event<I>, map: (i: I) => O, disposable?: DisposableStore): Event<O> {
143
return snapshot((listener, thisArgs = null, disposables?) => event(i => listener.call(thisArgs, map(i)), null, disposables), disposable);
144
}
146
>
/**
147
>
* Wraps an event in another event that performs some function on the event object before firing.
148
>
*
149
>
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
150
>
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
151
>
* returned event causes this utility to leak a listener on the original event.
152
>
*
153
>
* @param event The event source for the new event.
154
>
* @param each The function to perform on the event object.
155
>
* @param disposable A disposable store to add the new EventEmitter to.
156
>
*/
157
>
export function forEach<I>(event: Event<I>, each: (i: I) => void, disposable?: DisposableStore): Event<I> {
158
return snapshot((listener, thisArgs = null, disposables?) => event(i => { each(i); listener.call(thisArgs, i); }, null, disposables), disposable);
159
}
161
>
/**
162
>
* Wraps an event in another event that fires only when some condition is met.
163
>
*
164
>
* *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
165
>
* event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
166
>
* returned event causes this utility to leak a listener on the original event.
167
>
*
168
>
* @param event The event source for the new event.
169
>
* @param filter The filter function that defines the condition. The event will fire for the object if this function
170
>
* returns true.
171
>
* @param disposable A disposable store to add the new EventEmitter to.
172
>
*/
173
>
export function filter<T, U>(event: Event<T | U>, filter: (e: T | U) => e is T, disposable?: DisposableStore): Event<T>;
174
>
export function filter<T>(event: Event<T>, filter: (e: T) => boolean, disposable?: DisposableStore): Event<T>;
175
>
export function filter<T, R>(event: Event<T | R>, filter: (e: T | R) => e is R, disposable?: DisposableStore): Event<R>;
176
>
export function filter<T>(event: Event<T>, filter: (e: T) => boolean, disposable?: DisposableStore): Event<T> {
177
return snapshot((listener, thisArgs = null, disposables?) => event(e => filter(e) && listener.call(thisArgs, e), null, disposables), disposable);
178
}
180
>
/**
181
>
* Given an event, returns the same event but typed as `Event<void>`.
182
>
*/
183
>
export function signal<T>(event: Event<T>): Event<void> {
184
return event as Event<any> as Event<void>;
185
}
187
>
/**
188
>
* Given a collection of events, returns a single event which emits whenever any of the provided events emit.
189
>
*/
190
>
export function any<T>(...events: Event<T>[]): Event<T>;
191
>
export function any(...events: Event<any>[]): Event<void>;
192
>
export function any<T>(...events: Event<T>[]): Event<T> {
193
return (listener, thisArgs = null, disposables?) => {
194
const disposable = combinedDisposable(...events.map(event => event(e => listener.call(thisArgs, e))));