@mptool/enhance
Version:
Lifetime enhance for QQ/Wechat miniapp
3 lines (2 loc) • 10.3 kB
JavaScript
for(var y="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",b=typeof Uint8Array=="undefined"?[]:new Uint8Array(256),w=0;w<y.length;w++)b[y.charCodeAt(w)]=w;var ut=function(t){var e=new Uint8Array(t),n,r=e.length,o="";for(n=0;n<r;n+=3)o+=y[e[n]>>2],o+=y[(e[n]&3)<<4|e[n+1]>>4],o+=y[(e[n+1]&15)<<2|e[n+2]>>6],o+=y[e[n+2]&63];return r%3===2?o=o.substring(0,o.length-1)+"=":r%3===1&&(o=o.substring(0,o.length-2)+"=="),o},ft=function(t){var e=t.length*.75,n=t.length,r,o=0,i,c,l,a;t[t.length-1]==="="&&(e--,t[t.length-2]==="="&&e--);var s=new ArrayBuffer(e),d=new Uint8Array(s);for(r=0;r<n;r+=4)i=b[t.charCodeAt(r)],c=b[t.charCodeAt(r+1)],l=b[t.charCodeAt(r+2)],a=b[t.charCodeAt(r+3)],d[o++]=i<<2|c>>4,d[o++]=(c&15)<<4|l>>2,d[o++]=(l&3)<<6|a&63;return s};const _=typeof qq=="object"?"qq":typeof wx=="object"?"miniapp"in wx?"donut":"wx":"js";var ht=(t,e,n)=>new Promise((r,o)=>{var i=a=>{try{l(n.next(a))}catch(s){o(s)}},c=a=>{try{l(n.throw(a))}catch(s){o(s)}},l=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,c);l((n=n.apply(t,e)).next())});function A(t=new Map){return{all:t,on:(e,n)=>{const r=t.get(e);r?r.push(n):t.set(e,[n])},off:(e,n)=>{const r=t.get(e);r&&(n?r.splice(r.indexOf(n)>>>0,1):t.set(e,[]))},emit:(e,n)=>{let r=t.get(e);r&&r.slice().map(o=>o(n)),r=t.get("*"),r&&r.slice().map(o=>o(e,n))},emitAsync:(e,n)=>ht(null,null,function*(){var r,o;yield Promise.all(((r=t.get(e))!=null?r:[]).slice().map(i=>i(n))),yield Promise.all(((o=t.get("*"))!=null?o:[]).slice().map(i=>i(e,n)))})}}var pt=Object.defineProperty,dt=(t,e,n)=>e in t?pt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,gt=(t,e,n)=>dt(t,e+"",n);let $t=class extends Error{constructor({code:e,message:n}){super(n),gt(this,"code"),this.code=e!=null?e:null}toString(){return`code: ${this.code}, message: ${this.message}`}};var q;const u=_==="js"?console:((q=wx.getRealtimeLogManager)==null?void 0:q.call(wx))||wx.getLogManager({level:1}),k=_!=="js"&&"getRealtimeLogManager"in wx,mt=(...t)=>{wx.env.DEBUG&&(k?u.info("debug",...t):u.debug(...t)),u!==console&&console.debug(...t)},yt=(...t)=>{u.info(...t),u!==console&&console.info(...t)},bt=(...t)=>{u.warn(...t),u!==console&&console.warn(...t)},vt=(...t)=>{k?u.error(...t):u.warn("error",...t),u!==console&&console.error(...t)},Pt=t=>{k&&u.setFilterMsg(t)};var g=Object.freeze({__proto__:null,debug:mt,error:vt,filter:Pt,info:yt,warn:bt});const wt=(t="",e="&")=>{const n={},r=t?t.split(e):void 0;return r&&r.length>0&&r.forEach(o=>{const[i,c]=o.split("=");n[i]=c}),n},G=(t={},e="&",n=!1)=>Object.keys(t).map(r=>{const o=t[r];return`${r}=${n?o:encodeURIComponent(o)}`}).join(e),At=(t,e,n=!1)=>{const r=G(e,"&",n);return r?`${t}${/[?&]$/u.test(t)?"":t.includes("?")?"&":"?"}${r}`:t};var M=Object.freeze({__proto__:null,join:At,parse:wt,stringify:G});const B=t=>{if(t===void 0)return"undefined";const e=typeof t;if(e==="object"){if(t===null)return"null";const n=/\[object (\w+)\]/u.exec(Object.prototype.toString.call(t));return n?n[1].toLowerCase():""}return e},Q=t=>B(t)==="function";var Ot=Object.defineProperty,jt=(t,e,n)=>e in t?Ot(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,F=(t,e,n)=>jt(t,typeof e!="symbol"?e+"":e,n);function f(t,e){return function(...n){if(e.apply(this,n),t)return t.apply(this,n)}}const xt=(t,e)=>{let n;return function(...r){if(!n)return n=!0,t.apply(e!=null?e:this,[()=>{n=!1},...r])}},Et=(t,e)=>{let n;return function(...r){if(!n)return n=!0,t.apply(e!=null?e:null,r)}};class H{constructor(e=1){this.capacity=e,F(this,"funcQueue",[]),F(this,"running",0)}next(){const e=this.funcQueue.shift();if(e){const{func:n,ctx:r,args:o}=e,i=()=>{n.apply(r,[()=>{this.running-=1,this.next()},...[].slice.call(o,0)])};this.running+=1,i()}}add(e,n,...r){this.funcQueue.push({func:e,ctx:n,args:[].slice.call(r,0)}),this.running<this.capacity&&this.next()}clear(){this.funcQueue=[]}}const Ct=(t,e=1)=>{const n=new H(e);return function(...r){n.add(t,this,...r)}},R="a",z="al",W="n",J="p",K="r",V="u",h=A(),O=A(),p=A(),$={launch:!1,lOpt:{},hide:0},Lt=t=>{$.launch=!0,$.lOpt=t,h.emit(z,t)},_t=()=>{$.hide&&(h.emit(R,new Date().getTime()-$.hide),$.hide=0)},kt=()=>{$.hide=new Date().getTime()},Rt=t=>{t.onLaunch=f(t.onLaunch,Lt),t.onShow=f(t.onShow,_t),t.onHide=f(t.onHide,kt),t.onAwake&&(h.on(R,e=>{g.debug(`App: awake after ${e}ms`),t.onAwake(e)}),g.debug("App: registered onAwake")),t.$on=p.on,t.$off=p.off,t.$emit=p.emit,t.$emitAsync=p.emitAsync,App(t)},D={},Y=t=>D[t],Dt=(t,e)=>{D[t]=e},It=t=>{delete D[t]};var Nt=Object.defineProperty,Tt=Object.defineProperties,Ut=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,tt=(t,e,n)=>e in t?Nt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,et=(t,e)=>{for(var n in e||(e={}))X.call(e,n)&&tt(t,n,e[n]);if(j)for(var n of j(e))Z.call(e,n)&&tt(t,n,e[n]);return t},nt=(t,e)=>Tt(t,Ut(e)),St=(t,e)=>{var n={};for(var r in t)X.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(t!=null&&j)for(var r of j(t))e.indexOf(r)<0&&Z.call(t,r)&&(n[r]=t[r]);return n};let x;const qt=t=>{const e=t,{defaultPage:n,pages:r=[],getPath:o}=e,i=St(e,["defaultPage","pages","getPath"]);if(Q(o)){x=nt(et({},i),{getPath:o});return}let c={},l={};const a=(s,d)=>{const P=d.replace(/\$name/g,s);c[s]=P,l[P]=s};Array.isArray(r)?r.forEach(([s,d])=>{typeof s=="string"?a(s,d):s.forEach(P=>a(P,d))}):typeof r=="object"&&(c=r,l=Object.fromEntries(Object.keys(r).map(s=>[r[s],s]))),x=nt(et({},i),{getPath:s=>c[s]||n.replace(/\$name/g,s)})},m=()=>{if(!x)throw new Error("$Config is not called");return x};let v=!0;const rt=t=>{const e=m(),[n,r]=t.split("?"),o=n.startsWith("/")?n:e.getPath(n);return{path:o,query:M.parse(r),url:`${o}${r?`?${r}`:""}`}};function E(t){return e=>{if(v){v=!1;const{path:n,url:r,query:o}=rt(e);return Promise.race([O.emitAsync(`${W}:${n}`,o),new Promise(i=>{var c;setTimeout(()=>i(),(c=m().maxDelay)!=null?c:200)})]).then(()=>(v=!0,wx[t]({url:r})))}}}h.on(K,()=>{var t;setTimeout(()=>{v=!0},(t=m().minInterval)!=null?t:100)}),h.on(V,()=>{v=!0});const I=E("navigateTo"),N=E("redirectTo"),T=E("switchTab"),C=E("reLaunch");function L(t){return function(e){if(e){const{before:n,after:r,url:o}=e.currentTarget.dataset;if(this&&n&&typeof this[n]=="function"&&this[n](e),o)return t(o).then(()=>{this&&r&&typeof this[r]=="function"&&this[r](e)})}}}const Gt=L(I),Mt=L(N),Bt=L(T),Qt=L(C),ot=(t=1)=>{const{home:e}=m();return getCurrentPages().length<=t&&e?C(e):wx.navigateBack({delta:t})},Ft=function(t){if(t){const{before:e,after:n,delta:r=1}=t.currentTarget.dataset;return this&&e&&typeof this[e]=="function"&&this[e](t),Promise.resolve(ot(Number(r))).then(()=>{this&&n&&typeof this[n]=="function"&&this[n](t)})}},Ht=()=>getCurrentPages().slice(0).pop(),zt=t=>{const{path:e,query:n}=rt(t);O.emit(`${J}:${e}`,n)};function at(t){const{args:e,event:n,id:r}=t.detail;switch(n){case"$attached":{const o=Y(r);if(!o)break;const i=o.$refID;i&&(this.$refs[i]=o),o.$attached(this);break}default:{const o=this[n];o&&o.apply(this,e)}}}function it(t){const e=m();t.$=at,t.$refs={},t.$on=p.on,t.$off=p.off,t.$emit=p.emit,t.$emitAsync=p.emitAsync,t.$go=I,t.$redirect=N,t.$switch=T,t.$reLaunch=C,t.$back=ot,t.$preload=zt,t.$currentPage=Ht,t.$getPath=e.getPath,t.$bindGo=Gt,t.$bindRedirect=Mt,t.$bindSwitch=Bt,t.$bindRelaunch=Qt,t.$bindBack=Ft}var Wt=Object.defineProperty,Jt=Object.defineProperties,Kt=Object.getOwnPropertyDescriptors,st=Object.getOwnPropertySymbols,Vt=Object.prototype.hasOwnProperty,Yt=Object.prototype.propertyIsEnumerable,ct=(t,e,n)=>e in t?Wt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,U=(t,e)=>{for(var n in e||(e={}))Vt.call(e,n)&&ct(t,n,e[n]);if(st)for(var n of st(e))Yt.call(e,n)&&ct(t,n,e[n]);return t},S=(t,e)=>Jt(t,Kt(e));let Xt=0;const Zt=(t={})=>{const e={};return Object.keys(t).forEach(n=>{const r=t[n];if(r===null||typeof r=="function")e[n]=r;else{const{type:o}=r;o===null?e[n]={type:null,value:r.default}:Array.isArray(o)?e[n]={type:o[0],value:r.default,optionalTypes:o.slice(1)}:e[n]={type:o,value:r.default}}}),S(U({},e),{ref:{type:String,value:""}})},te=t=>{const{extendComponent:e,injectComponent:n}=m();return e&&e(t),t.lifetimes!=null||(t.lifetimes={}),t.lifetimes.created=f(t.lifetimes.created,function(){it(this),n&&n(t)}),t.lifetimes.attached=f(t.lifetimes.attached,function(){const r=Xt+=1;this.$id=r,Dt(r,this),this.$refID=this.data.ref,this.triggerEvent("ing",{id:this.$id,event:"$attached"})}),t.lifetimes.detached=f(t.lifetimes.detached,function(){var r;It(this.$id);const o=(r=this.$parent)==null?void 0:r.$refs,i=this.$refID;i&&o&&delete o[i],delete this.$parent}),t.methods=S(U({},t.methods),{$call(r,...o){g.debug(`Component ${this.$id} call ${r}:`,o),this.triggerEvent("ing",{id:this.$id,event:r,args:o})},$getRef:Y,$attached(r){this.$root=r.$root||r,this.$parent=r},$:at}),t.observers=S(U({},t.observers),{ref(r){var o;this.$refID&&this.$refID!==r&&((o=this.$parent)!=null&&o.$refs&&delete this.$parent.$refs[this.$refID],this.$refID=r,g.debug(`Component ${this.$id} ref: ${r}`))}}),t.properties=Zt(t.props),delete t.props,Component(t)};let lt=!0;const ee=(t,e)=>{const{getPath:n,extendPage:r,injectPage:o}=m(),i=n(t),c=(a,s)=>g.debug(`Page ${t}: ${a} has been invoked`,s||""),l=a=>g.debug(`Page ${t}: registered ${a}`);if(r&&r(t,e),e.$name=t,e.$state={firstOpen:!1},e.onAppLaunch){if($.launch){const{lOpt:a}=$;c("onAppLaunch"),e.onAppLaunch(a)}else h.on(z,a=>{c("onAppLaunch"),e.onAppLaunch(a)});l("onAppLaunch")}e.onNavigate&&(O.on(`${W}:${i}`,a=>(c("onNavigate",a),e.onNavigate(a))),l("onNavigate")),e.onPreload&&(O.on(`${J}:${i}`,a=>(c("onPreload",a),e.onPreload(a))),l("onPreload")),e.onLoad=f(e.onLoad,()=>{e.onAwake&&(h.on(R,a=>{c("onAwake"),e.onAwake(a)}),l("onAwake")),lt&&(lt=!1,e.$state.firstOpen=!0)}),e.onReady=f(e.onReady,()=>h.emit(K)),e.onUnload=f(e.onUnload,()=>h.emit(V)),it(e),o&&o(t,e),e.onRegister&&(c("onRegister"),e.onRegister()),Page(e),g.debug(`Registered: Page ${t}`)};export{Rt as $App,te as $Component,qt as $Config,ee as $Page,A as Emitter,$t as MpError,H as Queue,ft as decodeBase64,p as emitter,ut as encodeBase64,_ as env,Ct as funcQueue,I as go,Q as isFunction,xt as lock,g as logger,Et as once,M as query,C as reLaunch,N as redirect,T as switchTab,B as type,f as wrapFunction};
//# sourceMappingURL=index.mjs.map