hoverOperation.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. var __asyncValues = (this && this.__asyncValues) || function (o) {
  15. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  16. var m = o[Symbol.asyncIterator], i;
  17. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  18. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  19. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  20. };
  21. import { createCancelableAsyncIterable, RunOnceScheduler } from '../../../base/common/async.js';
  22. import { onUnexpectedError } from '../../../base/common/errors.js';
  23. export class HoverOperation {
  24. constructor(computer, success, error, progress, hoverTime) {
  25. this._computer = computer;
  26. this._state = 0 /* IDLE */;
  27. this._hoverTime = hoverTime;
  28. this._firstWaitScheduler = new RunOnceScheduler(() => this._triggerAsyncComputation(), 0);
  29. this._secondWaitScheduler = new RunOnceScheduler(() => this._triggerSyncComputation(), 0);
  30. this._loadingMessageScheduler = new RunOnceScheduler(() => this._showLoadingMessage(), 0);
  31. this._asyncIterable = null;
  32. this._asyncIterableDone = false;
  33. this._completeCallback = success;
  34. this._errorCallback = error;
  35. this._progressCallback = progress;
  36. }
  37. setHoverTime(hoverTime) {
  38. this._hoverTime = hoverTime;
  39. }
  40. _firstWaitTime() {
  41. return this._hoverTime / 2;
  42. }
  43. _secondWaitTime() {
  44. return this._hoverTime / 2;
  45. }
  46. _loadingMessageTime() {
  47. return 3 * this._hoverTime;
  48. }
  49. _triggerAsyncComputation() {
  50. this._state = 2 /* SECOND_WAIT */;
  51. this._secondWaitScheduler.schedule(this._secondWaitTime());
  52. if (this._computer.computeAsync) {
  53. this._asyncIterableDone = false;
  54. this._asyncIterable = createCancelableAsyncIterable(token => this._computer.computeAsync(token));
  55. (() => __awaiter(this, void 0, void 0, function* () {
  56. var e_1, _a;
  57. try {
  58. try {
  59. for (var _b = __asyncValues(this._asyncIterable), _c; _c = yield _b.next(), !_c.done;) {
  60. const item = _c.value;
  61. if (item) {
  62. this._computer.onResult([item], false);
  63. this._onProgress();
  64. }
  65. }
  66. }
  67. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  68. finally {
  69. try {
  70. if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
  71. }
  72. finally { if (e_1) throw e_1.error; }
  73. }
  74. this._asyncIterableDone = true;
  75. this._withAsyncResult();
  76. }
  77. catch (e) {
  78. this._onError(e);
  79. }
  80. }))();
  81. }
  82. else {
  83. this._asyncIterableDone = true;
  84. }
  85. }
  86. _triggerSyncComputation() {
  87. if (this._computer.computeSync) {
  88. this._computer.onResult(this._computer.computeSync(), true);
  89. }
  90. if (this._asyncIterableDone) {
  91. this._state = 0 /* IDLE */;
  92. this._onComplete();
  93. }
  94. else {
  95. this._state = 3 /* WAITING_FOR_ASYNC_COMPUTATION */;
  96. this._onProgress();
  97. }
  98. }
  99. _showLoadingMessage() {
  100. if (this._state === 3 /* WAITING_FOR_ASYNC_COMPUTATION */) {
  101. this._state = 4 /* WAITING_FOR_ASYNC_COMPUTATION_SHOWING_LOADING */;
  102. this._onProgress();
  103. }
  104. }
  105. _withAsyncResult() {
  106. if (this._state === 3 /* WAITING_FOR_ASYNC_COMPUTATION */ || this._state === 4 /* WAITING_FOR_ASYNC_COMPUTATION_SHOWING_LOADING */) {
  107. this._state = 0 /* IDLE */;
  108. this._onComplete();
  109. }
  110. }
  111. _onComplete() {
  112. this._completeCallback(this._computer.getResult());
  113. }
  114. _onError(error) {
  115. if (this._errorCallback) {
  116. this._errorCallback(error);
  117. }
  118. else {
  119. onUnexpectedError(error);
  120. }
  121. }
  122. _onProgress() {
  123. if (this._state === 4 /* WAITING_FOR_ASYNC_COMPUTATION_SHOWING_LOADING */) {
  124. this._progressCallback(this._computer.getResultWithLoadingMessage());
  125. }
  126. else {
  127. this._progressCallback(this._computer.getResult());
  128. }
  129. }
  130. start(mode) {
  131. if (mode === 0 /* Delayed */) {
  132. if (this._state === 0 /* IDLE */) {
  133. this._state = 1 /* FIRST_WAIT */;
  134. this._firstWaitScheduler.schedule(this._firstWaitTime());
  135. this._loadingMessageScheduler.schedule(this._loadingMessageTime());
  136. }
  137. }
  138. else {
  139. switch (this._state) {
  140. case 0 /* IDLE */:
  141. this._triggerAsyncComputation();
  142. this._secondWaitScheduler.cancel();
  143. this._triggerSyncComputation();
  144. break;
  145. case 2 /* SECOND_WAIT */:
  146. this._secondWaitScheduler.cancel();
  147. this._triggerSyncComputation();
  148. break;
  149. }
  150. }
  151. }
  152. cancel() {
  153. this._firstWaitScheduler.cancel();
  154. this._secondWaitScheduler.cancel();
  155. this._loadingMessageScheduler.cancel();
  156. if (this._asyncIterable) {
  157. this._asyncIterable.cancel();
  158. this._asyncIterable = null;
  159. }
  160. this._state = 0 /* IDLE */;
  161. }
  162. }