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

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

Update repo after prototype presentation

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