react-compound-timer
Version:
React hooks for timers, countdowns, and stopwatches.
2 lines • 5.12 kB
JavaScript
var V=Object.defineProperty,k=Object.defineProperties;var w=Object.getOwnPropertyDescriptors;var U=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,O=Object.prototype.propertyIsEnumerable;var C=(e,t,i)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i,r=(e,t)=>{for(var i in t||(t={}))x.call(t,i)&&C(e,i,t[i]);if(U)for(var i of U(t))O.call(t,i)&&C(e,i,t[i]);return e},c=(e,t)=>k(e,w(t));function b(e,t,i="ms"){let o=["ms","s","m","h","d"],a=o.findIndex(n=>n===t),s=o.findIndex(n=>n===i);if(s>a)throw new Error("roundUnitIndex must be less or equal than lastUnitIndex");let d=[1e3,60,60,24,1],T=[1,1e3,6e4,36e5,864e5],S={ms:0,s:0,m:0,h:0,d:0},h=o.reduce((n,l,u)=>(u>a?n[l]=0:u===a?n[l]=Math.floor(e/T[u]):n[l]=Math.floor(e/T[u])%d[u],n),S);if(s>0){let n=s-1,l=o[n];h[l]>=d[n]/2&&(h[i]=h[i]+1);for(let I=0;I<s;I++)h[o[I]]=0}return h}import{useEffect as D,useMemo as E,useState as y}from"react";var R=e=>{let[t,i]=y(e.value);return D(()=>{i(e.value);let a=s=>{i(s)};return e.events=c(r({},e.events),{onChange:a}),()=>{e.events=c(r({},e.events),{onChange:void 0})}},[e]),E(()=>({value:t,changeTime:e.changeTime,changeLastUnit:e.changeLastUnit,changeTimeToUpdate:e.changeTimeToUpdate,changeDirection:e.changeDirection,changeCheckpoints:e.changeCheckpoints,start:e.start,pause:e.pause,resume:e.resume,stop:e.stop,reset:e.reset}),[e,t])};function p(){return Date.now()}var m="INITED",f="PLAYING",M="PAUSED",P="STOPPED",v=class{constructor(t=m,i=()=>{}){this.state=m;this.state=t,this.onStateChange=i}setOnStateChange(t){this.onStateChange=t}getState(){return this.state}setInited(){return this.state===m?!1:(this.state=m,this.onChange(),!0)}isInited(){return this.state===m}setPlaying(){return this.state===f?!1:(this.state=f,this.onChange(),!0)}isPlaying(){return this.state===f}setPaused(){return this.state!==f?!1:(this.state=M,this.onChange(),!0)}isPaused(){return this.state===M}setStopped(){return this.state===m?!1:(this.state=P,this.onChange(),!0)}isStopped(){return this.state===P}onChange(){this.onStateChange({state:this.state})}};var g=class{constructor(t,i={}){this.changeTime=t=>{this.internalTime=p(),this.options.initialTime=t,this.time=this.options.initialTime,this.events.onChange&&this.events.onChange(this.getTimerValue(this.time))};this.changeLastUnit=t=>{this.innerState.isPlaying()?(this.pause(),this.options.lastUnit=t,this.resume(!0)):this.options.lastUnit=t};this.changeRoundUnit=t=>{this.innerState.isPlaying()?(this.pause(),this.options.roundUnit=t,this.resume(!0)):this.options.roundUnit=t};this.changeTimeToUpdate=t=>{this.innerState.isPlaying()?(this.pause(),this.options.timeToUpdate=t,this.resume()):this.options.timeToUpdate=t};this.changeDirection=t=>{this.options.direction=t};this.changeCheckpoints=t=>{this.options.checkpoints=t};this.start=()=>{this.innerState.setPlaying()&&(this.setTimerInterval(!0),this.events.onStart&&this.events.onStart())};this.resume=(t=!1)=>{this.innerState.isPaused()&&this.innerState.setPlaying()&&(this.setTimerInterval(t),this.events.onResume&&this.events.onResume())};this.pause=()=>{this.innerState.setPaused()&&(this.timerId&&clearInterval(this.timerId),this.events.onPause&&this.events.onPause())};this.stop=()=>{this.innerState.setStopped()&&(this.timerId&&clearInterval(this.timerId),this.events.onStop&&this.events.onStop())};this.reset=()=>{this.time=this.options.initialTime,this.events.onChange&&this.events.onChange(this.getTimerValue(this.time)),this.events.onReset&&this.events.onReset()};this.setTimerInterval=(t=!1)=>{this.timerId&&clearInterval(this.timerId),this.internalTime=p();let i=()=>{let o=this.time,a=this.computeTime();this.events.onChange&&this.events.onChange(this.getTimerValue(a)),this.options.checkpoints.map(({time:s,callback:d})=>{let T=s>o&&s<=a,S=s<o&&s>=a;(this.options.direction==="backward"?S:T)&&d()})};t&&this.events.onChange&&this.events.onChange(this.getTimerValue(this.time)),this.timerId=window.setInterval(i,this.options.timeToUpdate)};this.getTimerValue=t=>c(r({},b(t,this.options.lastUnit,this.options.roundUnit)),{state:this.innerState.getState()});this.computeTime=()=>{if(this.innerState.isPlaying()){let t=p(),i=Math.abs(t-this.internalTime);switch(this.options.direction){case"forward":return this.time=this.time+i,this.internalTime=t,this.time;case"backward":return this.time=this.time-i,this.internalTime=t,this.time<0?(this.stop(),0):this.time;default:return this.time}}return this.time<0?0:this.time};this.internalTime=p(),this.options=t,this.events=i,this.time=t.initialTime,this.innerState=new v("INITED",()=>{this.events.onChange&&this.events.onChange(this.value)}),this.timerId=null,this.options.startImmediately&&this.start()}get value(){return this.getTimerValue(this.computeTime())}get currentOptions(){return JSON.parse(JSON.stringify(this.options))}};var N={initialTime:0,direction:"forward",timeToUpdate:250,startImmediately:!0,lastUnit:"d",roundUnit:"ms",checkpoints:[]},A=(e={},t={})=>{let i=r(r({},N),e);return new g(i,t)};export{A as createTimeModel,b as getTimeParts,R as useTimeModel};
//# sourceMappingURL=react-compound-timer.browser.mjs.map