source: imaps-frontend/node_modules/set-harmonic-interval/lib/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var counter = 0;
6var buckets = {};
7var setHarmonicInterval = function (fn, ms) {
8 var _a;
9 var id = counter++;
10 if (buckets[ms]) {
11 buckets[ms].listeners[id] = fn;
12 }
13 else {
14 var timer = setInterval(function () {
15 var listeners = buckets[ms].listeners;
16 var didThrow = false;
17 var lastError;
18 for (var _i = 0, _a = Object.values(listeners); _i < _a.length; _i++) {
19 var listener = _a[_i];
20 try {
21 listener();
22 }
23 catch (error) {
24 didThrow = true;
25 lastError = error;
26 }
27 }
28 if (didThrow)
29 throw lastError;
30 }, ms);
31 buckets[ms] = {
32 ms: ms,
33 timer: timer,
34 listeners: (_a = {},
35 _a[id] = fn,
36 _a),
37 };
38 }
39 return {
40 bucket: buckets[ms],
41 id: id,
42 };
43};
44var clearHarmonicInterval = function (_a) {
45 var bucket = _a.bucket, id = _a.id;
46 delete bucket.listeners[id];
47 var hasListeners = false;
48 for (var listener in bucket.listeners) {
49 hasListeners = true;
50 break;
51 }
52 if (!hasListeners) {
53 clearInterval(bucket.timer);
54 delete buckets[bucket.ms];
55 }
56};
57
58exports.clearHarmonicInterval = clearHarmonicInterval;
59exports.setHarmonicInterval = setHarmonicInterval;
Note: See TracBrowser for help on using the repository browser.