-
Notifications
You must be signed in to change notification settings - Fork 1
/
riot-stx.min.js
1 lines (1 loc) · 1.31 KB
/
riot-stx.min.js
1
riotStx={useStxLocal:!1,optionsState(t){this.useStxLocal=t},initState(...t){let e=this;state=new Proxy({},{set:function(t,e,n){return"_"!=e[0]&&JSON.stringify(t[e]||null)!==JSON.stringify(n)&&(t[e]=n,window.dispatchEvent(new CustomEvent("state_"+e,{detail:{key:e,value:n}}))),!0}}),t.forEach(t=>{e.deepExtendState(t)}),riot.install(function(t){e.riotPluginState(t)})},riotPluginState(t){let e=this;const{onBeforeMount:n,onMounted:s,onBeforeUpdate:a,onUpdated:o,onBeforeUnmount:i,onUnmounted:d}=t;t.updateState=function(e){t.stx[e.detail.key]=e.detail.value,t.update()},t.stx=new Proxy(t.stx||{},{set:function(t,e,n){return"_"!=e[0]&&JSON.stringify(t[e])!==JSON.stringify(n)?(t[e]=n,state[e]=n):t[e]=n,!0}}),t.onBeforeMount=function(...s){for(let[n,s]of Object.entries(t.stx))"_"!=n[0]&&(void 0!==state[n]?t.stx[n]=state[n]:e.useStxLocal&&(state[n]=t.stx[n]),window.addEventListener("state_"+n,t.updateState));n&&n.apply(this,s)},t.onUnmounted=function(...e){for(let[e,n]of Object.entries(t.stx))"_"!=e[0]&&window.removeEventListener("state_"+e,t.updateState);d&&d.apply(this,e)},t.setState=function(t){e.setState(t)},t.setOneState=function(t){e.setState(t)}},setOneState(t,e){state[t]=e},setState(t){this.deepExtendState(t)},subscribeState(t,e){window.addEventListener("state_"+t,t=>{e(t.detail)})},deepExtendState(t){Object.assign(state,t)}};