@revenuecat/purchases-ui-js
Version:
Web components for Paywalls. Powered by RevenueCat
21 lines (20 loc) • 757 B
JavaScript
import { readable } from "svelte/store";
/**
* Wall-clock now (epoch ms) shared by every countdown so they tick in lockstep.
* The interval runs only while at least one countdown is subscribed: Svelte's
* `readable` starts on the first subscriber and stops on the last.
*/
export const sharedNow = readable(Date.now(), (set) => {
set(Date.now());
// Align the first tick to the whole second, then tick every second.
let interval;
const boundaryTimeout = setTimeout(() => {
set(Date.now());
interval = setInterval(() => set(Date.now()), 1000);
}, 1000 - (Date.now() % 1000));
return () => {
clearTimeout(boundaryTimeout);
if (interval !== undefined)
clearInterval(interval);
};
});