{"version":3,"file":"useNow.mjs","names":[],"sources":["../../../../package/common/calendar/useNow.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { isSameDay, startOfDay } from './dates';\n\nfunction read(day: boolean): Date {\n  return day ? startOfDay(new Date()) : new Date();\n}\n\n/**\n * Checked on each minute boundary rather than once at midnight: a long timer is paused while the\n * device sleeps, and would mark the old day for hours after waking.\n */\nfunction useClock(day: boolean): Date {\n  const [now, setNow] = useState(() => read(day));\n  useEffect(() => {\n    let timer: ReturnType<typeof setTimeout>;\n    function schedule() {\n      timer = setTimeout(\n        () => {\n          const next = read(day);\n          // The same object while the day lasts, so nothing re-renders for it.\n          setNow((previous) => (day && isSameDay(previous, next) ? previous : next));\n          schedule();\n        },\n        60_000 - (Date.now() % 60_000),\n      );\n    }\n    schedule();\n    return () => clearTimeout(timer);\n  }, [day]);\n  return now;\n}\n\n/** The current time, updated on each minute boundary: it carries the now-line through the day. */\nexport function useNow(): Date {\n  return useClock(false);\n}\n\n/** Midnight of today, updated when the day changes: today's mark, for a calendar left open overnight. */\nexport function useToday(): Date {\n  return useClock(true);\n}\n"],"mappings":"qHAIA,SAAS,EAAK,EAAoB,CAChC,OAAO,EAAM,EAAW,IAAI,IAAM,EAAI,IAAI,IAC5C,CAMA,SAAS,EAAS,EAAoB,CACpC,GAAM,CAAC,EAAK,GAAU,MAAe,EAAK,CAAG,CAAC,EAiB9C,OAhBA,MAAgB,CACd,IAAI,EACJ,SAAS,GAAW,CAClB,EAAQ,eACA,CACJ,IAAM,EAAO,EAAK,CAAG,EAErB,EAAQ,GAAc,GAAO,EAAU,EAAU,CAAI,EAAI,EAAW,CAAK,EACzE,EAAS,CACX,EACA,IAAU,KAAK,IAAI,EAAI,GACzB,CACF,CAEA,OADA,EAAS,MACI,aAAa,CAAK,CACjC,EAAG,CAAC,CAAG,CAAC,EACD,CACT,CAGA,SAAgB,GAAe,CAC7B,OAAO,EAAS,EAAK,CACvB,CAGA,SAAgB,GAAiB,CAC/B,OAAO,EAAS,EAAI,CACtB"}