Skip to content

Commit e2de136

Browse files
committed
chore: update
1 parent 3aa5bf0 commit e2de136

File tree

6 files changed

+157
-44
lines changed

6 files changed

+157
-44
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# vuex-localForage
2-
本项目作为一个Vuex 插件,用于把store中的数据通过localForage存储到indexDB中
2+
3+
本项目是一个[Vuex](https://vuex.vuejs.org/) plugin,用于自动把store中的数据永久存储,例如localStorage、Cookies、localForage。
4+
5+
**package status**
6+
[![GitHub stars](https://img.shields.io/github/stars/championswimmer/vuex-persist.svg?style=social&label=%20vuex-refesh-storage)](http://github.com/blackdous/vuex-refesh-storage)
7+
[![npm](https://img.shields.io/npm/v/vuex-refesh-storage.svg?colorB=dd1100)](http://npmjs.com/vuex-refesh-storage)
8+
[![npm](https://img.shields.io/npm/dw/vuex-refesh-storage.svg?colorB=fc4f4f)](http://npmjs.com/vuex-refesh-storage)
9+
[![license](https://img.shields.io/github/license/blackdous/vuex-refesh-storage.svg)]()
10+
11+
**package:size**
12+
[![npm:size:gzip](https://img.shields.io/bundlephobia/minzip/vuex-refesh-storage.svg?label=npm:size:gzip)](https://bundlephobia.com/result?p=vuex-refesh-storage)
13+
[![umd:min:gzip](https://img.badgesize.io/https://unpkg.com/vuex-refesh-storage?compression=gzip&label=umd:min:gzip)](https://unpkg.com/vuex-refesh-storage)
14+
[![umd:min:brotli](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/vuex-refesh-storage?compression=brotli&label=umd:min:brotli)](https://cdn.jsdelivr.net/npm/vuex-refesh-storage)
15+
16+
## Install
17+
18+
```bash
19+
npm install vuex-refesh-storage -S
20+
# or yarn
21+
yarn add vuex-refesh-storage
22+
```
23+
24+
使用 [UMD](https://github.com/umdjs/umd)[unpkg](https://unpkg.com):
25+
26+
```html
27+
<script src="https://unpkg.com/vuex-refesh-storage@0.1.0/dist/umd/index.min.js"></script>
28+
```
29+
30+
会在全局暴露一个`window.VuexRefeshStorage`对象。
31+
32+
## 用法
33+
34+
**vuex-refesh-storage**
35+
36+

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@blackdous/vuex_refesh_storage",
3-
"version": "1.0.0",
2+
"name": "vuex-refesh-storage",
3+
"version": "0.1.0",
44
"description": "A Vuex Refesh Storage plugin Typescript ",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/Options.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Author: 19080088
44
* @Date: 2021-04-19 08:51:29
55
* @LastEditors: 19080088
6-
* @LastEditTime: 2021-04-22 17:15:49
6+
* @LastEditTime: 2021-04-22 22:58:09
77
*/
88
import { Store, MutationPayload } from 'vuex'
99
import { AsyncStorage } from './AsyncStorage'
@@ -52,12 +52,16 @@ export interface Options<State> {
5252
/**
5353
* method to get state
5454
*/
55-
getState?: (key: string, storage: Storage | AsyncStorage | DefaultStorage) => Promise<void> | void
55+
getState?: (key: string, storage: Storage | AsyncStorage | DefaultStorage) => void | Promise<void>
5656
/**
5757
* filter state.replace
5858
*/
5959
filter?: (mutation: MutationPayload) => boolean
60-
60+
// after Store.replaceState execution
61+
initAfterFunction?: (store: Store<State>) => {}
62+
/**
63+
* filter modules
64+
*/
6165
reducer?: (state: State) => {}
6266
/**
6367
* Method to retrieve state from persistence

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class VuexRefeshStorage<State> implements Options<State> {
4040
public setState: (key: string, state: any, storage: Storage | AsyncStorage | DefaultStorage) => Promise<void> | void
4141
public reducer: (state: State) => Partial<State>
4242
public filter: (mutation: Payload) => boolean
43+
public initAfterFunction: (store: Store<State>) => {}
4344

4445

4546
/**
@@ -70,6 +71,9 @@ export class VuexRefeshStorage<State> implements Options<State> {
7071
this.overwrite = options.overwrite || false
7172

7273
this.filter = options.filter || ((mutation) => true)
74+
this.initAfterFunction = options.initAfterFunction || ((store) => {
75+
return {}
76+
})
7377
this.setState = (
7478
options.setState ?
7579
options.setState :
@@ -96,6 +100,7 @@ export class VuexRefeshStorage<State> implements Options<State> {
96100
const reState = this.overwrite ? initState : merge(store.state, currentState || {}, this.deepMergeOptions)
97101
store.replaceState(reState as State)
98102

103+
this.initAfterFunction(store)
99104
this.subscribe(store)((mutation: MutationPayload, state: State) => {
100105
if (this.filter(mutation)) {
101106
this.setState(this.key, this.reducer(state), this.storage)
@@ -120,10 +125,12 @@ export class VuexRefeshStorage<State> implements Options<State> {
120125

121126
this.install = (store: Store<State>) => {
122127
const initState = this.initStorage ? this.getState(this.key, this.storage) : {}
128+
// console.log('initState: ', this.getState(this.key, this.storage), initState);
123129
const reState = this.overwrite ? initState : merge(store.state, initState || {}, this.deepMergeOptions)
124130
store.replaceState(reState as State)
125-
131+
this.initAfterFunction(store)
126132
this.subscribe(store)((mutation: MutationPayload, state: State) => {
133+
// console.log('mutation: ', mutation);
127134
if (this.filter(mutation)) {
128135
// console.log('this.filter(mutation): ', this.reducer(state), state, options?.modules);
129136
this.setState(this.key, this.reducer(state), this.storage)

test/vuex-asyncStorage.spec.ts

Whitespace-only changes.

test/vuex-options.spec.ts

Lines changed: 105 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue';
2-
import Vuex, { Store } from 'vuex';
2+
import Vuex, { Store, MutationPayload } from 'vuex';
33
// @ts-ignore
44
import Storage from 'dom-storage';
55

@@ -8,8 +8,8 @@ const newJSON = require('flatted');
88
import { VuexRefeshStorage } from '../src/index';
99

1010
Vue.use(Vuex)
11-
1211
const storage = new Storage();
12+
const objStorage: any = {}
1313

1414
describe("test: options", () => {
1515
it("test: options custome", () => {
@@ -28,7 +28,7 @@ describe("test: options", () => {
2828
expect(vuexRefeshStorage.overwrite).toBe(true);
2929
expect(vuexRefeshStorage.key).toBe('custumer');
3030
expect(storage.getItem('custumer')).toBe(null);
31-
})
31+
});
3232

3333
it("test: options.initStorage: false or true ", () => {
3434
// options.initStorage: true
@@ -82,7 +82,7 @@ describe("test: options", () => {
8282
expect(storeTwo.state).toEqual({
8383
normal: 'default'
8484
})
85-
})
85+
});
8686

8787
it("test: modules", () => {
8888
storage.clear();
@@ -110,7 +110,7 @@ describe("test: options", () => {
110110

111111
store.commit('custom/changeState');
112112
expect(storage.getItem('custom')).toBe(JSON.stringify({custom: { change: "state" }}))
113-
})
113+
});
114114

115115
it("test: flatted to should not clone circular objects", () => {
116116
storage.clear();
@@ -137,7 +137,7 @@ describe("test: options", () => {
137137
vuexRefeshStorage.install(store);
138138
store.commit('updateState')
139139
expect(storage.getItem('vuex')).toBe(newJSON.stringify({ circularState: initState }))
140-
})
140+
});
141141

142142
it("should not persist whole store if modules array is empty", () => {
143143
storage.clear();
@@ -162,7 +162,7 @@ describe("test: options", () => {
162162
vuexRefeshStorage.install(store);
163163
store.commit('updateState');
164164
expect(storage.getItem('common')).toBe(JSON.stringify({}))
165-
})
165+
});
166166

167167
it("should not persist whole store if modules array", () => {
168168
storage.clear();
@@ -190,7 +190,7 @@ describe("test: options", () => {
190190
expect(storage.getItem('common')).toBe(JSON.stringify({circularState: {
191191
name: '12312'
192192
}}))
193-
})
193+
});
194194

195195
it("should not persist null values", () => {
196196
storage.clear();
@@ -214,7 +214,7 @@ describe("test: options", () => {
214214
orignal: '222',
215215
default: null
216216
}))
217-
})
217+
});
218218
it("should not merge array values when rehydrating by default", () => {
219219
storage.clear();
220220

@@ -234,43 +234,111 @@ describe("test: options", () => {
234234
persisted: ["state", "json"]
235235
});
236236
expect(store.subscribe).toBeCalled()
237-
})
237+
});
238238

239-
it("should apply a custom arrayMerger function", () => {
240-
storage.clear();
239+
it("should apply a custom arrayMerger function", () => {
240+
storage.clear();
241241

242-
storage.setItem("vuex", JSON.stringify({ persisted: [1, 2] }));
242+
storage.setItem("vuex", JSON.stringify({ persisted: [1, 2] }));
243243

244-
const store = new Vuex.Store({ state: { persisted: [1, 2, 3] } });
245-
store.replaceState = jest.fn();
246-
store.subscribe = jest.fn();
244+
const store = new Vuex.Store({ state: { persisted: [1, 2, 3] } });
245+
store.replaceState = jest.fn();
246+
store.subscribe = jest.fn();
247+
248+
const vuexRefeshStorage = new VuexRefeshStorage({
249+
storage,
250+
deepMergeOptions: {
251+
arrayMerge: function (store:any, saved:any) {
252+
return ["hello!"];
253+
}
254+
}
255+
});
256+
vuexRefeshStorage.install(store)
257+
expect(store.replaceState).toBeCalledWith({
258+
persisted: ["hello!"],
259+
});
260+
expect(store.subscribe).toBeCalled();
261+
});
262+
263+
// it("rehydrates store's state through the configured getter", () => {
264+
// storage.clear();
265+
// const store = new Vuex.Store({
266+
// state: {}
267+
// });
268+
// store.replaceState = jest.fn();
269+
// const vuexRefeshStorage = new VuexRefeshStorage({
270+
// storage,
271+
// getState: (key, storage) => () => ({ getter: "item" })
272+
// });
273+
// vuexRefeshStorage.install(store);
274+
// expect(store.replaceState).toBeCalledWith({ getter: "item" });
275+
// })
276+
it("persist the changed state back through the configured setter", () => {
277+
expect.assertions(1);
278+
storage.clear();
247279

280+
const store = new Vuex.Store({
281+
state: {
282+
setter: ''
283+
},
284+
mutations: {
285+
setCommit (state) {
286+
state.setter = "item"
287+
}
288+
}
289+
});
248290
const vuexRefeshStorage = new VuexRefeshStorage({
249291
storage,
250-
deepMergeOptions: {
251-
arrayMerge: function (store:any, saved:any) {
252-
return ["hello!"];
292+
setState: (key, state) => {
293+
expect(state).toEqual({ setter: "item" })
294+
}
295+
});
296+
297+
vuexRefeshStorage.install(store);
298+
store.commit('setCommit')
299+
});
300+
301+
it("filters to specific mutations", () => {
302+
storage.clear();
303+
const store = new Vuex.Store({
304+
state: {
305+
normal: ''
306+
},
307+
mutations: {
308+
defaultCommit (state, newVal) {
309+
state.normal = newVal
310+
},
311+
filter (state, newVal) {
312+
state.normal = newVal
253313
}
254314
}
255315
});
256-
vuexRefeshStorage.install(store)
257-
expect(store.replaceState).toBeCalledWith({
258-
persisted: ["hello!"],
316+
const vuexRefeshStorage = new VuexRefeshStorage({
317+
storage,
318+
filter: (mutation) => mutation.type === 'filter'
259319
});
260-
expect(store.subscribe).toBeCalled();
261-
})
320+
vuexRefeshStorage.install(store);
321+
store.commit('defaultCommit', 'state');
322+
expect(storage.getItem('vuex')).toBeNull()
323+
store.commit('filter', 'state');
324+
expect(storage.getItem('vuex')).toBe(JSON.stringify({normal: 'state'}))
325+
})
262326

263-
it("rehydrates store's state through the configured getter", () => {
264-
storage.clear();
265-
const store = new Vuex.Store({
266-
state: {}
267-
});
268-
store.replaceState = jest.fn();
269-
const vuexRefeshStorage = new VuexRefeshStorage({
270-
storage,
271-
getState: () => ({ getter: "item" })
272-
});
273-
vuexRefeshStorage.install(store);
274-
expect(store.replaceState).toBeCalledWith({ getter: "item" });
275-
})
327+
it("should call rehydrated callback once the state is replaced", () => {
328+
storage.clear();
329+
330+
storage.setItem('vuex', JSON.stringify({ persisted: "json" }));
331+
const store = new Vuex.Store({
332+
state: { original: "state" }
333+
});
334+
const initAfterFunction = jest.fn();
335+
336+
const vuexRefeshStorage = new VuexRefeshStorage({
337+
storage,
338+
initAfterFunction
339+
});
340+
vuexRefeshStorage.install(store);
341+
342+
expect(initAfterFunction).toBeCalledWith(store);
343+
});
276344
})

0 commit comments

Comments
 (0)