UNPKG

vuex-stateshot

Version:

A state snapshot plugin on actions/mutations for Vuex3.1+.

1 lines 9.91 kB
(function(t,e){"object"===typeof exports&&"object"===typeof module?module.exports=e(require("vue")):"function"===typeof define&&define.amd?define([],e):"object"===typeof exports?exports["vuex-stateshot"]=e(require("vue")):t["vuex-stateshot"]=e(t["Vue"])})("undefined"!==typeof self?self:this,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"===typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t["default"]}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s="fae3")}({"61e5":function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=t.length,r=e^n,i=0,s=void 0;while(n>=4)s=255&t.charCodeAt(i)|(255&t.charCodeAt(++i))<<8|(255&t.charCodeAt(++i))<<16|(255&t.charCodeAt(++i))<<24,s=1540483477*(65535&s)+((1540483477*(s>>>16)&65535)<<16),s^=s>>>24,s=1540483477*(65535&s)+((1540483477*(s>>>16)&65535)<<16),r=1540483477*(65535&r)+((1540483477*(r>>>16)&65535)<<16)^s,n-=4,++i;switch(n){case 3:r^=(255&t.charCodeAt(i+2))<<16;case 2:r^=(255&t.charCodeAt(i+1))<<8;case 1:r^=255&t.charCodeAt(i),r=1540483477*(65535&r)+((1540483477*(r>>>16)&65535)<<16)}return r^=r>>>13,r=1540483477*(65535&r)+((1540483477*(r>>>16)&65535)<<16),r^=r>>>15,r>>>0},i=r,s=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t};function o(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}var u={toRecord:function(t){return{chunks:[s({},t,{children:void 0})],children:t.children}},fromRecord:function(t){var e=t.chunks,n=t.children;return s({},e[0],{children:n})}},h=function t(e,n){for(var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,h=arguments.length>4&&void 0!==arguments[4]?arguments[4]:-1,c=r.findIndex((function(t){var n=t.match;return n(e)})),a=r[c]||u,d=a.toRecord(e),l=d.chunks,f=d.children,p=f,g=[],m=0;m<l.length;m++){var y=JSON.stringify(l[m]),v=i(y);g.push(v),n[v]=y}if(-1!==h&&Array.isArray(s&&s.children)){var $=[].concat(o(s.children));return $[h]=t(p[h],n,r),{hashes:g,ruleIndex:c,children:$}}return{hashes:g,ruleIndex:c,children:f&&f.map((function(e){return t(e,n,r)}))}},c=function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],i=e.hashes,s=e.ruleIndex,o=e.children,h=i.map((function(t){return JSON.parse(n[t])})),c=r[s]||u;return c.fromRecord({chunks:h,children:o&&o.map((function(e){return t(e,n,r)}))})},a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}();function d(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var l=function(){},f=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{rules:[],delay:50,maxLength:100,onChange:l,useChunks:!0};d(this,t),this.rules=e.rules||[],this.delay=e.delay||50,this.maxLength=e.maxLength||100,this.useChunks=void 0===e.useChunks||e.useChunks,this.onChange=e.onChange||l,this.$index=-1,this.$records=[],this.$chunks={},this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null}return a(t,[{key:"get",value:function(){var t=this.$records[this.$index],e=void 0;return e=t?this.useChunks?c(t,this.$chunks,this.rules):t:null,this.onChange(e),e}},{key:"pushSync",value:function(t){var e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,r=this.$records[this.$index]||null,i=this.useChunks?h(t,this.$chunks,this.rules,r,n):t;this.$index++,this.$records[this.$index]=i;for(var s=this.$index+1;s<this.$records.length;s++)this.$records[s]=null;return this.$index>=this.maxLength&&(this.$records[this.$index-this.maxLength]=null),this.$pending.timer&&(clearTimeout(this.$pending.timer),this.$pending.state=null,this.$pending.pickIndex=null,this.$pending.timer=null,this.$debounceTime=null,this.$pending.onResolves.forEach((function(t){return t(e)})),this.$pending.onResolves=[]),this.onChange(t),this}},{key:"push",value:function(t){var e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,r=+new Date,i=function(){e.$pending.state=t,e.$pending.pickIndex=n,e.$debounceTime=r;var i=new Promise((function(t,n){e.$pending.onResolves.push(t),e.$pending.timer=setTimeout((function(){e.pushSync(e.$pending.state,e.$pending.pickIndex)}),e.delay)}));return i};return null===this.$pending.timer?i():r-this.$debounceTime<this.delay?(clearTimeout(this.$pending.timer),this.$pending.timer=null,i()):Promise.reject(new Error("Invalid push ops"))}},{key:"undo",value:function(){return this.hasUndo&&this.$index--,this}},{key:"redo",value:function(){return this.hasRedo&&this.$index++,this}},{key:"reset",value:function(){var t=this;return this.$index=-1,this.$records.forEach((function(t){null})),Object.keys(this.$chunks).forEach((function(e){t.$chunks[e]=null})),this.$records=[],this.$chunks={},clearTimeout(this.$pending.timer),this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null,this}},{key:"hasRedo",get:function(){if(this.$index===this.$records.length-1)return!1;for(var t=!1,e=this.$index+1;e<this.$records.length;e++)null!==this.$records[e]&&(t=!0);return t}},{key:"hasUndo",get:function(){var t=Math.max(this.$records.length-this.maxLength,0);return this.$index>t}},{key:"length",get:function(){return Math.min(this.$records.length,this.maxLength)}}]),t}();e.History=f},"8bbf":function(e,n){e.exports=t},f6fd:function(t,e){(function(t){var e="currentScript",n=t.getElementsByTagName("script");e in t||Object.defineProperty(t,e,{get:function(){try{throw new Error}catch(r){var t,e=(/.*at [^\(]*\((.*):.+:.+\)$/gi.exec(r.stack)||[!1])[1];for(t in n)if(n[t].src==e||"interactive"==n[t].readyState)return n[t];return null}}})})(document)},fae3:function(t,e,n){"use strict";var r;(n.r(e),"undefined"!==typeof window)&&(n("f6fd"),(r=window.document.currentScript)&&(r=r.src.match(/(.+\/)[^/]+\.js(\?.*)?$/))&&(n.p=r[1]));var i=n("8bbf"),s=n.n(i),o=n("61e5");const u="vuexstateshot",h="__SYNC_STATE__",c="STATESHOT_UNDO",a="STATESHOT_REDO";class d{constructor(t,e,n){const{maxLength:r=100,delay:i=50,...s}=n,u=new o["History"]({maxLength:r+1,delay:i,...s});this.store=t,this.modules=e,this.moduleNames=Object.keys(e),this.rootModule=t._modules.root,this.actions=this.getSubscribeTypes("actions"),this.mutations=this.getSubscribeTypes("mutations"),this.unsubscribeAction=null,this.unsubscribe=null,this.history=u}getHistoryLength(){return this.history.$records.filter(t=>t).length}getSubscribeTypes(t){let e=[];for(const r of this.moduleNames){const n=this.modules[r][t],i=n&&n.map(t=>{return"rootModule"===r?t:`${r}/${t}`});e=[...e,i]}const n=t=>[].concat(...t.map(t=>Array.isArray(t)?n(t):t));return n(e.filter(t=>t))}findNamespacedModule(t,e=this.rootModule){if("root"===t)return this.rootModule;const n=t.split("/");if(!n.length)return!1;const r=e._children[n[0]],i=n.slice(1).join("/");if(r&&r.namespaced)return 1===n.length?r:this.findNamespacedModule(i,r);for(const s in e._children){const n=this.findNamespacedModule(t,e._children[s]);if(n)return n}return!1}registerPluginMoudle(){this.store.registerModule(u,{namespaced:!0,state:{prevState:{},nextState:{},hasUndo:!1,hasRedo:!1,historyLength:0,undoCount:0,redoCount:0},actions:{snapshot:({commit:t,dispatch:e,state:n,rootState:r},i)=>{this.history.pushSync(r);const s=this.getHistoryLength();t(h,{hasUndo:s-1>0,hasRedo:!1,historyLength:s,undoCount:s-1,redoCount:0})},undo:({commit:t,state:e},n)=>{const{hasUndo:r}=e,i=this.history.undo().get();return i&&r&&t("STATESHOT_UNDO",i),i},redo:({commit:t,state:e},n)=>{const{hasRedo:r}=e,i=this.history.redo().get();return i&&r&&t("STATESHOT_REDO",i),i},reset:({commit:t},e)=>{this.history.reset(),t(h,{nextState:{},prevState:{},hasUndo:!1,hasRedo:!1,historyLength:0,undoCount:0,redoCount:0})}},mutations:{[h](t,e){Object.assign(t,e)},[c](t,e){t.prevState=e,t.undoCount-=1,0===t.undoCount&&(t.hasUndo=!1),t.redoCount+=1,t.hasRedo=t.redoCount>0},[a](t,e){t.nextState=e,t.redoCount-=1,0===t.redoCount&&(t.hasRedo=!1),t.undoCount+=1,t.hasUndo=t.undoCount>0}},getters:{hasUndo:t=>t.hasUndo,hasRedo:t=>t.hasRedo,undoCount:t=>t.undoCount,redoCount:t=>t.redoCount,historyLength:t=>t.historyLength}})}syncState(t="root"){const{state:e}=this.findNamespacedModule(t,this.rootModule);this.store.dispatch(`${u}/snapshot`,e)}stateshotFromAction(t){return{before:(t,e)=>{},after:(t,e)=>{this.actions.includes(t.type)&&this.syncState()}}}stateshotFromMutation(t,e){this.mutations.includes(t.type)&&this.syncState()}subscribeAction(){this.unsubscribeAction=this.store.subscribeAction(this.stateshotFromAction(this.store))}subscribe(){this.unsubscribe=this.store.subscribe((t,e)=>this.stateshotFromMutation(t,e))}}function l(t,e){return n=>{const r=new d(n,t,e);r.registerPluginMoudle(),r.syncState(),r.subscribeAction(),r.subscribe(),s.a.prototype.$stateshot=r}}n.d(e,"createPlugin",(function(){return l}))}})}));