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.3 KB
|
Rev | Line | |
---|
[d565449] | 1 | let counter = 0;
|
---|
| 2 | const buckets = {};
|
---|
| 3 | const setHarmonicInterval = (fn, ms) => {
|
---|
| 4 | const id = counter++;
|
---|
| 5 | if (buckets[ms]) {
|
---|
| 6 | buckets[ms].listeners[id] = fn;
|
---|
| 7 | }
|
---|
| 8 | else {
|
---|
| 9 | const timer = setInterval(() => {
|
---|
| 10 | const { listeners } = buckets[ms];
|
---|
| 11 | let didThrow = false;
|
---|
| 12 | let lastError;
|
---|
| 13 | for (const listener of Object.values(listeners)) {
|
---|
| 14 | try {
|
---|
| 15 | listener();
|
---|
| 16 | }
|
---|
| 17 | catch (error) {
|
---|
| 18 | didThrow = true;
|
---|
| 19 | lastError = error;
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
| 22 | if (didThrow)
|
---|
| 23 | throw lastError;
|
---|
| 24 | }, ms);
|
---|
| 25 | buckets[ms] = {
|
---|
| 26 | ms,
|
---|
| 27 | timer,
|
---|
| 28 | listeners: {
|
---|
| 29 | [id]: fn,
|
---|
| 30 | },
|
---|
| 31 | };
|
---|
| 32 | }
|
---|
| 33 | return {
|
---|
| 34 | bucket: buckets[ms],
|
---|
| 35 | id,
|
---|
| 36 | };
|
---|
| 37 | };
|
---|
| 38 | const clearHarmonicInterval = ({ bucket, id }) => {
|
---|
| 39 | delete bucket.listeners[id];
|
---|
| 40 | let hasListeners = false;
|
---|
| 41 | for (const listener in bucket.listeners) {
|
---|
| 42 | hasListeners = true;
|
---|
| 43 | break;
|
---|
| 44 | }
|
---|
| 45 | if (!hasListeners) {
|
---|
| 46 | clearInterval(bucket.timer);
|
---|
| 47 | delete buckets[bucket.ms];
|
---|
| 48 | }
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | export { clearHarmonicInterval, setHarmonicInterval };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.