mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Replace per-date-badge intervals with one reference-counted ticker. Unsubscribing is idempotent, and destroying the final subscriber clears the shared interval so removed views leave no background timer behind. Thanks to xet7 !
36 lines
740 B
JavaScript
36 lines
740 B
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { ReactiveVar } from 'meteor/reactive-var';
|
|
|
|
const TICK_MS = 60000;
|
|
const sharedNow = new ReactiveVar(new Date());
|
|
let intervalId = null;
|
|
let subscriberCount = 0;
|
|
|
|
function tick() {
|
|
sharedNow.set(new Date());
|
|
}
|
|
|
|
export function subscribeDateNowTicker() {
|
|
subscriberCount += 1;
|
|
|
|
if (subscriberCount === 1) {
|
|
tick();
|
|
intervalId = Meteor.setInterval(tick, TICK_MS);
|
|
}
|
|
|
|
let subscribed = true;
|
|
return {
|
|
now: sharedNow,
|
|
unsubscribe() {
|
|
if (!subscribed) return;
|
|
subscribed = false;
|
|
subscriberCount -= 1;
|
|
|
|
if (subscriberCount === 0 && intervalId !== null) {
|
|
Meteor.clearInterval(intervalId);
|
|
intervalId = null;
|
|
}
|
|
},
|
|
};
|
|
}
|