All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.react-utils.useTimer.useTimer.js.map Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
{"version":3,"file":"useTimer.js","names":["useRef","useSyncExternalStore","isNotEmpty","subscriptions","interval","initiateInterval","setInterval","forEach","callback","cleanupInterval","clearInterval","subscribe","push","splice","indexOf","useTimer","delay","arguments","length","undefined","lastUpdatedRef","Date","now","lastUpdated","current"],"sources":["../../../src/react-utils/useTimer/useTimer.js"],"sourcesContent":["import { useRef, useSyncExternalStore } from \"react\";\n\nimport { isNotEmpty } from \"neetocist\";\n\nconst subscriptions = [];\nlet interval = null;\n\nconst initiateInterval = () => {\n  // Create new interval if there are no subscriptions.\n  if (isNotEmpty(subscriptions)) return;\n  interval = setInterval(() => {\n    subscriptions.forEach(callback => callback());\n  }, 1000);\n};\n\nconst cleanupInterval = () => {\n  // Cleanup existing interval if there are no more subscriptions\n  if (isNotEmpty(subscriptions)) return;\n  clearInterval(interval);\n};\n\nconst subscribe = callback => {\n  initiateInterval();\n  subscriptions.push(callback);\n\n  return () => {\n    subscriptions.splice(subscriptions.indexOf(callback), 1);\n    cleanupInterval();\n  };\n};\n\nconst useTimer = (delay = 60) => {\n  const lastUpdatedRef = useRef(Date.now());\n\n  return useSyncExternalStore(subscribe, () => {\n    const now = Date.now();\n    let lastUpdated = lastUpdatedRef.current;\n    if (now - lastUpdated >= delay * 1000) lastUpdated = now;\n    lastUpdatedRef.current = lastUpdated;\n\n    return lastUpdated;\n  });\n};\n\nexport default useTimer;\n"],"mappings":"AAAA,SAASA,MAAM,EAAEC,oBAAoB,QAAQ,OAAO;AAEpD,SAASC,UAAU;AAEnB,IAAMC,aAAa,GAAG,EAAE;AACxB,IAAIC,QAAQ,GAAG,IAAI;AAEnB,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAgBA,CAAA,EAAS;EAC7B;EACA,IAAIH,UAAU,CAAC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