UNPKG

@zeppos/zml

Version:

A Mini Library of ZeppOS MiniApp

2 lines (1 loc) 2.46 kB
const t=Object.prototype.hasOwnProperty;function e(e,n){return Object.getOwnPropertyNames(n).forEach((function(s){if(!t.call(e,s)){var i=Object.getOwnPropertyDescriptor(n,s);Object.defineProperty(e,s,i)}})),e}const n={init(){this.plugins=[],this.settings={},this.mixins=[]},set(t,e){if(1===arguments.length)return this.settings[t];this.settings[t]=e},use(t,...e){return"function"==typeof t?this.plugins.push({handler:t,args:e}):"object"==typeof t&&this.mixins.push({handler:t,args:[]}),this},handle(t){this.plugins.forEach((e=>{if(e&&"function"==typeof e.handler)try{const n=e.handler.call(this,t,...e.args);"object"==typeof n&&this.mixins.push({handler:n,args:[]})}catch(t){}})),this.mixins.forEach((({handler:{onInit:e,onPause:n,build:s,onResume:i,onDestroy:o,onCreate:r,...g},args:h})=>{Object.assign(t,g)}))}},s={onChange(t){return t?(settings.settingsStorage.addListener("change",t),this):this},offChange(){return settings.settingsStorage.removeListener("change"),this},getItem:t=>settings.settingsStorage.getItem(t),setItem:(t,e)=>settings.settingsStorage.setItem(t,e),clear:()=>settings.settingsStorage.clear(),removeItem(t){settings.settingsStorage.removeItem(t)},getAll:()=>settings.settingsStorage.toObject()};function i({state:t={},onInit:e,onRun:n,onDestroy:s,...o}={}){const r={state:t,...o,onInit(t){for(let e=0;e<=i.mixins.length-1;e++){const n=i.mixins[e];n&&n.handler.onInit?.apply(this,t)}e?.apply(this,t)},onRun(t){for(let e=0;e<=i.mixins.length-1;e++){const n=i.mixins[e];n&&n.handler.onRun?.apply(this,t)}n?.apply(this,t)},onDestroy(t){s?.apply(this,t);for(let e=i.mixins.length-1;e>=0;e--){const n=i.mixins[e];n&&n.handler.onDestroy?.apply(this,t)}}};return i.handle(r),r}e(i,n),i.init(),i.use((function(){return{onInit(){this.logger=Logger.getLogger(sideService.appInfo.app.appName),this.logger.scope=sideService.appInfo.app.appName,this.logger.name="side-service",this.log=(...t)=>{this.logger.log(...t)},this.error=(...t)=>{t[0]instanceof Error?this.logger.error(...t):this.logger.error({},...t)},this.debug=(...t)=>{this.logger.debug(...t)}}}})),i.use((function(){return{onInit(){this.settings=s,this._onSettingsChange=this.onSettingsChange?.bind(this),s.onChange(this._onSettingsChange),"undefined"!=typeof sideService&&sideService.launchReasons.settingsChanged&&this._onSettingsChange(sideService.launchArgs)},onDestroy(){this._onSettingsChange&&s.offChange(this._onSettingsChange)}}}));export{i as BaseSideService,e as merge};