UNPKG

motion-dom

Version:
3 lines (2 loc) • 5.07 kB
const e={};class t{constructor(){this.subscriptions=[]}add(e){var t,s;return t=this.subscriptions,s=e,-1===t.indexOf(s)&&t.push(s),()=>this.remove(e)}remove(e){!function(e,t){const s=e.indexOf(t);s>-1&&e.splice(s,1)}(this.subscriptions,e)}notify(e,t,s){const i=this.subscriptions.length;if(i)if(1===i)this.subscriptions[0](e,t,s);else for(let n=0;n<i;n++){const i=this.subscriptions[n];i&&i(e,t,s)}}getSize(){return this.subscriptions.length}clear(){this.subscriptions.length=0}}const s=["setup","read","resolveKeyframes","preUpdate","update","preRender","render","postRender"];function i(t,i,n={delta:0,timestamp:0,isProcessing:!1}){let r=!1,a=!0;const o=()=>r=!0,c=s.reduce((e,t)=>(e[t]=function(e){let t,s=new Set,i=new Set,n=!1,r=!1;const a=new Set;let o={delta:0,timestamp:0,isProcessing:!1};function c(s){a.has(s)&&(i.add(s),e()),(t=s)(o)}const h={schedule:(e,r=!1,o=!1)=>{const c=o&&n,h=c?s:i;return r&&a.add(e),c&&e!==t&&h.delete(e),h.add(e),e},cancel:e=>{i.delete(e),a.delete(e)},process:e=>{if(o=e,n)return void(r=!0);n=!0;const a=s;s=i,i=a,s.forEach(c),s.clear(),t=void 0,n=!1,r&&(r=!1,h.process(e))}};return h}(o),e),{}),{setup:h,read:d,resolveKeyframes:u,preUpdate:p,update:l,preRender:v,render:m,postRender:f}=c,g=()=>{const s=e.useManualTiming,o=s?n.timestamp:performance.now();r=!1,s||(n.delta=a?1e3/60:Math.max(Math.min(o-n.timestamp,40),1)),n.timestamp=o,n.isProcessing=!0,h.process(n),d.process(n),u.process(n),p.process(n),l.process(n),v.process(n),m.process(n),f.process(n),n.isProcessing=!1,r&&i&&(a=!1,t(g))};return{schedule:s.reduce((e,s)=>{const i=c[s];return e[s]=(e,s=!1,o=!1)=>(r||(r=!0,a=!0,n.isProcessing||t(g)),i.schedule(e,s,o)),e},{}),cancel:e=>{for(let t=0;t<s.length;t++)c[s[t]].cancel(e)},state:n,steps:c}}const n={delta:0,timestamp:0,isProcessing:!1},{schedule:r}=i("undefined"!=typeof requestAnimationFrame?requestAnimationFrame:e=>e,!0,n);let a;function o(){a=void 0}const c={now:()=>(void 0===a&&c.set(n.isProcessing||e.useManualTiming?n.timestamp:performance.now()),a),set:e=>{a=e,queueMicrotask(o)}},h={current:void 0};class d{constructor(e,t={}){this.canTrackVelocity=null,this.events={},this.updateAndNotify=e=>{const t=c.now();if(this.updatedAt!==t&&this.setPrevFrameValue(),this.prev=this.current,this.setCurrent(e),this.current!==this.prev&&(this.dirty(),this.dependents))for(const e of this.dependents)e.dirty()},this.hasAnimated=!1,this.setCurrent(e),this.owner=t.owner}setCurrent(e){var t;this.current=e,this.updatedAt=c.now(),null===this.canTrackVelocity&&void 0!==e&&(this.canTrackVelocity=(t=this.current,!isNaN(parseFloat(t))))}setPrevFrameValue(e=this.current){this.prevFrameValue=e,this.prevUpdatedAt=this.updatedAt}onChange(e){return this.on("change",e)}on(e,s){var i;return"change"===e?this.onChangeSubscribe(s):((i=this.events)[e]||(i[e]=new t)).add(s)}onChangeSubscribe(e){const{events:s}=this;return s.change||this.changeSubscriber?(s.change||(s.change=new t,s.change.add(this.changeSubscriber),this.changeSubscriber=void 0),s.change.add(e)):this.changeSubscriber=e,()=>{this.changeSubscriber===e?this.changeSubscriber=void 0:s.change?.remove(e),this.stopIfUnobserved()}}stopIfUnobserved(){r.read(()=>{this.changeSubscriber||this.events.change?.getSize()||this.stop()})}clearListeners(){this.changeSubscriber=void 0;for(const e in this.events)this.events[e].clear()}attach(e,t){this.passiveEffect=e,this.stopPassiveEffect=t}set(e){this.passiveEffect?this.passiveEffect(e,this.updateAndNotify):this.updateAndNotify(e)}setWithVelocity(e,t,s){this.set(t),this.prev=void 0,this.prevFrameValue=e,this.prevUpdatedAt=this.updatedAt-s}jump(e,t=!0){t&&(this.updatedAt=c.now(),this.stop()),this.updateAndNotify(e),this.prev=e,this.prevUpdatedAt=this.prevFrameValue=void 0,this.stopPassiveEffect?.()}dirty(){const{current:e,changeSubscriber:t}=this;t?t(e):this.events.change?.notify(e)}addDependent(e){(this.dependents||(this.dependents=new Set)).add(e)}removeDependent(e){this.dependents?.delete(e)}get(){h.current?.push(this);const e=this.animation?.liveValue?.();return void 0===e?this.current:e}getCurrent(){return this.current}getPrevious(){return this.prev}getVelocity(){const e=c.now();if(!this.canTrackVelocity||void 0===this.prevFrameValue||e-this.updatedAt>30)return 0;const t=Math.min(this.updatedAt-this.prevUpdatedAt,30);return s=parseFloat(this.current)-parseFloat(this.prevFrameValue),(i=t)?s*(1e3/i):0;var s,i}start(e){return new Promise(t=>this.run(e,t))}run(e,t){this.stop(),this.hasAnimated=!0;let s,i=!1;s=e(()=>{i=!0,this.events.animationComplete?.notify(),this.animation===s&&(this.animation=void 0),t?.()}),i||(this.animation=s),this.events.animationStart?.notify()}stop(){this.animation&&(this.animation.stop(),this.events.animationCancel?.notify()),this.animation=void 0}isAnimating(){return!!this.animation}destroy(){this.dependents?.clear(),this.events.destroy?.notify(),this.clearListeners(),this.stop(),this.stopPassiveEffect?.()}}function u(e,t){return new d(e,t)}export{d as MotionValue,h as collectMotionValues,u as motionValue}; //# sourceMappingURL=size-rollup-motion-value.js.map