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

package.cjs.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":["_react","require","_neetoCist","subscriptions","interval","initiateInterval","isNotEmpty","setInterval","forEach","callback","cleanupInterval","clearInterval","subscribe","push","splice","indexOf","useTimer","delay","arguments","length","undefined","lastUpdatedRef","useRef","Date","now","useSyncExternalStore","lastUpdated","current","_default","exports"],"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,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,UAAA,GAAAD,OAAA;AAEA,IAAME,aAAa,GAAG,EAAE;AACxB,IAAIC,QAAQ,GAAG,IAAI;AAEnB,IAAMC,gBAAgB,GAAG,SAAnBA,gBAAgBA,CAAA,EAAS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