UNPKG

svelte-ux

Version:

- Increment version in `package.json` and commit as `Version bump to x.y.z` - `npm run publish`

47 lines (46 loc) 1.15 kB
import { writable } from 'svelte/store'; /** * Subscribable timer store */ export default function timerStore(options = {}) { var _a; let intervalId = null; let delay = (_a = options.delay) !== null && _a !== void 0 ? _a : 1000; const state = writable(null, () => { if (!options.disabled) { start(); } return () => stop(); }); function start() { stop(); intervalId = setInterval(() => { var _a, _b; // console.log('tick'); state.set((_b = (_a = options.onTick) === null || _a === void 0 ? void 0 : _a.call(options)) !== null && _b !== void 0 ? _b : new Date()); }, delay); } function stop() { clearInterval(intervalId); intervalId = null; } function isRunning() { return intervalId != null; } function getDelay() { return delay; } function setDelay(value) { stop(); delay = value; start(); } return { subscribe: state.subscribe, start, stop, isRunning, getDelay, setDelay, }; }