@mptool/enhance
Version:
Lifetime enhance for QQ/Wechat miniapp
2 lines • 11.4 kB
JavaScript
for(var e=Object.defineProperty,t=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n||e(r,Symbol.toStringTag,{value:`Module`}),r},n=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`,r=typeof Uint8Array>`u`?[]:new Uint8Array(256),i=0;i<n.length;i++)r[n.charCodeAt(i)]=i;var a=function(e){var t=new Uint8Array(e),r,i=t.length,a=``;for(r=0;r<i;r+=3)a+=n[t[r]>>2],a+=n[(t[r]&3)<<4|t[r+1]>>4],a+=n[(t[r+1]&15)<<2|t[r+2]>>6],a+=n[t[r+2]&63];return i%3==2?a=a.substring(0,a.length-1)+`=`:i%3==1&&(a=a.substring(0,a.length-2)+`==`),a},o=function(e){var t=e.length*.75,n=e.length,i,a=0,o,s,c,l;e[e.length-1]===`=`&&(t--,e[e.length-2]===`=`&&t--);var u=new ArrayBuffer(t),d=new Uint8Array(u);for(i=0;i<n;i+=4)o=r[e.charCodeAt(i)],s=r[e.charCodeAt(i+1)],c=r[e.charCodeAt(i+2)],l=r[e.charCodeAt(i+3)],d[a++]=o<<2|s>>4,d[a++]=(s&15)<<4|c>>2,d[a++]=(c&3)<<6|l&63;return u};const s=typeof qq==`object`?`qq`:typeof wx==`object`?`miniapp`in wx?`donut`:`wx`:`js`,c=(e=new Map)=>({all:e,on:(t,n)=>{let r=e.get(t);r?r.push(n):e.set(t,[n])},off:(t,n)=>{let r=e.get(t);r&&(n?r.splice(r.indexOf(n)>>>0,1):e.set(t,[]))},emit:(t,n)=>{let r=e.get(t);r&&[...r].map(e=>e(n)),r=e.get(`*`),r&&[...r].map(e=>e(t,n))},emitAsync:async(t,n)=>{var r,i;await Promise.all([...(r=e.get(t))==null?[]:r].map(async e=>{await e(n)})),await Promise.all([...(i=e.get(`*`))==null?[]:i].map(async e=>{await e(t,n)}))}});function l(e){"@babel/helpers - typeof";return l=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},l(e)}function u(e,t){if(l(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(l(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function d(e){var t=u(e,`string`);return l(t)==`symbol`?t:t+``}function f(e,t,n){return(t=d(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var ee=class extends Error{constructor({code:e,message:t}){super(t),f(this,`code`,void 0),this.code=e==null?null:e,this.name=`MpError`}toString(){return`code: ${this.code}, message: ${this.message}`}},p=t({debug:()=>te,error:()=>v,filter:()=>ie,info:()=>ne,warn:()=>re}),m,h;const g=s===`js`?console:((m=(h=wx).getRealtimeLogManager)==null?void 0:m.call(h))||wx.getLogManager({level:1}),_=s!==`js`&&`getRealtimeLogManager`in wx,te=(...e)=>{wx.env.DEBUG&&(_?g.info(`debug`,...e):g.debug(...e)),g!==console&&console.debug(...e)},ne=(...e)=>{g.info(...e),g!==console&&console.info(...e)},re=(...e)=>{g.warn(...e),g!==console&&console.warn(...e)},v=(...e)=>{_?g.error(...e):g.warn(`error`,...e),g!==console&&console.error(...e)},ie=e=>{_&&g.setFilterMsg(e)};var y=t({join:()=>oe,parse:()=>ae,stringify:()=>b});const ae=(e=``,t=`&`)=>{let n={},r=e?e.split(t):null;return r&&r.length>0&&r.forEach(e=>{let[t,r]=e.split(`=`);n[t]=r}),n},b=(e={},t=`&`,n=!1)=>Object.keys(e).map(t=>{let r=e[t];return`${t}=${n?r:encodeURIComponent(r)}`}).join(t),oe=(e,t,n=!1)=>{let r=b(t,`&`,n);return r?`${e}${/[?&]$/u.test(e)?``:e.includes(`?`)?`&`:`?`}${r}`:e},x=e=>{if(e===void 0)return`undefined`;let t=typeof e;if(t===`object`){if(e===null)return`null`;let t=/\[object (\w+)\]/u.exec(Object.prototype.toString.call(e));return t?t[1].toLowerCase():``}return t},S=e=>x(e)===`function`;function C(e,t){return function(...n){if(t.apply(this,n),e)return e.apply(this,n)}}const w=(e,t)=>{let n;return function(...r){if(!n)return n=!0,e.apply(t==null?this:t,[()=>{n=!1},...r])}},se=(e,t)=>{let n;return function(...r){if(!n)return n=!0,e.apply(t==null?null:t,r)}};var T=class{constructor(e=1){this.capacity=e,f(this,`funcQueue`,[]),f(this,`running`,0)}next(){let e=this.funcQueue.shift();if(e){let{func:t,ctx:n,args:r}=e,i=()=>{t.apply(n,[()=>{--this.running,this.next()},...Array.prototype.slice.call(r,0)])};this.running+=1,i()}}add(e,t,...n){this.funcQueue.push({func:e,ctx:t,args:Array.prototype.slice.call(n,0)}),this.running<this.capacity&&this.next()}clear(){this.funcQueue=[]}};const ce=(e,t=1)=>{let n=new T(t);return function(...t){n.add(e,this,...t)}},le=(e,t=1)=>{let n=!1,r;return{run:()=>new Promise(i=>{let a=e,o=0,s=()=>{if(n){i({interrupted:!0,msg:r});return}let e=a.shift();e?(o+=1,e().then(()=>{--o,s()})):o===0&&i({interrupted:!1})},c=0;for(;c<t;)c+=1,s()}),stop:e=>{n=!0,r=e}}},E=c(),D=c(),O=c(),k={launch:!1,lOpt:{},hide:0},ue=e=>{k.launch=!0,k.lOpt=e,E.emit(`al`,e)},de=()=>{k.hide&&(E.emit(`a`,Date.now()-k.hide),k.hide=0)},fe=()=>{k.hide=Date.now()},pe=e=>{e.onLaunch=C(e.onLaunch,ue),e.onShow=C(e.onShow,de),e.onHide=C(e.onHide,fe),e.onAwake&&(E.on(`a`,t=>{p.debug(`App: awake after ${t}ms`),e.onAwake(t)}),p.debug(`App: registered onAwake`)),e.$on=O.on,e.$off=O.off,e.$emit=O.emit,e.$emitAsync=O.emitAsync,App(e)},A=new Map,j=e=>A.get(e),me=(e,t)=>{A.set(e,t)},he=e=>{A.delete(e)};function ge(e,t){if(e==null)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}function M(e,t){if(e==null)return{};var n,r,i=ge(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}function N(e){"@babel/helpers - typeof";return N=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function R(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]==null?{}:arguments[t];t%2?L(Object(n),!0).forEach(function(t){I(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):L(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}const _e=[`defaultPage`,`pages`,`getPath`];let z;const ve=e=>{let t=e,{defaultPage:n,pages:r,getPath:i}=t,a=M(t,_e);if(S(i)){z=R(R({},a),{},{getPath:i});return}let o={},s={},c=(e,t)=>{let n=t.replace(/\$name/g,e);o[e]=n,s[n]=e};Array.isArray(r)?r.forEach(([e,t])=>{typeof e==`string`?c(e,t):e.forEach(e=>{c(e,t)})}):typeof r==`object`&&(o=r,s=Object.keys(r).reduce((e,t)=>(e[r[t]]=t,e),{})),z=R(R({},a),{},{getPath:e=>o[e]||n.replace(/\$name/g,e)})},B=()=>{if(!z)throw Error(`$Config is not called`);return z};let V=!0;const H=e=>{let t=B(),[n,r]=e.split(`?`),i=n.startsWith(`/`)?n:t.getPath(n);return{path:i,query:y.parse(r),url:`${i}${r?`?${r}`:``}`}};function U(e){return t=>{if(V){V=!1;let{path:n,url:r,query:i}=H(t);return Promise.race([D.emitAsync(`n:${n}`,i),new Promise(e=>{var t;setTimeout(()=>{e()},(t=B().maxDelay)==null?200:t)})]).then(()=>(V=!0,wx[e]({url:r})))}}}E.on(`r`,()=>{var e;setTimeout(()=>{V=!0},(e=B().minInterval)==null?100:e)}),E.on(`u`,()=>{V=!0});const W=U(`navigateTo`),G=U(`redirectTo`),K=U(`switchTab`),q=U(`reLaunch`),J=function(e){return async function(t){if(t){let{before:n,after:r,url:i}=t.currentTarget.dataset;this&&n&&typeof this[n]==`function`&&this[n](t),i&&(await e(i),this&&r&&typeof this[r]==`function`&&this[r](t))}}},ye=J(W),be=J(G),xe=J(K),Se=J(q),Y=(e=1)=>{let{home:t}=B();return getCurrentPages().length<=e&&t?q(t):wx.navigateBack({delta:e})},Ce=async function(e){if(e){let{before:t,after:n,delta:r=1}=e.currentTarget.dataset;this&&t&&typeof this[t]==`function`&&this[t](e),await Promise.resolve(Y(Number(r))),this&&n&&typeof this[n]==`function`&&this[n](e)}},X=()=>[...getCurrentPages()].pop(),we=e=>{let{path:t,query:n}=H(e);D.emit(`p:${t}`,n)},Z=function(e){let{args:t,event:n,id:r}=e.detail;switch(n){case`$attached`:{let e=j(r);if(!e)break;let t=e.$refID;t&&this.$refs.set(t,e),e.$attached(this);break}default:{let e=this[n];e&&e.apply(this,t)}}};function Q(e){let t=B();e.$=Z,e.$refs=new Map,e.$on=O.on,e.$off=O.off,e.$emit=O.emit,e.$emitAsync=O.emitAsync,e.$go=W,e.$redirect=G,e.$switch=K,e.$reLaunch=q,e.$back=Y,e.$preload=we,e.$currentPage=X,e.$getPath=t.getPath,e.$bindGo=ye,e.$bindRedirect=be,e.$bindSwitch=xe,e.$bindRelaunch=Se,e.$bindBack=Ce}let Te=0;const Ee=(e={})=>{let t={};return Object.keys(e).forEach(n=>{let r=e[n];if(r===null||typeof r==`function`)t[n]=r;else{let{type:e}=r;e===null?t[n]={type:null,value:r.default}:Array.isArray(e)?t[n]={type:e[0],value:r.default,optionalTypes:e.slice(1)}:t[n]={type:e,value:r.default}}}),R(R({},t),{},{ref:{type:String,value:``}})},De=e=>{let{extendComponent:t,injectComponent:n}=B();return t&&t(e),e.lifetimes!=null||(e.lifetimes={}),e.lifetimes.created=C(e.lifetimes.created,function(){Q(this),n&&n(e)}),e.lifetimes.attached=C(e.lifetimes.attached,function(){let e=Te+=1;this.$id=e,me(e,this),this.$refID=this.data.ref,this.triggerEvent(`ing`,{id:this.$id,event:`$attached`})}),e.lifetimes.detached=C(e.lifetimes.detached,function(){var e;he(this.$id);let t=(e=this.$parent)==null?void 0:e.$refs,n=this.$refID;n&&t&&t.delete(n),delete this.$parent}),e.methods=R(R({},e.methods),{},{$call(e,...t){p.debug(`Component ${this.$id} call ${e}:`,t),this.triggerEvent(`ing`,{id:this.$id,event:e,args:t})},$getRef:j,$attached(e){this.$root=e.$root||e,this.$parent=e},$:Z}),e.observers=R(R({},e.observers),{},{ref(e){if(this.$refID&&this.$refID!==e){var t;(t=this.$parent)==null||t.$refs.delete(this.$refID),this.$refID=e,p.debug(`Component ${this.$id} ref: ${e}`)}}}),e.properties=Ee(e.props),delete e.props,Component(e)};let $=!0;const Oe=(e,t)=>{let{getPath:n,extendPage:r,injectPage:i}=B(),a=n(e),o=(t,n)=>{p.debug(`Page ${e}: ${t} has been invoked`,n)},s=t=>{p.debug(`Page ${e}: registered ${t}`)};if(r&&r(e,t),t.$name=e,t.$state={firstOpen:!1},t.onAppLaunch){if(k.launch){let{lOpt:e}=k;o(`onAppLaunch`),t.onAppLaunch(e)}else E.on(`al`,e=>{o(`onAppLaunch`),t.onAppLaunch(e)});s(`onAppLaunch`)}t.onNavigate&&(D.on(`n:${a}`,e=>(o(`onNavigate`,e),t.onNavigate(e))),s(`onNavigate`)),t.onPreload&&(D.on(`p:${a}`,e=>(o(`onPreload`,e),t.onPreload(e))),s(`onPreload`)),t.onLoad=C(t.onLoad,()=>{t.onAwake&&(E.on(`a`,e=>{o(`onAwake`),t.onAwake(e)}),s(`onAwake`)),$&&($=!1,t.$state.firstOpen=!0)}),t.onReady=C(t.onReady,()=>{E.emit(`r`)}),t.onUnload=C(t.onUnload,()=>{E.emit(`u`)}),Q(t),i&&i(e,t),t.onRegister&&(o(`onRegister`),t.onRegister()),Page(t),p.debug(`Registered: Page ${e}`)};export{pe as $App,De as $Component,ve as $Config,Oe as $Page,c as Emitter,ee as MpError,T as Queue,le as createQueue,o as decodeBase64,O as emitter,a as encodeBase64,s as env,ce as funcQueue,W as go,S as isFunction,w as lock,p as logger,se as once,y as query,q as reLaunch,G as redirect,K as switchTab,x as type,C as wrapFunction};
//# sourceMappingURL=index.js.map