1
import { Store, VuexPlugin, Option } from './interfaces'
2 1
import { copy } from './objpath'
3 1
import { localStorage } from './drivers'
4 1
import defaultMerge from './merge'
5

6
/**
7
 * Create Vuex plugin
8
 */
9 1
export function createVuexPlugin(option: Option): VuexPlugin<object> {
10 1
	const { keys, merge = defaultMerge, namespace: ns, driver = localStorage } = option
11 1
	return (store: Store<object>) => {
12 1
		if (driver.has(ns)) {
13 1
			const data = driver.get(ns)
14 1
			store.replaceState(merge(store.state, data))
15
		} else {
16 1
			const data = {}
17 1
			for (const k of keys) {
18 1
				copy(data, store.state, k)
19
			}
20 1
			driver.set(ns, data)
21
		}
22 1
		store.subscribe((mutation, state) => {
23 1
			const data = {}
24 1
			for (const k of keys) {
25 1
				copy(data, state, k)
26
			}
27 1
			driver.set(ns, data)
28
		})
29
	}
30
}

Read our documentation on viewing source code .

Loading