Files
fesatcodeMiniApp/node_modules/spa-custom-hooks/lib/spa-custom-hooks.mjs
王鹏 0c2edb6036 init
2025-08-14 14:52:01 +08:00

2 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function t(t,e){var o=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),o.push.apply(o,n)}return o}function e(e){for(var o=1;o<arguments.length;o++){var n=null!=arguments[o]?arguments[o]:{};o%2?t(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):t(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,h(n.key),n)}}function r(t,e,o){return e&&i(t.prototype,e),o&&i(t,o),Object.defineProperty(t,"prototype",{writable:!1}),t}function a(t,e,o){return(e=h(e))in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}function c(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var n,i,r,a,c=[],u=!0,s=!1;try{if(r=(o=o.call(t)).next,0===e){if(Object(o)!==o)return;u=!1}else for(;!(u=(n=r.call(o)).done)&&(c.push(n.value),c.length!==e);u=!0);}catch(t){s=!0,i=t}finally{try{if(!u&&null!=o.return&&(a=o.return(),Object(a)!==a))return}finally{if(s)throw i}}return c}}(t,e)||u(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(t,e){if(t){if("string"==typeof t)return s(t,e);var o=Object.prototype.toString.call(t).slice(8,-1);return"Object"===o&&t.constructor&&(o=t.constructor.name),"Map"===o||"Set"===o?Array.from(t):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?s(t,e):void 0}}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,n=new Array(e);o<e;o++)n[o]=t[o];return n}function h(t){var e=function(t,e){if("object"!=typeof t||null===t)return t;var o=t[Symbol.toPrimitive];if(void 0!==o){var n=o.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:String(e)}var l,f,m=function(t,e,o){try{if(e){var n=e;"string"==typeof e&&(n=e.split("."));for(var i=o?n.length-1:n.length,r=0;r<i;r++)t=t[n[r]];return o?{key:n[i],obj:t}:t}return t}catch(t){return}},p=function(t){return l&&l.state?l:t&&t.$store||{}},d=function(){function t(e){var o=e.customhook,i=e.name,r=e.destroy,a=e.hit,c=void 0!==a&&a,u=e.watchKey,s=e.onUpdate,h=e.type,l=e.weightValue;n(this,t),this.name=i,this.destroy=r,this.type=h,this.hit=c,this.watchKey=u,this.weightValue=l,this.initFlag=!1,this.onUpdate=s,this.__customhook=o}return r(t,[{key:"init",value:function(){var t=this;this.initFlag||(this.watchKey&&(this.unwatchFn=this.watchAttr((function(e){t[e?"cycleStart":"cycleEnd"]()}))),this.initFlag=!0)}},{key:"cycleStart",value:function(){this.hit||(this.hit=!0,this.__customhook&&this.__customhook.triggerHook(this.name))}},{key:"cycleEnd",value:function(){this.hit&&(this.hit=!1,this.__customhook&&this.destroy&&this.__customhook.resetExecute(this.destroy))}},{key:"watchAttr",value:function(t){try{var e=this;return p(this.__customhook.instance).watch((function(t){return m(t,e.watchKey)}),(function(o,n){t(e.onUpdate?e.onUpdate(o,n):o)}),{watchKey:e.watchKey})}catch(t){}}}]),t}(),y={},k=["created","attached","ready","detached"],g=["show","hide","routeDone"],v=function(t){if(Array.isArray(t))return s(t)}(f=new Set([].concat(["beforeCreate","created","onPageShow","beforeMount","mounted","activated","onPageHide","deactivated","beforeDestroy","destroyed","onLaunch","onLoad","onShow","onReady","onHide","onUnload"],["onInit","created","attached","ready","didMount","detached","didUnmount"])))||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(f)||u(f)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(),w=function(t,o){return Object.keys(y).reduce((function(e,o){var n=y[o];return n.customhook=t,(e[o]=new d(n))&&e}),e({BeforeCreate:new d({customhook:t,name:"BeforeCreate",destroy:"destroyed",hit:!0,weightValue:2}),BeforeMount:new d({customhook:t,name:"beforeMount",destroy:"destroyed",weightValue:4}),PageShow:new d({customhook:t,name:"onPageShow",destroy:"onPageHide",weightValue:4.1}),Mounted:new d({customhook:t,name:"mounted",destroy:"destroyed",weightValue:5}),Activated:new d({customhook:t,name:"activated",destroy:"deactivated",weightValue:6}),PageHide:new d({customhook:t,name:"onPageHide",destroy:"onPageShow",weightValue:6.1}),Deactivated:new d({customhook:t,name:"deactivated",destroy:"activated",weightValue:7})},t.isComponent?{Init:new d({customhook:t,name:"onInit",destroy:"didUnmount",hit:"onInit"===o,weightValue:2}),Created:new d({customhook:t,name:"created",destroy:"detached",hit:"created"===o,weightValue:3}),Attached:new d({customhook:t,name:"attached",destroy:"detached",weightValue:4}),Show:new d({customhook:t,name:"show",destroy:"hide",weightValue:5}),DidMount:new d({customhook:t,name:"didMount",destroy:"didUnmount",hit:"didMount"===o,weightValue:6}),Ready:new d({customhook:t,name:"ready",destroy:"detached",weightValue:7}),RouteDone:new d({customhook:t,name:"routeDone",destroy:"detached",weightValue:8}),Hide:new d({customhook:t,name:"hide",destroy:"show",weightValue:9}),Detached:new d({customhook:t,name:"detached",destroy:"attached",weightValue:10})}:{Launch:new d({customhook:t,name:"onLaunch",destroy:"onUnload",hit:!0,weightValue:3}),Created:new d({customhook:t,name:"created",destroy:"destroyed",hit:"created"===o,weightValue:3}),Load:new d({customhook:t,name:"onLoad",destroy:"onUnload",hit:"onLoad"===o,weightValue:4}),Ready:new d({customhook:t,name:"onReady",destroy:"onUnload",weightValue:5}),Show:new d({customhook:t,name:"onShow",destroy:"onHide",weightValue:6}),Hide:new d({customhook:t,name:"onHide",destroy:"onShow",weightValue:7})}))},b=function(){function t(e){var o=e.instance,i=e.options,r=e.pageHooks,a=e.instanceType,c=e.initHookName;n(this,t),this.instance=o,this.instanceType=a,this.initHookName=c,this.isComponent="component"===a,this.customHooks={},this.customHookArr=[],this.hook={},this.options=i||{},this.pageHooks=r,this.diyHooks=Object.keys(y),this.init(),this.triggerHook()}return r(t,[{key:"init",value:function(){var t=this,e=w(this,this.initHookName);this.presetHooksName=Object.keys(e),this.hook=e;var o=this.pageHooks,n=o.hasOwnProperty("beforeCreate")||o.hasOwnProperty("onReady");o=n?o:o.__proto__,this.isComponent&&Object.assign(o,o.lifetimes,o.pageLifetimes);var i=this.filterHooks(o),r=i.customHookArr,a=i.hookInscape;r.forEach((function(e){t.customHooks[e]={callback:o[e].bind(t.instance),inscape:a[e],execute:!1,weightValue:a[e].reduce((function(e,o){var n;return e+((null===(n=t.hook[o])||void 0===n?void 0:n.weightValue)||0)}),0)}})),this.customHookArr=r.sort((function(e,o){return t.customHooks[e].weightValue-t.customHooks[o].weightValue})),Object.keys(this.hook).forEach((function(e){return t.hook[e].init()}))}},{key:"filterHooks",value:function(t){var e=this,o={},n={},i=Object.keys(t).filter((function(t){var i=e.getHookArr(t);return!!i.length&&(o[t]=i.filter((function(o){return e.hook[o]?(n[o]=e.hook[o],!0):(console.warn('[custom-hook 错误声明警告] "'.concat(o,'"钩子未注册,意味着"').concat(t,'"可能永远不会执行请先注册此钩子再使用文档https://github.com/1977474741/spa-custom-hooks#-diyhooks对象说明')),!1)})),t=="on"+i.join("")&&o[t].length==i.length)}));return this.hook=n,{customHookArr:i,hookInscape:o}}},{key:"triggerHook",value:function(t){var e=this;this.customHookArr.forEach((function(t){var o=e.customHooks[t];o.inscape.every((function(t){return e.hook[t]&&e.checkHookHit(e.hook[t])}))&&!o.execute&&(o.execute=!0,e.customHooks[t].callback(e.options))}))}},{key:"resetExecute",value:function(t){var e=this;this.customHookArr.forEach((function(o){var n=e.customHooks[o];n.inscape.find((function(o){var n;return t===(null===(n=e.hook[o])||void 0===n?void 0:n.destroy)}))&&(n.execute=!1)}))}},{key:"checkHookHit",value:function(t){if(t.watchKey){var e=m(p(t.__customhook.instance).state,t.watchKey);return t.onUpdate?t.onUpdate(e):e}return t.hit}},{key:"getHookArr",value:function(t){if(-1==t.indexOf("on"))return[];var e=this.splitHook(t),o=this.diyHooks;return e.length>1||-1!=o.indexOf(e[0])?e:[]}},{key:"splitHook",value:function(t){var e=this;t=t.replace("on",""),this.presetHooksName.sort((function(t,e){return e.length-t.length}));var o=new RegExp("("+this.presetHooksName.join("|")+")","g");return t.split(o).filter((function(t){return e.hook[t]}))}}]),t}(),H={"vue-h5":{hooksKey:"$options",initHook:"beforeCreate",supportComponent:!0,isPage:function(t){return t._compiled&&this.supportComponent}},"vue-miniprogram":{hooksKey:"$options",initHook:"beforeCreate",supportComponent:!0,isPage:function(){return this.supportComponent}},miniprogram:{name:"miniprogram",hooksKey:"",initHook:"onLoad",initHookApp:"onLaunch",initHookComponentAlipay:"didMount",initHookComponentAlipayInit:"onInit",initHookComponentWx:"created",initHookComponentLifetimes:"created",supportComponent:!0,isPage:function(){return this.supportComponent}}},j=H["vue-miniprogram"],A=function(t,n,i,r){function c(t,e,o){this.customHook?h.call(this,e,o):u.call(this,e,"component",t,o)}function u(t,e,o,n){if(!this.customHook){var a=m(this,j.hooksKey);if(j.isPage(a)){!i.state&&r&&(i.state=this[r]||r);var c="component"===e;Object.defineProperty(this,"customHook",{value:new b({instance:this,options:t,pageHooks:c?o:a,instanceType:e,initHookName:n}),configurable:!0,enumerable:!1})}}}function s(t){return t.reduce((function(t,e){return(t[e]=function(t){h.call(this,t,e)})&&t}),{})}function h(t,e){if(("object"==o(this.customHook)||null==o(this.customHook))&&this.customHook.customHookArr.length){t&&Object.keys(t).length>0&&(this.customHook.options=t);var n=this.customHook.hook,i=["beforeDestroy","destroyed","onUnload","didUnmount","detached"].includes(e);for(var r in n){var a,c=n[r];c.name==e?c.cycleStart():c.destroy==e&&c.cycleEnd(),i&&(null===(a=c.unwatchFn)||void 0===a||a.call(c))}i&&delete this.customHook}}t.mpvueVersion?j.initHook="onLoad":t.userAgentKey&&(j=H[t.userAgentKey]),l=i,y=n,t.mixin(e(e({},s(v)),{},a({},j.initHook,(function(t){u.call(this,t,"page",void 0,j.initHook)})),"miniprogram"===j.name?a(a(a(a(a(a({},j.initHookApp,(function(t){u.call(this,t,"app",void 0,j.initHookApp)})),j.initHookComponentWx,(function(t,e){u.call(this,e,"component",t,j.initHookComponentWx)})),j.initHookComponentAlipayInit,(function(t,e){u.call(this,e,"component",t,j.initHookComponentAlipayInit)})),j.initHookComponentAlipay,(function(t,e){c.call(this,t,e,j.initHookComponentAlipay)})),"lifetimes",e(e({},s(k)),{},a({},j.initHookComponentLifetimes,(function(t,e){c.call(this,t,e,j.initHookComponentLifetimes)})))),"pageLifetimes",e({},s(g))):{}))},O=Object.freeze({__proto__:null,get BASE(){return j},install:A}),S={mixin:function(t){var e=this,o=App,n=Page,i=Component;App=function(n){e.mergeHook(t,n),o(n)},Page=function(o){e.mergeHook(t,o),n(o)},Component=function(o){e.mergeComponentHook(t,o);var n=e.mixinLifetimes(t,o);i(n)}},mergeHook:function(t,e){for(var o=function(){var t=c(i[n],2),o=t[0],r=t[1],a=e[o];e[o]=function(){for(var t=arguments.length,e=new Array(t),o=0;o<t;o++)e[o]=arguments[o];a&&a.call.apply(a,[this].concat(e)),r.call.apply(r,[this].concat(e))}},n=0,i=Object.entries(t);n<i.length;n++)o()},mergeComponentHook:function(t,e){var o,n=["lifetimes","pageLifetimes"],i=function(t,e){var o="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!o){if(Array.isArray(t)||(o=u(t))||e&&t&&"number"==typeof t.length){o&&(t=o);var n=0,i=function(){};return{s:i,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,a=!0,c=!1;return{s:function(){o=o.call(t)},n:function(){var t=o.next();return a=t.done,t},e:function(t){c=!0,r=t},f:function(){try{a||null==o.return||o.return()}finally{if(c)throw r}}}}(Object.entries(t).filter((function(t){return!n.includes(t[0])})));try{var r=function(){var t=c(o.value,2),n=t[0],i=t[1],r=e[n];e[n]=function(){for(var t=arguments.length,o=new Array(t),n=0;n<t;n++)o[n]=arguments[n];r&&r.call.apply(r,[this].concat(o)),i.call.apply(i,[this,e].concat(o))}};for(i.s();!(o=i.n()).done;)r()}catch(t){i.e(t)}finally{i.f()}},mixinLifetimes:function(t,e){if("function"==typeof Behavior){var o=e.behaviors||[],n=t.lifetimes.created;return t.lifetimes.created=function(){for(var t=arguments.length,o=new Array(t),i=0;i<t;i++)o[i]=arguments[i];n&&n.call.apply(n,[this,e].concat(o))},o.push(Behavior({lifetimes:t.lifetimes,pageLifetimes:t.pageLifetimes})),e.behaviors=o,e}var i=e.mixins||[];if(e.options&&e.options.lifetimes){var r=t.lifetimes.created;t.lifetimes.created=function(){for(var t=arguments.length,o=new Array(t),n=0;n<t;n++)o[n]=arguments[n];r&&r.call.apply(r,[this,e].concat(o))}}return i.push(Mixin({lifetimes:t.lifetimes,pageLifetimes:t.pageLifetimes,options:{lifetimes:!0}})),e.mixins=i,e},userAgentKey:"miniprogram"},C={},P={watch:function(t,e,n){var i=p().state,r=n.watchKey,a=m(i,r,!0),u={callback:e,unwatch:!1};C[r]?C[r].push(u):C[r]=[u];var s=[];return function t(n,i){var u=n[i];Object.defineProperty(n,i,{configurable:!0,enumerable:!0,set:function(t){u=t,C[r].map((function(t){return t.callback(a.obj[a.key])}))},get:function(){return u}}),Array.isArray(n[i])&&s.push((h=n[i],l=function(){e(a.obj[a.key])},f=Object.create(h),m=["push","pop","shift","unshift","splice","sort","reverse"].map((function(t){var e=f[t];return V(h,t,(function(){return e.apply(this,arguments),l.apply(this,arguments)})),function(){V(h,t,e)}})),function(){m.map((function(t){return t()}))}));var h,l,f,m;if("object"===o(n[i])&&null!=n[i])for(var p=0,d=Object.entries(n[i]);p<d.length;p++){var y=c(d[p],2),k=y[0];y[1],t(n[i],k)}}(a.obj,a.key),function(){u.unwatch=!0;var t=C[r].findIndex((function(t){return t.unwatch}));C[r].splice(t,1),C[r].length||delete C[r],s.map((function(t){return t()}))}}};function V(t,e,o,n){Object.defineProperty(t,e,{value:o,enumerable:!!n,writable:!0,configurable:!0})}var x={install:function(){arguments.length<3?A(S,arguments[0],P,arguments[1]||"globalData"):A.apply(O,arguments)},setHit:function(t,e){w()[t][e?"cycleStart":"cycleEnd"]()}};export{x as default};