@timekeeper-countdown/react 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ import { CountdownEngineOptions, CountdownSnapshot, TimerState } from '@timekeeper-countdown/core';
2
+
3
+ interface UseCountdownOptions extends Omit<CountdownEngineOptions, 'onSnapshot' | 'onStateChange' | 'onError'> {
4
+ autoStart?: boolean;
5
+ onSnapshot?: CountdownEngineOptions['onSnapshot'];
6
+ onStateChange?: CountdownEngineOptions['onStateChange'];
7
+ onError?: CountdownEngineOptions['onError'];
8
+ }
9
+ interface UseCountdownControls {
10
+ start: () => boolean;
11
+ pause: () => boolean;
12
+ resume: () => boolean;
13
+ reset: (nextInitialSeconds?: number) => boolean;
14
+ stop: () => boolean;
15
+ setSeconds: (value: number) => void;
16
+ }
17
+ interface UseCountdownResult extends UseCountdownControls {
18
+ snapshot: CountdownSnapshot;
19
+ state: TimerState;
20
+ totalSeconds: number;
21
+ parts: CountdownSnapshot['parts'];
22
+ isRunning: boolean;
23
+ isCompleted: boolean;
24
+ }
25
+ declare function useCountdown(initialSeconds: number, options?: UseCountdownOptions): UseCountdownResult;
26
+
27
+ export { type UseCountdownControls, type UseCountdownOptions, type UseCountdownResult, useCountdown };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {CountdownEngine}from'@timekeeper-countdown/core';var M=(a,p)=>{let u=CountdownEngine(a,{tickIntervalMs:p.tickIntervalMs,timeProvider:p.timeProvider}),i=u.getSnapshot();return u.destroy(),i};function P(a,p={}){let{autoStart:u=false,tickIntervalMs:i,timeProvider:c,onSnapshot:l,onStateChange:C,onError:S}=p,d=useRef({onSnapshot:l,onStateChange:C,onError:S}),g=useRef(null),[r,m]=useState(()=>M(a,{tickIntervalMs:i,timeProvider:c}));useEffect(()=>{d.current={onSnapshot:l,onStateChange:C,onError:S};},[S,l,C]);let w=useMemo(()=>({tickIntervalMs:i,timeProvider:c}),[i,c]);useEffect(()=>{let n=true,e=CountdownEngine(a,{...w,onSnapshot:o=>{n&&m(o),d.current.onSnapshot?.(o);},onStateChange:(o,y)=>{d.current.onStateChange?.(o,y);},onError:o=>{d.current.onError?.(o);}});return g.current=e,m(e.getSnapshot()),u&&e.start(),()=>{n=false,e.destroy(),g.current=null;}},[u,w,a]);let t=useCallback((n,e)=>{let o=g.current;return o?n(o):e},[]),E=useCallback(()=>t(n=>n.start(),false),[t]),v=useCallback(()=>t(n=>n.pause(),false),[t]),O=useCallback(()=>t(n=>n.resume(),false),[t]),U=useCallback(()=>t(n=>n.stop(),false),[t]),I=useCallback(n=>t(e=>e.setSeconds(n),void 0),[t]),R=useCallback(n=>t(e=>e.reset(n),false),[t]);return {snapshot:r,state:r.state,totalSeconds:r.totalSeconds,parts:r.parts,isRunning:r.isRunning,isCompleted:r.isCompleted,start:E,pause:v,resume:O,reset:R,stop:U,setSeconds:I}}export{P as useCountdown};
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@timekeeper-countdown/react",
3
+ "version": "0.1.0",
4
+ "description": "React adapter for the Timekeeper Countdown engine",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "dev": "tsup --watch",
21
+ "test": "vitest run",
22
+ "test:ui": "vitest --ui",
23
+ "test:coverage": "vitest run --coverage",
24
+ "typecheck": "tsc --noEmit",
25
+ "lint": "eslint .",
26
+ "lint:fix": "eslint . --fix",
27
+ "format": "prettier --write \"src/**/*.{ts,tsx}\"",
28
+ "format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
29
+ "prepublishOnly": "npm run build && npm run test && npm run typecheck"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=17.0.0"
33
+ },
34
+ "dependencies": {
35
+ "@timekeeper-countdown/core": "^0.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@testing-library/jest-dom": "^6.4.2",
39
+ "@testing-library/react": "^15.0.0",
40
+ "@types/react": "^18.2.55",
41
+ "@types/react-dom": "^18.2.19",
42
+ "react": "^18.3.1",
43
+ "react-dom": "^18.3.1"
44
+ },
45
+ "keywords": [
46
+ "react",
47
+ "countdown",
48
+ "timer",
49
+ "hook",
50
+ "typescript"
51
+ ],
52
+ "sideEffects": false,
53
+ "publishConfig": {
54
+ "registry": "https://registry.npmjs.org",
55
+ "access": "public"
56
+ }
57
+ }