@nent/core
Version:
4 lines • 2.92 kB
JavaScript
/*!
* NENT 2022
*/
import{r as t,d as s,h as i,a as h,H as o}from"./index-916ca544.js";import{E as r,e as n}from"./index-f7016b94.js";import{R as e}from"./interfaces-3b78db83.js";import{s as a}from"./state-adf07580.js";import{T as d}from"./interfaces-aed4a5ac.js";import{g as l}from"./time-d244c045.js";import{f as c}from"./logging-5a93c8af.js";import{t as m}from"./promises-584c4ece.js";import"./index-4bfabbbd.js";class p extends r{constructor(t,s,i,h=performance.now,o=null,r=!1){super(),this.provider=t,this.interval=s,this.durationSeconds=i,this.getStart=h,this.onInterval=o,this.debug=r,this.timer=0,this.start=0,this.durationMs=0,this.durationMs=1e3*this.durationSeconds,c(this.debug,`presentation-timer: starting timer w/ ${this.durationSeconds} duration`),this.currentTime=l(this.start,this.start,this.durationMs),this.debouncedInterval=this.interval>0?m(this.interval,(()=>{this.timer=this.provider.requestAnimationFrame((t=>{this.doInterval(t)}))}),!0,!0):()=>{this.timer=this.provider.requestAnimationFrame((t=>{this.doInterval(t)}))}}begin(){this.timer&&this.stop(),this.start=this.getStart(),this.currentTime=l(this.start,this.start,this.durationMs),this.provider.requestAnimationFrame((async t=>{await this.doInterval(t)}))}stop(){this.provider.cancelAnimationFrame(this.timer)}async doInterval(t){var s;this.currentTime=l(this.start,t,this.durationMs),this.currentTime.ended?(this.stop(),this.emit(d.OnEnd,this.currentTime)):(this.emit(d.OnInterval,this.currentTime),await this.debouncedInterval()),null===(s=this.onInterval)||void 0===s||s.call(this)}destroy(){this.stop(),this.removeAllListeners()}}const u=class{constructor(i){t(this,i),this.ready=s(this,"ready",7),this.elapsed=0,this.debug=!1,this.duration=0,this.interval=200,this.display=!1,this.deferLoad=!1,this.now=()=>performance.now()}get currentRoute(){var t;const s=this.el.closest("n-view-prompt")||this.el.closest("n-view");return s?s.route:(null===(t=a.router)||void 0===t?void 0:t.exactRoute)||null}async begin(){this.timer.begin()}async stop(){this.timer.stop()}componentWillLoad(){this.timer=new p(window,this.interval,this.duration,(()=>this.now()),(()=>{this.elapsed=this.timer.currentTime.elapsedSeconds}),this.debug)}render(){return i(o,null,this.display?this.elapsed:null)}componentDidLoad(){var t,s;this.ready.emit(!0),this.currentRoute?(c(this.debug,`n-presentation-timer: syncing to route ${this.currentRoute.path}`),(null===(s=null===(t=this.currentRoute)||void 0===t?void 0:t.match)||void 0===s?void 0:s.isExact)&&this.timer.begin(),this.navigationSubscription=n.on(e.RouteChanged,(()=>{var t,s;(null===(s=null===(t=this.currentRoute)||void 0===t?void 0:t.match)||void 0===s?void 0:s.isExact)?this.timer.begin():this.timer.stop()}))):this.timer.begin()}disconnectedCallback(){var t;this.timer.destroy(),null===(t=this.navigationSubscription)||void 0===t||t.call(this)}get el(){return h(this)}};export{u as n_presentation_timer}