index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@lumino/coreutils'), require('@lumino/signaling')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@lumino/coreutils', '@lumino/signaling'], factory) :
  4. (global = global || self, factory(global.lumino_polling = {}, global.lumino_coreutils, global.lumino_signaling));
  5. }(this, (function (exports, coreutils, signaling) { 'use strict';
  6. /*! *****************************************************************************
  7. Copyright (c) Microsoft Corporation. All rights reserved.
  8. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  9. this file except in compliance with the License. You may obtain a copy of the
  10. License at http://www.apache.org/licenses/LICENSE-2.0
  11. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. MERCHANTABLITY OR NON-INFRINGEMENT.
  15. See the Apache Version 2.0 License for specific language governing permissions
  16. and limitations under the License.
  17. ***************************************************************************** */
  18. /* global Reflect, Promise */
  19. var extendStatics = function(d, b) {
  20. extendStatics = Object.setPrototypeOf ||
  21. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  22. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  23. return extendStatics(d, b);
  24. };
  25. function __extends(d, b) {
  26. extendStatics(d, b);
  27. function __() { this.constructor = d; }
  28. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  29. }
  30. var __assign = function() {
  31. __assign = Object.assign || function __assign(t) {
  32. for (var s, i = 1, n = arguments.length; i < n; i++) {
  33. s = arguments[i];
  34. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  35. }
  36. return t;
  37. };
  38. return __assign.apply(this, arguments);
  39. };
  40. function __awaiter(thisArg, _arguments, P, generator) {
  41. return new (P || (P = Promise))(function (resolve, reject) {
  42. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  43. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  44. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  45. step((generator = generator.apply(thisArg, _arguments || [])).next());
  46. });
  47. }
  48. function __generator(thisArg, body) {
  49. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  50. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  51. function verb(n) { return function (v) { return step([n, v]); }; }
  52. function step(op) {
  53. if (f) throw new TypeError("Generator is already executing.");
  54. while (_) try {
  55. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  56. if (y = 0, t) op = [op[0] & 2, t.value];
  57. switch (op[0]) {
  58. case 0: case 1: t = op; break;
  59. case 4: _.label++; return { value: op[1], done: false };
  60. case 5: _.label++; y = op[1]; op = [0]; continue;
  61. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  62. default:
  63. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  64. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  65. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  66. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  67. if (t[2]) _.ops.pop();
  68. _.trys.pop(); continue;
  69. }
  70. op = body.call(thisArg, _);
  71. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  72. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  73. }
  74. }
  75. // Copyright (c) Jupyter Development Team.
  76. /**
  77. * A function to defer an action immediately.
  78. */
  79. var defer = typeof requestAnimationFrame === 'function'
  80. ? requestAnimationFrame
  81. : setImmediate;
  82. /**
  83. * A function to cancel a deferred action.
  84. */
  85. var cancel = typeof cancelAnimationFrame === 'function'
  86. ? cancelAnimationFrame
  87. : clearImmediate;
  88. /**
  89. * A class that wraps an asynchronous function to poll at a regular interval
  90. * with exponential increases to the interval length if the poll fails.
  91. *
  92. * @typeparam T - The resolved type of the factory's promises.
  93. * Defaults to `any`.
  94. *
  95. * @typeparam U - The rejected type of the factory's promises.
  96. * Defaults to `any`.
  97. *
  98. * @typeparam V - An optional type to extend the phases supported by a poll.
  99. * Defaults to `standby`, which already exists in the `Phase` type.
  100. */
  101. exports.Poll = /** @class */ (function () {
  102. /**
  103. * Instantiate a new poll with exponential backoff in case of failure.
  104. *
  105. * @param options - The poll instantiation options.
  106. */
  107. function Poll(options) {
  108. var _this = this;
  109. this._disposed = new signaling.Signal(this);
  110. this._tick = new coreutils.PromiseDelegate();
  111. this._ticked = new signaling.Signal(this);
  112. this._timeout = -1;
  113. this._factory = options.factory;
  114. this._standby = options.standby || Private.DEFAULT_STANDBY;
  115. this._state = __assign(__assign({}, Private.DEFAULT_STATE), { timestamp: new Date().getTime() });
  116. // Normalize poll frequency `max` to be the greater of
  117. // default `max`, `options.frequency.max`, or `options.frequency.interval`.
  118. var frequency = options.frequency || {};
  119. var max = Math.max(frequency.interval || 0, frequency.max || 0, Private.DEFAULT_FREQUENCY.max);
  120. this.frequency = __assign(__assign(__assign({}, Private.DEFAULT_FREQUENCY), frequency), { max: max });
  121. this.name = options.name || Private.DEFAULT_NAME;
  122. if ('auto' in options ? options.auto : true) {
  123. defer(function () { return void _this.start(); });
  124. }
  125. }
  126. Object.defineProperty(Poll.prototype, "disposed", {
  127. /**
  128. * A signal emitted when the poll is disposed.
  129. */
  130. get: function () {
  131. return this._disposed;
  132. },
  133. enumerable: true,
  134. configurable: true
  135. });
  136. Object.defineProperty(Poll.prototype, "frequency", {
  137. /**
  138. * The polling frequency parameters.
  139. */
  140. get: function () {
  141. return this._frequency;
  142. },
  143. set: function (frequency) {
  144. if (this.isDisposed || coreutils.JSONExt.deepEqual(frequency, this.frequency || {})) {
  145. return;
  146. }
  147. var backoff = frequency.backoff, interval = frequency.interval, max = frequency.max;
  148. interval = Math.round(interval);
  149. max = Math.round(max);
  150. if (typeof backoff === 'number' && backoff < 1) {
  151. throw new Error('Poll backoff growth factor must be at least 1');
  152. }
  153. if ((interval < 0 || interval > max) && interval !== Poll.NEVER) {
  154. throw new Error('Poll interval must be between 0 and max');
  155. }
  156. if (max > Poll.MAX_INTERVAL && max !== Poll.NEVER) {
  157. throw new Error("Max interval must be less than " + Poll.MAX_INTERVAL);
  158. }
  159. this._frequency = { backoff: backoff, interval: interval, max: max };
  160. },
  161. enumerable: true,
  162. configurable: true
  163. });
  164. Object.defineProperty(Poll.prototype, "isDisposed", {
  165. /**
  166. * Whether the poll is disposed.
  167. */
  168. get: function () {
  169. return this.state.phase === 'disposed';
  170. },
  171. enumerable: true,
  172. configurable: true
  173. });
  174. Object.defineProperty(Poll.prototype, "standby", {
  175. /**
  176. * Indicates when the poll switches to standby.
  177. */
  178. get: function () {
  179. return this._standby;
  180. },
  181. set: function (standby) {
  182. if (this.isDisposed || this.standby === standby) {
  183. return;
  184. }
  185. this._standby = standby;
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. Object.defineProperty(Poll.prototype, "state", {
  191. /**
  192. * The poll state, which is the content of the current poll tick.
  193. */
  194. get: function () {
  195. return this._state;
  196. },
  197. enumerable: true,
  198. configurable: true
  199. });
  200. Object.defineProperty(Poll.prototype, "tick", {
  201. /**
  202. * A promise that resolves when the poll next ticks.
  203. */
  204. get: function () {
  205. return this._tick.promise;
  206. },
  207. enumerable: true,
  208. configurable: true
  209. });
  210. Object.defineProperty(Poll.prototype, "ticked", {
  211. /**
  212. * A signal emitted when the poll ticks and fires off a new request.
  213. */
  214. get: function () {
  215. return this._ticked;
  216. },
  217. enumerable: true,
  218. configurable: true
  219. });
  220. /**
  221. * Dispose the poll.
  222. */
  223. Poll.prototype.dispose = function () {
  224. if (this.isDisposed) {
  225. return;
  226. }
  227. this._state = __assign(__assign({}, Private.DISPOSED_STATE), { timestamp: new Date().getTime() });
  228. this._tick.promise.catch(function (_) { return undefined; });
  229. this._tick.reject(new Error("Poll (" + this.name + ") is disposed."));
  230. this._disposed.emit(undefined);
  231. signaling.Signal.clearData(this);
  232. };
  233. /**
  234. * Refreshes the poll. Schedules `refreshed` tick if necessary.
  235. *
  236. * @returns A promise that resolves after tick is scheduled and never rejects.
  237. *
  238. * #### Notes
  239. * The returned promise resolves after the tick is scheduled, but before
  240. * the polling action is run. To wait until after the poll action executes,
  241. * await the `poll.tick` promise: `await poll.refresh(); await poll.tick;`
  242. */
  243. Poll.prototype.refresh = function () {
  244. return this.schedule({
  245. cancel: function (_a) {
  246. var phase = _a.phase;
  247. return phase === 'refreshed';
  248. },
  249. interval: Poll.IMMEDIATE,
  250. phase: 'refreshed'
  251. });
  252. };
  253. /**
  254. * Schedule the next poll tick.
  255. *
  256. * @param next - The next poll state data to schedule. Defaults to standby.
  257. *
  258. * @param next.cancel - Cancels state transition if function returns `true`.
  259. *
  260. * @returns A promise that resolves when the next poll state is active.
  261. *
  262. * #### Notes
  263. * This method is not meant to be invoked by user code typically. It is public
  264. * to allow poll instances to be composed into classes that schedule ticks.
  265. */
  266. Poll.prototype.schedule = function (next) {
  267. if (next === void 0) { next = {}; }
  268. return __awaiter(this, void 0, void 0, function () {
  269. var last, pending, scheduled, state, execute;
  270. var _this = this;
  271. return __generator(this, function (_a) {
  272. switch (_a.label) {
  273. case 0:
  274. if (this.isDisposed) {
  275. return [2 /*return*/];
  276. }
  277. // Check if the phase transition should be canceled.
  278. if (next.cancel && next.cancel(this.state)) {
  279. return [2 /*return*/];
  280. }
  281. last = this.state;
  282. pending = this._tick;
  283. scheduled = new coreutils.PromiseDelegate();
  284. state = __assign({ interval: this.frequency.interval, payload: null, phase: 'standby', timestamp: new Date().getTime() }, next);
  285. this._state = state;
  286. this._tick = scheduled;
  287. // Clear the schedule if possible.
  288. if (last.interval === Poll.IMMEDIATE) {
  289. cancel(this._timeout);
  290. }
  291. else {
  292. clearTimeout(this._timeout);
  293. }
  294. // Emit ticked signal, resolve pending promise, and await its settlement.
  295. this._ticked.emit(this.state);
  296. pending.resolve(this);
  297. return [4 /*yield*/, pending.promise];
  298. case 1:
  299. _a.sent();
  300. execute = function () {
  301. if (_this.isDisposed || _this.tick !== scheduled.promise) {
  302. return;
  303. }
  304. _this._execute();
  305. };
  306. this._timeout =
  307. state.interval === Poll.IMMEDIATE
  308. ? defer(execute)
  309. : state.interval === Poll.NEVER
  310. ? -1
  311. : setTimeout(execute, state.interval);
  312. return [2 /*return*/];
  313. }
  314. });
  315. });
  316. };
  317. /**
  318. * Starts the poll. Schedules `started` tick if necessary.
  319. *
  320. * @returns A promise that resolves after tick is scheduled and never rejects.
  321. */
  322. Poll.prototype.start = function () {
  323. return this.schedule({
  324. cancel: function (_a) {
  325. var phase = _a.phase;
  326. return phase !== 'constructed' && phase !== 'standby' && phase !== 'stopped';
  327. },
  328. interval: Poll.IMMEDIATE,
  329. phase: 'started'
  330. });
  331. };
  332. /**
  333. * Stops the poll. Schedules `stopped` tick if necessary.
  334. *
  335. * @returns A promise that resolves after tick is scheduled and never rejects.
  336. */
  337. Poll.prototype.stop = function () {
  338. return this.schedule({
  339. cancel: function (_a) {
  340. var phase = _a.phase;
  341. return phase === 'stopped';
  342. },
  343. interval: Poll.NEVER,
  344. phase: 'stopped'
  345. });
  346. };
  347. /**
  348. * Execute a new poll factory promise or stand by if necessary.
  349. */
  350. Poll.prototype._execute = function () {
  351. var _this = this;
  352. var standby = typeof this.standby === 'function' ? this.standby() : this.standby;
  353. standby =
  354. standby === 'never'
  355. ? false
  356. : standby === 'when-hidden'
  357. ? !!(typeof document !== 'undefined' && document && document.hidden)
  358. : standby;
  359. // If in standby mode schedule next tick without calling the factory.
  360. if (standby) {
  361. void this.schedule();
  362. return;
  363. }
  364. var pending = this.tick;
  365. this._factory(this.state)
  366. .then(function (resolved) {
  367. if (_this.isDisposed || _this.tick !== pending) {
  368. return;
  369. }
  370. void _this.schedule({
  371. payload: resolved,
  372. phase: _this.state.phase === 'rejected' ? 'reconnected' : 'resolved'
  373. });
  374. })
  375. .catch(function (rejected) {
  376. if (_this.isDisposed || _this.tick !== pending) {
  377. return;
  378. }
  379. void _this.schedule({
  380. interval: Private.sleep(_this.frequency, _this.state),
  381. payload: rejected,
  382. phase: 'rejected'
  383. });
  384. });
  385. };
  386. return Poll;
  387. }());
  388. /**
  389. * A namespace for `Poll` types, interfaces, and statics.
  390. */
  391. (function (Poll) {
  392. /**
  393. * An interval value that indicates the poll should tick immediately.
  394. */
  395. Poll.IMMEDIATE = 0;
  396. /**
  397. * Delays are 32-bit integers in many browsers so intervals need to be capped.
  398. *
  399. * #### Notes
  400. * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value
  401. */
  402. Poll.MAX_INTERVAL = 2147483647;
  403. /**
  404. * An interval value that indicates the poll should never tick.
  405. */
  406. Poll.NEVER = Infinity;
  407. })(exports.Poll || (exports.Poll = {}));
  408. /**
  409. * A namespace for private module data.
  410. */
  411. var Private;
  412. (function (Private) {
  413. /**
  414. * The default backoff growth rate if `backoff` is `true`.
  415. */
  416. Private.DEFAULT_BACKOFF = 3;
  417. /**
  418. * The default polling frequency.
  419. */
  420. Private.DEFAULT_FREQUENCY = {
  421. backoff: true,
  422. interval: 1000,
  423. max: 30 * 1000
  424. };
  425. /**
  426. * The default poll name.
  427. */
  428. Private.DEFAULT_NAME = 'unknown';
  429. /**
  430. * The default poll standby behavior.
  431. */
  432. Private.DEFAULT_STANDBY = 'when-hidden';
  433. /**
  434. * The first poll tick state's default values superseded in constructor.
  435. */
  436. Private.DEFAULT_STATE = {
  437. interval: exports.Poll.NEVER,
  438. payload: null,
  439. phase: 'constructed',
  440. timestamp: new Date(0).getTime()
  441. };
  442. /**
  443. * The disposed tick state values.
  444. */
  445. Private.DISPOSED_STATE = {
  446. interval: exports.Poll.NEVER,
  447. payload: null,
  448. phase: 'disposed',
  449. timestamp: new Date(0).getTime()
  450. };
  451. /**
  452. * Get a random integer between min and max, inclusive of both.
  453. *
  454. * #### Notes
  455. * From
  456. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values_inclusive
  457. *
  458. * From the MDN page: It might be tempting to use Math.round() to accomplish
  459. * that, but doing so would cause your random numbers to follow a non-uniform
  460. * distribution, which may not be acceptable for your needs.
  461. */
  462. function getRandomIntInclusive(min, max) {
  463. min = Math.ceil(min);
  464. max = Math.floor(max);
  465. return Math.floor(Math.random() * (max - min + 1)) + min;
  466. }
  467. /**
  468. * Returns the number of milliseconds to sleep before the next tick.
  469. *
  470. * @param frequency - The poll's base frequency.
  471. * @param last - The poll's last tick.
  472. */
  473. function sleep(frequency, last) {
  474. var backoff = frequency.backoff, interval = frequency.interval, max = frequency.max;
  475. if (interval === exports.Poll.NEVER) {
  476. return interval;
  477. }
  478. var growth = backoff === true ? Private.DEFAULT_BACKOFF : backoff === false ? 1 : backoff;
  479. var random = getRandomIntInclusive(interval, last.interval * growth);
  480. return Math.min(max, random);
  481. }
  482. Private.sleep = sleep;
  483. })(Private || (Private = {}));
  484. // Copyright (c) Jupyter Development Team.
  485. /**
  486. * A base class to implement rate limiters with different invocation strategies.
  487. *
  488. * @typeparam T - The resolved type of the underlying function.
  489. *
  490. * @typeparam U - The rejected type of the underlying function.
  491. */
  492. var RateLimiter = /** @class */ (function () {
  493. /**
  494. * Instantiate a rate limiter.
  495. *
  496. * @param fn - The function to rate limit.
  497. *
  498. * @param limit - The rate limit; defaults to 500ms.
  499. */
  500. function RateLimiter(fn, limit) {
  501. var _this = this;
  502. if (limit === void 0) { limit = 500; }
  503. /**
  504. * A promise that resolves on each successful invocation.
  505. */
  506. this.payload = null;
  507. this.limit = limit;
  508. this.poll = new exports.Poll({
  509. auto: false,
  510. factory: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
  511. switch (_a.label) {
  512. case 0: return [4 /*yield*/, fn()];
  513. case 1: return [2 /*return*/, _a.sent()];
  514. }
  515. }); }); },
  516. frequency: { backoff: false, interval: exports.Poll.NEVER, max: exports.Poll.NEVER },
  517. standby: 'never'
  518. });
  519. this.payload = new coreutils.PromiseDelegate();
  520. this.poll.ticked.connect(function (_, state) {
  521. var payload = _this.payload;
  522. if (state.phase === 'resolved') {
  523. _this.payload = new coreutils.PromiseDelegate();
  524. payload.resolve(state.payload);
  525. return;
  526. }
  527. if (state.phase === 'rejected' || state.phase === 'stopped') {
  528. _this.payload = new coreutils.PromiseDelegate();
  529. payload.promise.catch(function (_) { return undefined; });
  530. payload.reject(state.payload);
  531. return;
  532. }
  533. }, this);
  534. }
  535. Object.defineProperty(RateLimiter.prototype, "isDisposed", {
  536. /**
  537. * Whether the rate limiter is disposed.
  538. */
  539. get: function () {
  540. return this.payload === null;
  541. },
  542. enumerable: true,
  543. configurable: true
  544. });
  545. /**
  546. * Disposes the rate limiter.
  547. */
  548. RateLimiter.prototype.dispose = function () {
  549. if (this.isDisposed) {
  550. return;
  551. }
  552. this.payload = null;
  553. this.poll.dispose();
  554. };
  555. /**
  556. * Stop the function if it is mid-flight.
  557. */
  558. RateLimiter.prototype.stop = function () {
  559. return __awaiter(this, void 0, void 0, function () {
  560. return __generator(this, function (_a) {
  561. return [2 /*return*/, this.poll.stop()];
  562. });
  563. });
  564. };
  565. return RateLimiter;
  566. }());
  567. /**
  568. * Wraps and debounces a function that can be called multiple times and only
  569. * executes the underlying function one `interval` after the last invocation.
  570. *
  571. * @typeparam T - The resolved type of the underlying function. Defaults to any.
  572. *
  573. * @typeparam U - The rejected type of the underlying function. Defaults to any.
  574. */
  575. var Debouncer = /** @class */ (function (_super) {
  576. __extends(Debouncer, _super);
  577. function Debouncer() {
  578. return _super !== null && _super.apply(this, arguments) || this;
  579. }
  580. /**
  581. * Invokes the function and only executes after rate limit has elapsed.
  582. * Each invocation resets the timer.
  583. */
  584. Debouncer.prototype.invoke = function () {
  585. void this.poll.schedule({ interval: this.limit, phase: 'invoked' });
  586. return this.payload.promise;
  587. };
  588. return Debouncer;
  589. }(RateLimiter));
  590. /**
  591. * Wraps and throttles a function that can be called multiple times and only
  592. * executes the underlying function once per `interval`.
  593. *
  594. * @typeparam T - The resolved type of the underlying function. Defaults to any.
  595. *
  596. * @typeparam U - The rejected type of the underlying function. Defaults to any.
  597. */
  598. exports.Throttler = /** @class */ (function (_super) {
  599. __extends(Throttler, _super);
  600. /**
  601. * Instantiate a throttler.
  602. *
  603. * @param fn - The function being throttled.
  604. *
  605. * @param options - Throttling configuration or throttling limit in ms.
  606. *
  607. * #### Notes
  608. * The `edge` defaults to `leading`; the `limit` defaults to `500`.
  609. */
  610. function Throttler(fn, options) {
  611. var _this = _super.call(this, fn, typeof options === 'number' ? options : options && options.limit) || this;
  612. var edge = 'leading';
  613. if (typeof options !== 'number') {
  614. options = options || {};
  615. edge = 'edge' in options ? options.edge : edge;
  616. }
  617. _this._interval = edge === 'trailing' ? _this.limit : exports.Poll.IMMEDIATE;
  618. return _this;
  619. }
  620. /**
  621. * Throttles function invocations if one is currently in flight.
  622. */
  623. Throttler.prototype.invoke = function () {
  624. if (this.poll.state.phase !== 'invoked') {
  625. void this.poll.schedule({ interval: this._interval, phase: 'invoked' });
  626. }
  627. return this.payload.promise;
  628. };
  629. return Throttler;
  630. }(RateLimiter));
  631. /**
  632. * A namespace for `Throttler` interfaces.
  633. */
  634. (function (Throttler) {
  635. })(exports.Throttler || (exports.Throttler = {}));
  636. exports.Debouncer = Debouncer;
  637. exports.RateLimiter = RateLimiter;
  638. Object.defineProperty(exports, '__esModule', { value: true });
  639. })));
  640. //# sourceMappingURL=index.js.map