vuex-shared-mutations
Version:
Share vuex mutations across tabs via localStorage
2 lines • 6.38 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.vuexSharedMutations=t():e.vuexSharedMutations=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=3)}([function(e,t,n){"use strict";(function(e){function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}n.d(t,"a",function(){return u});var a="vuex-shared-mutations",i="undefined"!=typeof window?window:e,u=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e);var n=t.BroadcastChannel||i.BroadcastChannel,r=t.key||a;if(!this.constructor.available(n))throw new Error("Broadcast strategy not available");this.channel=new n(r)}return o(e,null,[{key:"available",value:function(){return!("function"!=typeof(arguments.length>0&&void 0!==arguments[0]?arguments[0]:i.BroadcastChannel))}}]),o(e,[{key:"addEventListener",value:function(e){this.channel.addEventListener("message",function(t){e(t.data)})}},{key:"share",value:function(e){return this.channel.postMessage(e)}}]),e}()}).call(this,n(2))},function(e,t,n){"use strict";(function(e){function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),e}n.d(t,"a",function(){return s});var i="vuex-shared-mutations",u="undefined"!=typeof window?window:e,c=4096,f=1;var s=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e);var n=t.window||u.window,o=t.localStorage||u.localStorage;if(!this.constructor.available({window:n,localStorage:o}))throw new Error("Strategy unavailable");this.uniqueId="".concat(Date.now(),"-").concat(Math.random()),this.messageBuffer=[],this.window=n,this.storage=o,this.options=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},o=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),o.forEach(function(t){r(e,t,n[t])})}return e}({key:i},t)}return a(e,null,[{key:"available",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{window:u.window,localStorage:u.localStorage},t=e.window,n=e.localStorage;if(!t||!n)return!1;try{return n.setItem("vuex-shared-mutations-test-key",Date.now()),n.removeItem("vuex-shared-mutations-test-key"),!0}catch(e){return!1}}}]),a(e,[{key:"addEventListener",value:function(e){var t=this;return this.window.addEventListener("storage",function(n){if(!n.newValue)return!1;if(-1===n.key.indexOf("##")||n.key.split("##")[0]!==t.options.key)return!1;var r=t.window.JSON.parse(n.newValue);if(r.author===t.uniqueId)return!1;if(t.messageBuffer.push(r.messagePart),t.messageBuffer.length===r.total){var o=t.window.JSON.parse(t.messageBuffer.join(""));t.messageBuffer=[],e(o)}return!0})}},{key:"share",value:function(e){var t=this,n=function(e){var t=Math.ceil(e.length/c);return Array.from({length:t}).map(function(t,n){return e.substr(n*c,c)})}(this.window.JSON.stringify(e));n.forEach(function(e,r){f+=1;var o="".concat(t.options.key,"##").concat(r);t.storage.setItem(o,JSON.stringify({author:t.uniqueId,part:r,total:n.length,messagePart:e,messageCounter:f})),t.storage.removeItem(o)})}}]),e}()}).call(this,n(2))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){"use strict";n.r(t);var r=n(0),o=n(1);function a(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n,r,o={},a=Object.keys(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}n.d(t,"BroadcastChannelStrategy",function(){return r.a}),n.d(t,"LocalStorageStratery",function(){return o.a});t.default=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.predicate,n=e.strategy,i=a(e,["predicate","strategy"]);if(("storageKey"in i||"sharingKey"in i)&&window.console.warn("Configuration directly on plugin was removed, configure specific strategies if needed"),!Array.isArray(t)&&"function"!=typeof t)throw new Error("Either array of accepted mutations or predicate function must be supplied");var u="function"==typeof t?t:function(e){var n=e.type;return-1!==t.indexOf(n)},c=!1,f=n||function(){if(o.a.available())return new o.a;if(r.a.available())return new r.a;throw new Error("No strategies available")}();return function(e){e.subscribe(function(e,t){return c?Promise.resolve(!1):Promise.resolve(u(e,t)).then(function(t){t&&f.share(e)})}),f.addEventListener(function(t){try{c=!0,e.commit(t.type,t.payload)}finally{c=!1}return"done"})}}}])});
//# sourceMappingURL=vuex-shared-mutations.js.map