reactant-storage
Version:
A persistence storage plugin for Reactant
2 lines (1 loc) • 9.24 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("reactant-module"),require("redux-persist")):"function"==typeof define&&define.amd?define(["exports","react","reactant-module","redux-persist"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).reactantStorage={},t.React,t.reactantModule,t.reduxPersist)}(this,(function(t,e,r,o){"use strict";var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},n(t,e)};var i=function(){return i=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var n in e=arguments[r])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},i.apply(this,arguments)};function s(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}function a(t){var e="function"==typeof Symbol&&Symbol.iterator,r=e&&t[e],o=0;if(r)return r.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function u(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)s.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return s}function l(t,e,r){if(r||2===arguments.length)for(var o,n=0,i=e.length;n<i;n++)!o&&n in e||(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))}function c(t){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c(t)}function p(t,e,r){return e&&function(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}(t.prototype,e),t}function f(t){return f=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},f(t)}function d(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function y(t,e){return y=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},y(t,e)}function h(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}"function"==typeof SuppressedError&&SuppressedError;var b=function(t){function e(){var t,r;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);for(var o=arguments.length,n=new Array(o),i=0;i<o;i++)n[i]=arguments[i];return r=function(t,e){return!e||"object"!==c(e)&&"function"!=typeof e?d(t):e}(this,(t=f(e)).call.apply(t,[this].concat(n))),h(d(r),"state",{bootstrapped:!1}),h(d(r),"_unsubscribe",void 0),h(d(r),"handlePersistorState",(function(){r.props.persistor.getState().bootstrapped&&(r.props.onBeforeLift?Promise.resolve(r.props.onBeforeLift()).finally((function(){return r.setState({bootstrapped:!0})})):r.setState({bootstrapped:!0}),r._unsubscribe&&r._unsubscribe())})),r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&y(t,e)}(e,t),p(e,[{key:"componentDidMount",value:function(){this._unsubscribe=this.props.persistor.subscribe(this.handlePersistorState),this.handlePersistorState()}},{key:"componentWillUnmount",value:function(){this._unsubscribe&&this._unsubscribe()}},{key:"render",value:function(){return"production"!==process.env.NODE_ENV&&"function"==typeof this.props.children&&this.props.loading&&console.error("redux-persist: PersistGate expects either a function child or loading prop, but not both. The loading prop will be ignored."),"function"==typeof this.props.children?this.props.children(this.state.bootstrapped):this.state.bootstrapped?this.props.children:this.props.loading}}]),e}(e.PureComponent);h(b,"defaultProps",{children:null,loading:null});var v=Symbol("StorageOptions"),g=function(t){var e,o=t[r.stateKey];return null===(e=null==o?void 0:o._persist)||void 0===e?void 0:e.rehydrated},m=function(t){function c(n){var s,a=t.call(this)||this;return a.blacklist=["router","lastAction"],a.rehydrated=!1,a.persistConfig={},a.storageSettingMap=new Map,a.middleware=function(t){return function(t){return function(e){var n,i;if(a[r.enableAutoComputedKey]&&e.type===o.REHYDRATE&&"root"!==e.key){var s=r.getRef(a).modules[e.key];(null!==(i=Object.keys(null!==(n=e.payload)&&void 0!==n?n:{}))&&void 0!==i?i:[]).forEach((function(t){var o;"_persist"!==t&&(null===(o=s[r.signalMapKey])||void 0===o?void 0:o[t])&&(s[r.signalMapKey][t].value=e.payload[t])}))}return t(e)}}},a.manualPersist=!1,a.paused=!1,a.rehydrateCallbackSet=new Set,a.provider=function(t){var r,o;return(null===(o=null===(r=a.store)||void 0===r?void 0:r.getState()._persist)||void 0===o?void 0:o.rehydrated)?e.createElement(e.Fragment,null,t.children):e.createElement(b,{loading:a.options.loading||null,persistor:a.persistor},t.children)},a.options=i(i({},n),{whitelist:null!==(s=n.whitelist)&&void 0!==s?s:[]}),a.persistRootConfig=i({key:"root",timeout:null},a.options),a}var p,f;return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(c,t),c.prototype.setStorage=function(t,e){var o=this,n=t;this.storageSettingMap.set(n,(function(){var t,s=i(i({storage:o.options.storage},e),{key:n[r.identifierKey]});Object.assign(o.persistConfig,((t={})[n[r.identifierKey]]=s,t))}))},c.prototype.getRehydrated=function(t){if(!this.storageSettingMap.has(t))throw new Error("Module '".concat(t.constructor.name,"' is not set to storage persistent."));return g(t)},c.prototype.beforeCombineRootReducers=function(t){var e,r,n,i=this;null===(n=this.beforeCombinePersistReducer)||void 0===n||n.call(this);try{for(var s=a(this.storageSettingMap),l=s.next();!l.done;l=s.next()){var c=u(l.value,2);c[0];(0,c[1])()}}catch(t){e={error:t}}finally{try{l&&!l.done&&(r=s.return)&&r.call(s)}finally{if(e)throw e.error}}return Object.keys(t).forEach((function(e){var r;/^@@reactant\//.test(e)&&i.blacklist.push(e);var n=i.persistConfig[e];if(n){var s=o.persistReducer(n,t[e]);Object.assign(t,((r={})[e]=s,r))}else i.persistRootConfig.blacklist})),t},c.prototype.afterCombineRootReducers=function(t){return o.persistReducer(i({blacklist:l(l([],u(Object.keys(this.persistConfig)),!1),u(this.blacklist),!1)},this.persistRootConfig),t)},Object.defineProperty(c.prototype,"store",{get:function(){return this[r.storeKey]},enumerable:!1,configurable:!0}),c.prototype.afterCreateStore=function(t){var e=this;this.paused=this.manualPersist;var r=t.replaceReducer;return t.replaceReducer=function(n){e.rehydrated=!1,r(n),e.persistor=o.persistStore(t,{manualPersist:e.manualPersist},(function(){var t;e.rehydrated=!0,null===(t=e._onRehydrated)||void 0===t||t.call(e)})),e._enhancePersistor()},this.persistor=o.persistStore(t,{manualPersist:this.manualPersist},(function(){var t;e.rehydrated=!0,null===(t=e._onRehydrated)||void 0===t||t.call(e)})),this._enhancePersistor(),t},c.prototype._enhancePersistor=function(){var t=this;if(!this.persistor)throw new Error("Persistor is not created yet.");var e=this.persistor,r=e.pause,o=e.persist;this.persistor.pause=function(){t.paused||(t.paused=!0,r())},this.persistor.persist=function(){t.paused&&(t.paused=!1,o())}},c.prototype.pause=function(){var t;return null===(t=this.persistor)||void 0===t?void 0:t.pause()},c.prototype.persist=function(){var t;return null===(t=this.persistor)||void 0===t?void 0:t.persist()},c.prototype.flush=function(){var t;return null===(t=this.persistor)||void 0===t?void 0:t.flush()},c.prototype.purge=function(){var t;return null===(t=this.persistor)||void 0===t?void 0:t.purge()},c.prototype._onRehydrated=function(){var t,e;if(this.rehydrateCallbackSet.size){var r=Array.from(this.rehydrateCallbackSet);this.rehydrateCallbackSet.clear();try{for(var o=a(r),n=o.next();!n.done;n=o.next()){(0,n.value)()}}catch(e){t={error:e}}finally{try{n&&!n.done&&(e=o.return)&&e.call(o)}finally{if(t)throw t.error}}}},c.prototype.onRehydrated=function(t){this.rehydrated?t():this.rehydrateCallbackSet.add(t)},c=function(t,e,r,o){var n,i=arguments.length,s=i<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,r,o);else for(var a=t.length-1;a>=0;a--)(n=t[a])&&(s=(i<3?n(s):i>3?n(e,r,s):n(e,r))||s);return i>3&&s&&Object.defineProperty(e,r,s),s}([r.injectable(),(p=0,f=r.inject(v),function(t,e){f(t,e,p)}),s("design:paramtypes",[Object])],c),c}(r.PluginModule);Object.defineProperty(t,"REHYDRATE",{enumerable:!0,get:function(){return o.REHYDRATE}}),t.Storage=m,t.StorageOptions=v,t.getRehydrated=g}));