react-wire-persisted 1.0.6 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import{createWire as e}from"@forminator/react-wire";function t(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function r(e){for(var r=1;arguments.length>r;r++){var n=null!=arguments[r]?arguments[r]:{};r%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 n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function i(e,t,r){return t&&o(e.prototype,t),r&&o(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function u(e){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function c(e,t){return(c=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function l(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function s(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var r,n=u(e);if(t){var o=u(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return l(this,r)}}function f(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==r)return;var n,o,i=[],a=!0,u=!1;try{for(r=r.call(e);!(a=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,o=e}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}(e,t)||p(e,t)||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 y(e){return function(e){if(Array.isArray(e))return v(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||p(e)||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.")}()}function p(e,t){if(e){if("string"==typeof e)return v(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?v(e,t):void 0}}function v(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);t>r;r++)n[r]=e[r];return n}var g={},h=function(e){g[e]=e},m={getItem:function(e){return m[e]},setItem:function(e,t){m[e]=t},removeItem:function(e){delete m[e]}},b=function(e){var t=typeof e;return null===e||!Array.isArray(e)&&("object"!==t&&"function"!==t)},d=Object.freeze({__proto__:null,isPrimitive:b,addKey:h,key:function(e){return h(e)},getKeys:function(){return g},getPrefixedKeys:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=t||g;return e?Object.keys(n).reduce((function(t,o){return r(r({},t),{},a({},o,"".concat(e,".").concat(n[o])))}),{}):n},fakeLocalStorage:m}),O=function(){function e(t,r){if(n(this,e),(this instanceof e?this.constructor:void 0)===e)throw TypeError("StorageProvider is abstract. Extend this class to implement it");this.namespace=t||null,this.registry=r||{}}return i(e,[{key:"setNamespace",value:function(e){}},{key:"register",value:function(e,t){this.registry[e]=t}},{key:"getItem",value:function(e){}},{key:"setItem",value:function(e,t){}},{key:"removeItem",value:function(e){}},{key:"getAll",value:function(){}},{key:"_resetAll",value:function(){}},{key:"resetAll",value:function(){}},{key:"removeAll",value:function(){}}]),e}(),w={logging:{enabled:!1}},j=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&c(e,t)}(r,O);var t=s(r);function r(){var e,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return n(this,r),(e=t.call(this,o,i)).storage=e.getStorage(),e}return i(r,[{key:"getStorage",value:function(){try{return window.localStorage}catch(e){return console.warn("LocalStorageProvider: localStorage not supported"),m}}},{key:"setNamespace",value:function(e){if(this.namespace){if(this.namespace!==e){var t=JSON.parse(JSON.stringify(this.getAll()));this.removeAll();for(var r=0,n=Object.entries(t);n.length>r;r++){var o=f(n[r],2),i=o[1],a=o[0].replace(this.namespace,e);this.setItem(a,i)}this.namespace=e}}else this.namespace=e}},{key:"getItem",value:function(e){var t=this.storage.getItem(e);if(null==t)return null;try{return JSON.parse(t)}catch(e){return t}}},{key:"setItem",value:function(e,t){var r=t;return null!=r&&(r=b(t)?t:JSON.stringify(t)),this.storage.setItem(e,r)}},{key:"removeItem",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return t&&delete this.registry[e],this.storage.removeItem(e)}},{key:"getAll",value:function(){var e=this,t="".concat(this.namespace,".");return Object.keys(this.storage).reduce((function(r,n){return e.namespace&&!n.startsWith(t)||(r[n]=e.storage.getItem(n)),r}),{})}},{key:"_resetAll",value:function(){var e=this,t=0>=arguments.length||void 0===arguments[0]||arguments[0],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],o="".concat(this.namespace,".");Object.keys(localStorage).forEach((function(i){var a=!e.namespace||i.startsWith(o),u=(null==r?void 0:r.includes(i))||!1;a&&!u&&(t?Object.prototype.hasOwnProperty.call(e.registry,i)?e.storage.setItem(i,e.registry[i]):e.storage.removeItem(i):(e.storage.removeItem(i),n&&delete e.registry[i]))}))}},{key:"resetAll",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this._resetAll(!0,e||[],t)}},{key:"removeAll",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this._resetAll(!1,e||[],t)}}]),r}(),k=new j,A=r({},w),I=[],P=function(){return k.namespace},S=function(){return k},_=function(){return A},E=function(e){k.setNamespace(e),k=new j(e||P())},N=function(e){if((A=r(r({},A),e)).logging.enabled)for(console.info("Flushing",I.length,"pending logs");I.length;){var t;(t=console).log.apply(t,y(I.shift()))}},x=function(){for(var e,t=arguments.length,r=Array(t),n=0;t>n;n++)r[n]=arguments[n];A.logging.enabled?(e=console).log.apply(e,r):I.push(r)},R=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(!t&&"number"!=typeof t)throw Error("createPersistedWire: Key cannot be a falsey value (".concat(t,"}"));k.register(t,n);var o=e(n),i=function(){return o.getValue()},a=function(e){return k.setItem(t,e),o.setValue(e)},u=function(e){o.subscribe(e)},c=k.getItem(t),l=null===c?n:c;return x("react-wire-persisted: create",t,{value:n,storedValue:c,initialValue:l}),l!==n&&a(l),r(r({},o),{},{getValue:i,setValue:a,subscribe:u})};export{R as createPersistedWire,w as defaultOptions,P as getNamespace,_ as getOptions,S as getStorage,E as setNamespace,N as setOptions,d as utils};