viewModelEventDispatcher.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Emitter } from '../../base/common/event.js';
  6. import { Disposable } from '../../base/common/lifecycle.js';
  7. export class ViewModelEventDispatcher extends Disposable {
  8. constructor() {
  9. super();
  10. this._onEvent = this._register(new Emitter());
  11. this.onEvent = this._onEvent.event;
  12. this._eventHandlers = [];
  13. this._viewEventQueue = null;
  14. this._isConsumingViewEventQueue = false;
  15. this._collector = null;
  16. this._collectorCnt = 0;
  17. this._outgoingEvents = [];
  18. }
  19. emitOutgoingEvent(e) {
  20. this._addOutgoingEvent(e);
  21. this._emitOutgoingEvents();
  22. }
  23. _addOutgoingEvent(e) {
  24. for (let i = 0, len = this._outgoingEvents.length; i < len; i++) {
  25. const mergeResult = (this._outgoingEvents[i].kind === e.kind ? this._outgoingEvents[i].attemptToMerge(e) : null);
  26. if (mergeResult) {
  27. this._outgoingEvents[i] = mergeResult;
  28. return;
  29. }
  30. }
  31. // not merged
  32. this._outgoingEvents.push(e);
  33. }
  34. _emitOutgoingEvents() {
  35. while (this._outgoingEvents.length > 0) {
  36. if (this._collector || this._isConsumingViewEventQueue) {
  37. // right now collecting or emitting view events, so let's postpone emitting
  38. return;
  39. }
  40. const event = this._outgoingEvents.shift();
  41. if (event.isNoOp()) {
  42. continue;
  43. }
  44. this._onEvent.fire(event);
  45. }
  46. }
  47. addViewEventHandler(eventHandler) {
  48. for (let i = 0, len = this._eventHandlers.length; i < len; i++) {
  49. if (this._eventHandlers[i] === eventHandler) {
  50. console.warn('Detected duplicate listener in ViewEventDispatcher', eventHandler);
  51. }
  52. }
  53. this._eventHandlers.push(eventHandler);
  54. }
  55. removeViewEventHandler(eventHandler) {
  56. for (let i = 0; i < this._eventHandlers.length; i++) {
  57. if (this._eventHandlers[i] === eventHandler) {
  58. this._eventHandlers.splice(i, 1);
  59. break;
  60. }
  61. }
  62. }
  63. beginEmitViewEvents() {
  64. this._collectorCnt++;
  65. if (this._collectorCnt === 1) {
  66. this._collector = new ViewModelEventsCollector();
  67. }
  68. return this._collector;
  69. }
  70. endEmitViewEvents() {
  71. this._collectorCnt--;
  72. if (this._collectorCnt === 0) {
  73. const outgoingEvents = this._collector.outgoingEvents;
  74. const viewEvents = this._collector.viewEvents;
  75. this._collector = null;
  76. for (const outgoingEvent of outgoingEvents) {
  77. this._addOutgoingEvent(outgoingEvent);
  78. }
  79. if (viewEvents.length > 0) {
  80. this._emitMany(viewEvents);
  81. }
  82. }
  83. this._emitOutgoingEvents();
  84. }
  85. emitSingleViewEvent(event) {
  86. try {
  87. const eventsCollector = this.beginEmitViewEvents();
  88. eventsCollector.emitViewEvent(event);
  89. }
  90. finally {
  91. this.endEmitViewEvents();
  92. }
  93. }
  94. _emitMany(events) {
  95. if (this._viewEventQueue) {
  96. this._viewEventQueue = this._viewEventQueue.concat(events);
  97. }
  98. else {
  99. this._viewEventQueue = events;
  100. }
  101. if (!this._isConsumingViewEventQueue) {
  102. this._consumeViewEventQueue();
  103. }
  104. }
  105. _consumeViewEventQueue() {
  106. try {
  107. this._isConsumingViewEventQueue = true;
  108. this._doConsumeQueue();
  109. }
  110. finally {
  111. this._isConsumingViewEventQueue = false;
  112. }
  113. }
  114. _doConsumeQueue() {
  115. while (this._viewEventQueue) {
  116. // Empty event queue, as events might come in while sending these off
  117. const events = this._viewEventQueue;
  118. this._viewEventQueue = null;
  119. // Use a clone of the event handlers list, as they might remove themselves
  120. const eventHandlers = this._eventHandlers.slice(0);
  121. for (const eventHandler of eventHandlers) {
  122. eventHandler.handleEvents(events);
  123. }
  124. }
  125. }
  126. }
  127. export class ViewModelEventsCollector {
  128. constructor() {
  129. this.viewEvents = [];
  130. this.outgoingEvents = [];
  131. }
  132. emitViewEvent(event) {
  133. this.viewEvents.push(event);
  134. }
  135. emitOutgoingEvent(e) {
  136. this.outgoingEvents.push(e);
  137. }
  138. }
  139. export class ContentSizeChangedEvent {
  140. constructor(oldContentWidth, oldContentHeight, contentWidth, contentHeight) {
  141. this.kind = 0 /* OutgoingViewModelEventKind.ContentSizeChanged */;
  142. this._oldContentWidth = oldContentWidth;
  143. this._oldContentHeight = oldContentHeight;
  144. this.contentWidth = contentWidth;
  145. this.contentHeight = contentHeight;
  146. this.contentWidthChanged = (this._oldContentWidth !== this.contentWidth);
  147. this.contentHeightChanged = (this._oldContentHeight !== this.contentHeight);
  148. }
  149. isNoOp() {
  150. return (!this.contentWidthChanged && !this.contentHeightChanged);
  151. }
  152. attemptToMerge(other) {
  153. if (other.kind !== this.kind) {
  154. return null;
  155. }
  156. return new ContentSizeChangedEvent(this._oldContentWidth, this._oldContentHeight, other.contentWidth, other.contentHeight);
  157. }
  158. }
  159. export class FocusChangedEvent {
  160. constructor(oldHasFocus, hasFocus) {
  161. this.kind = 1 /* OutgoingViewModelEventKind.FocusChanged */;
  162. this.oldHasFocus = oldHasFocus;
  163. this.hasFocus = hasFocus;
  164. }
  165. isNoOp() {
  166. return (this.oldHasFocus === this.hasFocus);
  167. }
  168. attemptToMerge(other) {
  169. if (other.kind !== this.kind) {
  170. return null;
  171. }
  172. return new FocusChangedEvent(this.oldHasFocus, other.hasFocus);
  173. }
  174. }
  175. export class ScrollChangedEvent {
  176. constructor(oldScrollWidth, oldScrollLeft, oldScrollHeight, oldScrollTop, scrollWidth, scrollLeft, scrollHeight, scrollTop) {
  177. this.kind = 2 /* OutgoingViewModelEventKind.ScrollChanged */;
  178. this._oldScrollWidth = oldScrollWidth;
  179. this._oldScrollLeft = oldScrollLeft;
  180. this._oldScrollHeight = oldScrollHeight;
  181. this._oldScrollTop = oldScrollTop;
  182. this.scrollWidth = scrollWidth;
  183. this.scrollLeft = scrollLeft;
  184. this.scrollHeight = scrollHeight;
  185. this.scrollTop = scrollTop;
  186. this.scrollWidthChanged = (this._oldScrollWidth !== this.scrollWidth);
  187. this.scrollLeftChanged = (this._oldScrollLeft !== this.scrollLeft);
  188. this.scrollHeightChanged = (this._oldScrollHeight !== this.scrollHeight);
  189. this.scrollTopChanged = (this._oldScrollTop !== this.scrollTop);
  190. }
  191. isNoOp() {
  192. return (!this.scrollWidthChanged && !this.scrollLeftChanged && !this.scrollHeightChanged && !this.scrollTopChanged);
  193. }
  194. attemptToMerge(other) {
  195. if (other.kind !== this.kind) {
  196. return null;
  197. }
  198. return new ScrollChangedEvent(this._oldScrollWidth, this._oldScrollLeft, this._oldScrollHeight, this._oldScrollTop, other.scrollWidth, other.scrollLeft, other.scrollHeight, other.scrollTop);
  199. }
  200. }
  201. export class ViewZonesChangedEvent {
  202. constructor() {
  203. this.kind = 3 /* OutgoingViewModelEventKind.ViewZonesChanged */;
  204. }
  205. isNoOp() {
  206. return false;
  207. }
  208. attemptToMerge(other) {
  209. if (other.kind !== this.kind) {
  210. return null;
  211. }
  212. return this;
  213. }
  214. }
  215. export class HiddenAreasChangedEvent {
  216. constructor() {
  217. this.kind = 4 /* OutgoingViewModelEventKind.HiddenAreasChanged */;
  218. }
  219. isNoOp() {
  220. return false;
  221. }
  222. attemptToMerge(other) {
  223. if (other.kind !== this.kind) {
  224. return null;
  225. }
  226. return this;
  227. }
  228. }
  229. export class CursorStateChangedEvent {
  230. constructor(oldSelections, selections, oldModelVersionId, modelVersionId, source, reason, reachedMaxCursorCount) {
  231. this.kind = 6 /* OutgoingViewModelEventKind.CursorStateChanged */;
  232. this.oldSelections = oldSelections;
  233. this.selections = selections;
  234. this.oldModelVersionId = oldModelVersionId;
  235. this.modelVersionId = modelVersionId;
  236. this.source = source;
  237. this.reason = reason;
  238. this.reachedMaxCursorCount = reachedMaxCursorCount;
  239. }
  240. static _selectionsAreEqual(a, b) {
  241. if (!a && !b) {
  242. return true;
  243. }
  244. if (!a || !b) {
  245. return false;
  246. }
  247. const aLen = a.length;
  248. const bLen = b.length;
  249. if (aLen !== bLen) {
  250. return false;
  251. }
  252. for (let i = 0; i < aLen; i++) {
  253. if (!a[i].equalsSelection(b[i])) {
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259. isNoOp() {
  260. return (CursorStateChangedEvent._selectionsAreEqual(this.oldSelections, this.selections)
  261. && this.oldModelVersionId === this.modelVersionId);
  262. }
  263. attemptToMerge(other) {
  264. if (other.kind !== this.kind) {
  265. return null;
  266. }
  267. return new CursorStateChangedEvent(this.oldSelections, other.selections, this.oldModelVersionId, other.modelVersionId, other.source, other.reason, this.reachedMaxCursorCount || other.reachedMaxCursorCount);
  268. }
  269. }
  270. export class ReadOnlyEditAttemptEvent {
  271. constructor() {
  272. this.kind = 5 /* OutgoingViewModelEventKind.ReadOnlyEditAttempt */;
  273. }
  274. isNoOp() {
  275. return false;
  276. }
  277. attemptToMerge(other) {
  278. if (other.kind !== this.kind) {
  279. return null;
  280. }
  281. return this;
  282. }
  283. }
  284. export class ModelDecorationsChangedEvent {
  285. constructor(event) {
  286. this.event = event;
  287. this.kind = 7 /* OutgoingViewModelEventKind.ModelDecorationsChanged */;
  288. }
  289. isNoOp() {
  290. return false;
  291. }
  292. attemptToMerge(other) {
  293. return null;
  294. }
  295. }
  296. export class ModelLanguageChangedEvent {
  297. constructor(event) {
  298. this.event = event;
  299. this.kind = 8 /* OutgoingViewModelEventKind.ModelLanguageChanged */;
  300. }
  301. isNoOp() {
  302. return false;
  303. }
  304. attemptToMerge(other) {
  305. return null;
  306. }
  307. }
  308. export class ModelLanguageConfigurationChangedEvent {
  309. constructor(event) {
  310. this.event = event;
  311. this.kind = 9 /* OutgoingViewModelEventKind.ModelLanguageConfigurationChanged */;
  312. }
  313. isNoOp() {
  314. return false;
  315. }
  316. attemptToMerge(other) {
  317. return null;
  318. }
  319. }
  320. export class ModelContentChangedEvent {
  321. constructor(event) {
  322. this.event = event;
  323. this.kind = 10 /* OutgoingViewModelEventKind.ModelContentChanged */;
  324. }
  325. isNoOp() {
  326. return false;
  327. }
  328. attemptToMerge(other) {
  329. return null;
  330. }
  331. }
  332. export class ModelOptionsChangedEvent {
  333. constructor(event) {
  334. this.event = event;
  335. this.kind = 11 /* OutgoingViewModelEventKind.ModelOptionsChanged */;
  336. }
  337. isNoOp() {
  338. return false;
  339. }
  340. attemptToMerge(other) {
  341. return null;
  342. }
  343. }
  344. export class ModelTokensChangedEvent {
  345. constructor(event) {
  346. this.event = event;
  347. this.kind = 12 /* OutgoingViewModelEventKind.ModelTokensChanged */;
  348. }
  349. isNoOp() {
  350. return false;
  351. }
  352. attemptToMerge(other) {
  353. return null;
  354. }
  355. }