-
Notifications
You must be signed in to change notification settings - Fork 29k
/
viewModelEventDispatcher.ts
529 lines (433 loc) · 14.3 KB
/
viewModelEventDispatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ViewEventHandler } from 'vs/editor/common/viewEventHandler';
import { ViewEvent } from 'vs/editor/common/viewEvents';
import { IContentSizeChangedEvent } from 'vs/editor/common/editorCommon';
import { Emitter } from 'vs/base/common/event';
import { Selection } from 'vs/editor/common/core/selection';
import { Disposable } from 'vs/base/common/lifecycle';
import { CursorChangeReason } from 'vs/editor/common/cursorEvents';
import { IModelContentChangedEvent, IModelDecorationsChangedEvent, IModelLanguageChangedEvent, IModelLanguageConfigurationChangedEvent, IModelOptionsChangedEvent, IModelTokensChangedEvent } from 'vs/editor/common/textModelEvents';
export class ViewModelEventDispatcher extends Disposable {
private readonly _onEvent = this._register(new Emitter<OutgoingViewModelEvent>());
public readonly onEvent = this._onEvent.event;
private readonly _eventHandlers: ViewEventHandler[];
private _viewEventQueue: ViewEvent[] | null;
private _isConsumingViewEventQueue: boolean;
private _collector: ViewModelEventsCollector | null;
private _collectorCnt: number;
private _outgoingEvents: OutgoingViewModelEvent[];
constructor() {
super();
this._eventHandlers = [];
this._viewEventQueue = null;
this._isConsumingViewEventQueue = false;
this._collector = null;
this._collectorCnt = 0;
this._outgoingEvents = [];
}
public emitOutgoingEvent(e: OutgoingViewModelEvent): void {
this._addOutgoingEvent(e);
this._emitOutgoingEvents();
}
private _addOutgoingEvent(e: OutgoingViewModelEvent): void {
for (let i = 0, len = this._outgoingEvents.length; i < len; i++) {
const mergeResult = (this._outgoingEvents[i].kind === e.kind ? this._outgoingEvents[i].attemptToMerge(e) : null);
if (mergeResult) {
this._outgoingEvents[i] = mergeResult;
return;
}
}
// not merged
this._outgoingEvents.push(e);
}
private _emitOutgoingEvents(): void {
while (this._outgoingEvents.length > 0) {
if (this._collector || this._isConsumingViewEventQueue) {
// right now collecting or emitting view events, so let's postpone emitting
return;
}
const event = this._outgoingEvents.shift()!;
if (event.isNoOp()) {
continue;
}
this._onEvent.fire(event);
}
}
public addViewEventHandler(eventHandler: ViewEventHandler): void {
for (let i = 0, len = this._eventHandlers.length; i < len; i++) {
if (this._eventHandlers[i] === eventHandler) {
console.warn('Detected duplicate listener in ViewEventDispatcher', eventHandler);
}
}
this._eventHandlers.push(eventHandler);
}
public removeViewEventHandler(eventHandler: ViewEventHandler): void {
for (let i = 0; i < this._eventHandlers.length; i++) {
if (this._eventHandlers[i] === eventHandler) {
this._eventHandlers.splice(i, 1);
break;
}
}
}
public beginEmitViewEvents(): ViewModelEventsCollector {
this._collectorCnt++;
if (this._collectorCnt === 1) {
this._collector = new ViewModelEventsCollector();
}
return this._collector!;
}
public endEmitViewEvents(): void {
this._collectorCnt--;
if (this._collectorCnt === 0) {
const outgoingEvents = this._collector!.outgoingEvents;
const viewEvents = this._collector!.viewEvents;
this._collector = null;
for (const outgoingEvent of outgoingEvents) {
this._addOutgoingEvent(outgoingEvent);
}
if (viewEvents.length > 0) {
this._emitMany(viewEvents);
}
}
this._emitOutgoingEvents();
}
public emitSingleViewEvent(event: ViewEvent): void {
try {
const eventsCollector = this.beginEmitViewEvents();
eventsCollector.emitViewEvent(event);
} finally {
this.endEmitViewEvents();
}
}
private _emitMany(events: ViewEvent[]): void {
if (this._viewEventQueue) {
this._viewEventQueue = this._viewEventQueue.concat(events);
} else {
this._viewEventQueue = events;
}
if (!this._isConsumingViewEventQueue) {
this._consumeViewEventQueue();
}
}
private _consumeViewEventQueue(): void {
try {
this._isConsumingViewEventQueue = true;
this._doConsumeQueue();
} finally {
this._isConsumingViewEventQueue = false;
}
}
private _doConsumeQueue(): void {
while (this._viewEventQueue) {
// Empty event queue, as events might come in while sending these off
const events = this._viewEventQueue;
this._viewEventQueue = null;
// Use a clone of the event handlers list, as they might remove themselves
const eventHandlers = this._eventHandlers.slice(0);
for (const eventHandler of eventHandlers) {
eventHandler.handleEvents(events);
}
}
}
}
export class ViewModelEventsCollector {
public readonly viewEvents: ViewEvent[];
public readonly outgoingEvents: OutgoingViewModelEvent[];
constructor() {
this.viewEvents = [];
this.outgoingEvents = [];
}
public emitViewEvent(event: ViewEvent) {
this.viewEvents.push(event);
}
public emitOutgoingEvent(e: OutgoingViewModelEvent): void {
this.outgoingEvents.push(e);
}
}
export type OutgoingViewModelEvent = (
ContentSizeChangedEvent
| FocusChangedEvent
| ScrollChangedEvent
| ViewZonesChangedEvent
| HiddenAreasChangedEvent
| ReadOnlyEditAttemptEvent
| CursorStateChangedEvent
| ModelDecorationsChangedEvent
| ModelLanguageChangedEvent
| ModelLanguageConfigurationChangedEvent
| ModelContentChangedEvent
| ModelOptionsChangedEvent
| ModelTokensChangedEvent
);
export const enum OutgoingViewModelEventKind {
ContentSizeChanged,
FocusChanged,
ScrollChanged,
ViewZonesChanged,
HiddenAreasChanged,
ReadOnlyEditAttempt,
CursorStateChanged,
ModelDecorationsChanged,
ModelLanguageChanged,
ModelLanguageConfigurationChanged,
ModelContentChanged,
ModelOptionsChanged,
ModelTokensChanged,
}
export class ContentSizeChangedEvent implements IContentSizeChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ContentSizeChanged;
private readonly _oldContentWidth: number;
private readonly _oldContentHeight: number;
readonly contentWidth: number;
readonly contentHeight: number;
readonly contentWidthChanged: boolean;
readonly contentHeightChanged: boolean;
constructor(oldContentWidth: number, oldContentHeight: number, contentWidth: number, contentHeight: number) {
this._oldContentWidth = oldContentWidth;
this._oldContentHeight = oldContentHeight;
this.contentWidth = contentWidth;
this.contentHeight = contentHeight;
this.contentWidthChanged = (this._oldContentWidth !== this.contentWidth);
this.contentHeightChanged = (this._oldContentHeight !== this.contentHeight);
}
public isNoOp(): boolean {
return (!this.contentWidthChanged && !this.contentHeightChanged);
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return new ContentSizeChangedEvent(this._oldContentWidth, this._oldContentHeight, other.contentWidth, other.contentHeight);
}
}
export class FocusChangedEvent {
public readonly kind = OutgoingViewModelEventKind.FocusChanged;
readonly oldHasFocus: boolean;
readonly hasFocus: boolean;
constructor(oldHasFocus: boolean, hasFocus: boolean) {
this.oldHasFocus = oldHasFocus;
this.hasFocus = hasFocus;
}
public isNoOp(): boolean {
return (this.oldHasFocus === this.hasFocus);
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return new FocusChangedEvent(this.oldHasFocus, other.hasFocus);
}
}
export class ScrollChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ScrollChanged;
private readonly _oldScrollWidth: number;
private readonly _oldScrollLeft: number;
private readonly _oldScrollHeight: number;
private readonly _oldScrollTop: number;
public readonly scrollWidth: number;
public readonly scrollLeft: number;
public readonly scrollHeight: number;
public readonly scrollTop: number;
public readonly scrollWidthChanged: boolean;
public readonly scrollLeftChanged: boolean;
public readonly scrollHeightChanged: boolean;
public readonly scrollTopChanged: boolean;
constructor(
oldScrollWidth: number, oldScrollLeft: number, oldScrollHeight: number, oldScrollTop: number,
scrollWidth: number, scrollLeft: number, scrollHeight: number, scrollTop: number,
) {
this._oldScrollWidth = oldScrollWidth;
this._oldScrollLeft = oldScrollLeft;
this._oldScrollHeight = oldScrollHeight;
this._oldScrollTop = oldScrollTop;
this.scrollWidth = scrollWidth;
this.scrollLeft = scrollLeft;
this.scrollHeight = scrollHeight;
this.scrollTop = scrollTop;
this.scrollWidthChanged = (this._oldScrollWidth !== this.scrollWidth);
this.scrollLeftChanged = (this._oldScrollLeft !== this.scrollLeft);
this.scrollHeightChanged = (this._oldScrollHeight !== this.scrollHeight);
this.scrollTopChanged = (this._oldScrollTop !== this.scrollTop);
}
public isNoOp(): boolean {
return (!this.scrollWidthChanged && !this.scrollLeftChanged && !this.scrollHeightChanged && !this.scrollTopChanged);
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return new ScrollChangedEvent(
this._oldScrollWidth, this._oldScrollLeft, this._oldScrollHeight, this._oldScrollTop,
other.scrollWidth, other.scrollLeft, other.scrollHeight, other.scrollTop
);
}
}
export class ViewZonesChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ViewZonesChanged;
constructor() {
}
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return this;
}
}
export class HiddenAreasChangedEvent {
public readonly kind = OutgoingViewModelEventKind.HiddenAreasChanged;
constructor() {
}
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return this;
}
}
export class CursorStateChangedEvent {
public readonly kind = OutgoingViewModelEventKind.CursorStateChanged;
public readonly oldSelections: Selection[] | null;
public readonly selections: Selection[];
public readonly oldModelVersionId: number;
public readonly modelVersionId: number;
public readonly source: string;
public readonly reason: CursorChangeReason;
public readonly reachedMaxCursorCount: boolean;
constructor(oldSelections: Selection[] | null, selections: Selection[], oldModelVersionId: number, modelVersionId: number, source: string, reason: CursorChangeReason, reachedMaxCursorCount: boolean) {
this.oldSelections = oldSelections;
this.selections = selections;
this.oldModelVersionId = oldModelVersionId;
this.modelVersionId = modelVersionId;
this.source = source;
this.reason = reason;
this.reachedMaxCursorCount = reachedMaxCursorCount;
}
private static _selectionsAreEqual(a: Selection[] | null, b: Selection[] | null): boolean {
if (!a && !b) {
return true;
}
if (!a || !b) {
return false;
}
const aLen = a.length;
const bLen = b.length;
if (aLen !== bLen) {
return false;
}
for (let i = 0; i < aLen; i++) {
if (!a[i].equalsSelection(b[i])) {
return false;
}
}
return true;
}
public isNoOp(): boolean {
return (
CursorStateChangedEvent._selectionsAreEqual(this.oldSelections, this.selections)
&& this.oldModelVersionId === this.modelVersionId
);
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return new CursorStateChangedEvent(
this.oldSelections, other.selections, this.oldModelVersionId, other.modelVersionId, other.source, other.reason, this.reachedMaxCursorCount || other.reachedMaxCursorCount
);
}
}
export class ReadOnlyEditAttemptEvent {
public readonly kind = OutgoingViewModelEventKind.ReadOnlyEditAttempt;
constructor() {
}
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
if (other.kind !== this.kind) {
return null;
}
return this;
}
}
export class ModelDecorationsChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelDecorationsChanged;
constructor(
public readonly event: IModelDecorationsChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}
export class ModelLanguageChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelLanguageChanged;
constructor(
public readonly event: IModelLanguageChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}
export class ModelLanguageConfigurationChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelLanguageConfigurationChanged;
constructor(
public readonly event: IModelLanguageConfigurationChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}
export class ModelContentChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelContentChanged;
constructor(
public readonly event: IModelContentChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}
export class ModelOptionsChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelOptionsChanged;
constructor(
public readonly event: IModelOptionsChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}
export class ModelTokensChangedEvent {
public readonly kind = OutgoingViewModelEventKind.ModelTokensChanged;
constructor(
public readonly event: IModelTokensChangedEvent
) { }
public isNoOp(): boolean {
return false;
}
public attemptToMerge(other: OutgoingViewModelEvent): OutgoingViewModelEvent | null {
return null;
}
}