{"version":3,"file":"index.min.js","sources":["../../src/SimplePromiseQueue.ts","../../src/utils.ts","../../src/index.ts"],"sourcesContent":["// tslint:disable: variable-name\nexport default class SimplePromiseQueue {\n private readonly _queue: Array> = []\n private _flushing = false\n\n public enqueue(promise: Promise) {\n this._queue.push(promise)\n if (!this._flushing) { return this.flushQueue() }\n return Promise.resolve()\n }\n\n private flushQueue() {\n this._flushing = true\n\n const chain = (): Promise | void => {\n const nextTask = this._queue.shift()\n if (nextTask) {\n return nextTask.then(chain)\n } else {\n this._flushing = false\n }\n }\n return Promise.resolve(chain())\n }\n}\n","import deepmerge from 'deepmerge'\n\nexport type MergeOptionType = 'replaceArrays' | 'concatArrays'\n\nconst options: {[k in MergeOptionType]: deepmerge.Options} = {\n replaceArrays: {\n arrayMerge: (destinationArray, sourceArray, options) => sourceArray\n },\n concatArrays: {\n arrayMerge: (target, source, options) => target.concat(...source)\n }\n}\n\nconst defaultMergeOptions: deepmerge.Options = {\n // replacing arrays\n \n}\n\nexport function merge(into: Partial, from: Partial, mergeOption: MergeOptionType): I & F & {} {\n return deepmerge(into, from, options[mergeOption])\n}\n","/**\n * Created by championswimmer on 18/07/17.\n */\nimport { Mutation, MutationPayload, Payload, Plugin, Store } from 'vuex'\nimport { AsyncStorage } from './AsyncStorage'\nimport { MockStorage } from './MockStorage'\nimport { PersistOptions } from './PersistOptions'\nimport SimplePromiseQueue from './SimplePromiseQueue'\nimport { merge, MergeOptionType } from './utils'\n\nlet FlattedJSON = JSON\n\n/**\n * A class that implements the vuex persistence.\n * @type S type of the 'state' inside the store (default: any)\n */\nexport class VuexPersistence implements PersistOptions {\n public asyncStorage: boolean\n public storage: Storage | AsyncStorage | undefined\n public restoreState: (key: string, storage?: AsyncStorage | Storage) => Promise | S\n public saveState: (key: string, state: {}, storage?: AsyncStorage | Storage) => Promise | void\n public reducer: (state: S) => Partial\n public key: string\n public filter: (mutation: Payload) => boolean\n public modules: string[]\n public strictMode: boolean\n public supportCircular: boolean\n public mergeOption: MergeOptionType\n\n /**\n * The plugin function that can be used inside a vuex store.\n */\n public plugin: Plugin\n /**\n * A mutation that can be used to restore state\n * Helpful if we are running in strict mode\n */\n public RESTORE_MUTATION: Mutation\n public subscribed: boolean\n\n // tslint:disable-next-line:variable-name\n private _mutex = new SimplePromiseQueue()\n\n /**\n * Create a {@link VuexPersistence} object.\n * Use the plugin function of this class as a\n * Vuex plugin.\n * @param {PersistOptions} options\n */\n public constructor(options?: PersistOptions) {\n if (typeof options === 'undefined') options = {} as PersistOptions\n this.key = ((options.key != null) ? options.key : 'vuex')\n\n this.subscribed = false\n this.supportCircular = options.supportCircular || false\n if (this.supportCircular) {\n FlattedJSON = require('flatted')\n }\n this.mergeOption = options.mergeOption || 'replaceArrays'\n\n let localStorageLitmus = true\n\n try {\n window.localStorage.getItem('')\n } catch (err) {\n localStorageLitmus = false\n }\n\n /**\n * 1. First, prefer storage sent in optinos\n * 2. Otherwise, use window.localStorage if available\n * 3. Finally, try to use MockStorage\n * 4. None of above? Well we gotta fail.\n */\n if (options.storage) { this.storage = options.storage }\n else if (localStorageLitmus) { this.storage = window.localStorage }\n else if (MockStorage) { this.storage = new MockStorage() }\n else { throw new Error(\"Neither 'window' is defined, nor 'MockStorage' is available\") }\n\n /**\n * How this works is -\n * 1. If there is options.reducer function, we use that, if not;\n * 2. We check options.modules;\n * 1. If there is no options.modules array, we use entire state in reducer\n * 2. Otherwise, we create a reducer that merges all those state modules that are\n * defined in the options.modules[] array\n * @type {((state: S) => {}) | ((state: S) => S) | ((state: any) => {})}\n */\n this.reducer = (\n (options.reducer != null)\n ? options.reducer\n : (\n (options.modules == null)\n ? ((state: S) => state)\n : (\n (state: any) =>\n (options!.modules as string[]).reduce((a, i) =>\n merge(a, { [i]: state[i] }, this.mergeOption), {/* start empty accumulator*/ })\n )\n )\n )\n\n this.filter = options.filter || ((mutation) => true)\n\n this.strictMode = options.strictMode || false\n\n this.RESTORE_MUTATION = function RESTORE_MUTATION(state: S, savedState: any) {\n const mergedState = merge(state, savedState || {}, this.mergeOption)\n for (const propertyName of Object.keys(mergedState as {})) {\n (this as any)._vm.$set(state, propertyName, (mergedState as any)[propertyName])\n }\n }\n\n this.asyncStorage = options.asyncStorage || false\n\n if (this.asyncStorage) {\n\n /**\n * Async {@link #VuexPersistence.restoreState} implementation\n * @type {((key: string, storage?: Storage) =>\n * (Promise | S)) | ((key: string, storage: AsyncStorage) => Promise)}\n */\n this.restoreState = (\n (options.restoreState != null)\n ? options.restoreState\n : ((key: string, storage: AsyncStorage) =>\n (storage).getItem(key)\n .then((value) =>\n typeof value === 'string' // If string, parse, or else, just return\n ? (\n this.supportCircular\n ? FlattedJSON.parse(value || '{}')\n : JSON.parse(value || '{}')\n )\n : (value || {})\n )\n )\n )\n\n /**\n * Async {@link #VuexPersistence.saveState} implementation\n * @type {((key: string, state: {}, storage?: Storage) =>\n * (Promise | void)) | ((key: string, state: {}, storage?: Storage) => Promise)}\n */\n this.saveState = (\n (options.saveState != null)\n ? options.saveState\n : ((key: string, state: {}, storage: AsyncStorage) =>\n (storage).setItem(\n key, // Second argument is state _object_ if asyc storage, stringified otherwise\n // do not stringify the state if the storage type is async\n (this.asyncStorage\n ? merge({}, state || {}, this.mergeOption)\n : (\n this.supportCircular\n ? FlattedJSON.stringify(state) as any\n : JSON.stringify(state) as any\n )\n )\n )\n )\n )\n\n /**\n * Async version of plugin\n * @param {Store} store\n */\n this.plugin = (store: Store) => {\n /**\n * For async stores, we're capturing the Promise returned\n * by the `restoreState()` function in a `restored` property\n * on the store itself. This would allow app developers to\n * determine when and if the store's state has indeed been\n * refreshed. This approach was suggested by GitHub user @hotdogee.\n * See https://github.com/championswimmer/vuex-persist/pull/118#issuecomment-500914963\n * @since 2.1.0\n */\n (store as any).restored = ((this.restoreState(this.key, this.storage)) as Promise).then((savedState) => {\n /**\n * If in strict mode, do only via mutation\n */\n if (this.strictMode) {\n store.commit('RESTORE_MUTATION', savedState)\n } else {\n store.replaceState(merge(store.state, savedState || {}, this.mergeOption) as S)\n }\n this.subscriber(store)((mutation: MutationPayload, state: S) => {\n if (this.filter(mutation)) {\n this._mutex.enqueue(\n this.saveState(this.key, this.reducer(state), this.storage) as Promise\n )\n }\n })\n this.subscribed = true\n })\n }\n } else {\n\n /**\n * Sync {@link #VuexPersistence.restoreState} implementation\n * @type {((key: string, storage?: Storage) =>\n * (Promise | S)) | ((key: string, storage: Storage) => (any | string | {}))}\n */\n this.restoreState = (\n (options.restoreState != null)\n ? options.restoreState\n : ((key: string, storage: Storage) => {\n const value = (storage).getItem(key)\n if (typeof value === 'string') {// If string, parse, or else, just return\n return (\n this.supportCircular\n ? FlattedJSON.parse(value || '{}')\n : JSON.parse(value || '{}')\n )\n } else {\n return (value || {})\n }\n })\n )\n\n /**\n * Sync {@link #VuexPersistence.saveState} implementation\n * @type {((key: string, state: {}, storage?: Storage) =>\n * (Promise | void)) | ((key: string, state: {}, storage?: Storage) => Promise)}\n */\n this.saveState = (\n (options.saveState != null)\n ? options.saveState\n : ((key: string, state: {}, storage: Storage) =>\n (storage).setItem(\n key, // Second argument is state _object_ if localforage, stringified otherwise\n (\n this.supportCircular\n ? FlattedJSON.stringify(state) as any\n : JSON.stringify(state) as any\n )\n )\n )\n )\n\n /**\n * Sync version of plugin\n * @param {Store} store\n */\n this.plugin = (store: Store) => {\n const savedState = this.restoreState(this.key, this.storage) as S\n\n if (this.strictMode) {\n store.commit('RESTORE_MUTATION', savedState)\n } else {\n store.replaceState(merge(store.state, savedState || {}, this.mergeOption) as S)\n }\n\n this.subscriber(store)((mutation: MutationPayload, state: S) => {\n if (this.filter(mutation)) {\n this.saveState(this.key, this.reducer(state), this.storage)\n }\n })\n\n this.subscribed = true\n }\n }\n }\n\n /**\n * Creates a subscriber on the store. automatically is used\n * when this is used a vuex plugin. Not for manual usage.\n * @param store\n */\n private subscriber = (store: Store) =>\n (handler: (mutation: MutationPayload, state: S) => any) => store.subscribe(handler)\n}\n\nexport {\n MockStorage, AsyncStorage, PersistOptions\n}\n\nexport default VuexPersistence\n"],"names":["SimplePromiseQueue","promise","this","_queue","push","_flushing","Promise","resolve","flushQueue","chain","nextTask","_this","shift","then","options","replaceArrays","arrayMerge","destinationArray","sourceArray","concatArrays","target","source","concat","merge","into","from","mergeOption","deepmerge","store","handler","subscribe","key","subscribed","supportCircular","FlattedJSON","require","localStorageLitmus","window","localStorage","getItem","err","storage","MockStorage","Error","reducer","modules","state","reduce","a","i","filter","mutation","strictMode","RESTORE_MUTATION","savedState","mergedState","_a","Object","keys","_i","propertyName","_vm","$set","asyncStorage","restoreState","value","parse","JSON","saveState","setItem","stringify","plugin","restored","commit","replaceState","subscriber","_mutex","enqueue"],"mappings":"wSACA,OAISA,oBAAP,SAAeC,GAEb,OADAC,KAAKC,OAAOC,KAAKH,GACZC,KAAKG,UACHC,QAAQC,UADeL,KAAKM,cAI7BR,uBAAR,WAAA,WACEE,KAAKG,WAAY,EAEjB,IAAMI,EAAQ,WACZ,IAAMC,EAAWC,EAAKR,OAAOS,QAC7B,GAAIF,EACF,OAAOA,EAASG,KAAKJ,GAErBE,EAAKN,WAAY,GAGrB,OAAOC,QAAQC,QAAQE,SArB3B,aACmBP,YAA+B,GACxCA,gBAAY,ECCtB,IAAMY,EAAuD,CAC3DC,cAAe,CACbC,WAAY,SAACC,EAAkBC,EAAaJ,GAAY,OAAAI,IAE1DC,aAAc,CACZH,WAAY,SAACI,EAAQC,EAAQP,GAAY,OAAAM,EAAOE,aAAPF,EAAiBC,eAS9CE,EAAYC,EAAkBC,EAAkBC,GAC9D,OAAOC,EAAUH,EAAMC,EAAMX,EAAQY,IC8BrC,WAAmBZ,GAAnB,WARQZ,YAAS,IAAIF,EAoObE,gBAAa,SAAC0B,GACpB,OAAA,SAACC,GAA0D,OAAAD,EAAME,UAAUD,UA5NpD,IAAZf,IAAyBA,EAAU,IAC9CZ,KAAK6B,IAAuB,MAAfjB,EAAQiB,IAAejB,EAAQiB,IAAM,OAElD7B,KAAK8B,YAAa,EAClB9B,KAAK+B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