@uiloos/core
Version:
The core of the uiloos headless UI
2 lines (1 loc) • 5.01 kB
JavaScript
var uiloosViewChannel=function(t){"use strict";const s="uiloos > ViewChannel >";class i extends Error{constructor(){super(`${s} autoDismiss > duration cannot be negative or zero`),this.name="ViewChannelAutoDismissDurationError"}}class e{constructor(t,s){this.t=null,this.i=new Date,this.h=null,this.o=0,this.u=null,this.l=t,this.u=s}D(t){if(this.l.autoDismiss.isPlaying)return;if(this._(),!this.u)return;const s=this.I(this.u);if(s<=0)throw new i;this.o=s,this.i=new Date,null===this.h&&(this.l.autoDismiss.duration=s);const e=this.u.result;if(this.t=window.setTimeout((()=>{this.l.isPresented&&(this.l.autoDismiss.isPlaying=!1,this.l.autoDismiss.duration=0,this.l.viewChannel.m(this.l.index,e,"AUTO_DISMISS")),this.t=null}),s),this.l.result.then((()=>{this._()})),this.l.autoDismiss.isPlaying=!0,t){const t={type:"AUTO_DISMISS_PLAYING",view:this.l,index:this.l.index,time:new Date};this.l.viewChannel.S(t)}}p(){if(!this.l.autoDismiss.isPlaying)return;this.h=new Date,this._(),this.l.autoDismiss.isPlaying=!1;const t={type:"AUTO_DISMISS_PAUSED",view:this.l,index:this.l.index,time:new Date};this.l.viewChannel.S(t)}v(){if(!this.l.autoDismiss.isPlaying&&!this.h)return;this._(),this.h=null,this.l.autoDismiss.isPlaying=!1,this.l.autoDismiss.duration=0;const t={type:"AUTO_DISMISS_STOPPED",view:this.l,index:this.l.index,time:new Date};this.l.viewChannel.S(t)}_(){null!==this.t&&(window.clearTimeout(this.t),this.t=null)}I(t){return this.h?this.o-(this.h.getTime()-this.i.getTime()):t.duration}}class h{constructor(t,s,i,h,n){var r;this.autoDismiss={isPlaying:!1,duration:0},this.A=null,this.isPresented=!0,this.viewChannel=t,this.index=s,this.data=i,this.priority=h,this.result=new Promise((t=>{this.A=t})),this.g=new e(this,n),this.g.D(!1),this.autoDismiss.duration=null!==(r=null==n?void 0:n.duration)&&void 0!==r?r:0}dismiss(t){this.viewChannel.dismiss(this,t)}play(){this.g.D(!0)}pause(){this.g.p()}stop(){this.g.v()}changeData(t){this.viewChannel.changeData(this,t)}}class n{constructor(){this.T=[],this.C=0}V(t){this.C>0&&(this.T.push(t),this.T.length-1===this.C&&this.T.shift())}P(t=0){this.C=t}}class r{constructor(){this.O=[]}$(t){return this.O.push(t),()=>{this.N(t)}}N(t){this.O=this.O.filter((s=>t!==s))}B(){this.O.length=0}S(t,s){this.O.forEach((i=>i(t,s)))}}class o extends Error{constructor(t){super(`${s} ${t} > "index" is out of bounds`),this.name="ViewChannelIndexOutOfBoundsError"}}class c extends Error{constructor(t){super(`${s} ${t} > "ViewChannelView" not found in views array`),this.name="ViewChannelViewNotFoundError"}}return t.ViewChannel=class{constructor(t={},s){this.views=[],this.L=new n,this.history=this.L.T,this.M=new r,uiloosLicenseChecker.licenseChecker._checkLicense(),s&&this.subscribe(s),this.initialize(t)}initialize(t){this.L.T.length=0,this.L.P(t.keepHistoryFor),this.U();const s={type:"INITIALIZED",time:new Date};this.S(s)}subscribe(t){return this.M.$(t)}unsubscribe(t){this.M.N(t)}unsubscribeAll(){this.M.B()}present(t){const s=t.priority?t.priority:0,i=Array.isArray(s)?s:[s],e=this.R(i),n=new h(this,e,t.data,i,t.autoDismiss);this.views.splice(e,0,n),this.F();const r={type:"PRESENTED",view:n,index:e,time:new Date};return this.S(r),n}m(t,s,i){if(t<0||t>=this.views.length)throw new o("dismissByIndex");const e=this.views[t];this.views.splice(t,1),this.F(),e.isPresented=!1,e.autoDismiss.duration=0,e.autoDismiss.isPlaying=!1,e.A(s);const h={type:"DISMISSED",view:e,index:t,reason:i,time:new Date};this.S(h)}dismissByIndex(t,s){this.m(t,s,"USER_INTERACTION")}dismiss(t,s){if(!t.isPresented)return;const i=this.views.indexOf(t);if(-1===i)throw new c("dismiss");this.dismissByIndex(i,s)}dismissAll(t){if(0===this.views.length)return;const s=[],i=[...this.views];this.U(),i.forEach((i=>{i.isPresented=!1,i.A(t),s.push(i.index)}));const e={type:"DISMISSED_ALL",views:i,indexes:s,time:new Date};this.S(e)}changeDataByIndex(t,s){if(t<0||t>=this.views.length)throw new o("changeDataByIndex");const i=this.views[t];i.data=s;const e={type:"DATA_CHANGED",view:i,data:s,index:i.index,time:new Date};this.S(e)}changeData(t,s){const i=this.views.indexOf(t);if(-1===i)throw new c("changeData");this.changeDataByIndex(i,s)}R(t){for(let s of this.views){const i=t.length>s.priority.length?t:s.priority;for(let e=0;e<i.length;e++){if(this.G(t,e)<this.G(s.priority,e))return s.index}}return this.views.length}G(t,s){const i=t[s];return void 0!==i?i:0}F(){this.views.forEach(((t,s)=>{t.index=s}))}U(){this.views.length=0}S(t){this.L.V(t),this.M.S(this,t)}},t.ViewChannelAutoDismissDurationError=i,t.ViewChannelIndexOutOfBoundsError=o,t.ViewChannelView=h,t.ViewChannelViewNotFoundError=c,t.createViewChannelSubscriber=function(t){return(s,i)=>{!function(t,s,i,e){const h="on"+s.type.toLowerCase().split("_").reduce(((t,s)=>{const i=s.split("");return i[0]=i[0].toUpperCase(),t+i.join("")}),""),n=e[h];n?n(s,i):e.debug&&console.warn(`uiloos > ${t} event '${s.type}' was fired but '${h}' method is not implemented, this might not be correct.`)}("createViewChannelSubscriber",i,s,t)}},t}({});