Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise based filter function #89

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"build": "rollup -c build/rollup.config.js",
"prepublishOnly": "npm run build",
"pretest": "npm run build",
"prepare": "npm run build",
"test": "cd test && mocha -r ts-node/register *.ts",
"test:dirty": "cd test && mocha -r ts-node/register *.ts",
"cover": "nyc npm test"
Expand Down
2 changes: 1 addition & 1 deletion src/PersistOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface PersistOptions<S> {
* Check mutations using <code>mutation.type</code>
* @param mutation object of type {@link Payload}
*/
filter?: (mutation: Payload) => boolean
filter?: (mutation: Payload) => Promise<boolean> | boolean

/**
* Names of modules that you want to persist.
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class VuexPersistence<S> implements PersistOptions<S> {
public saveState: (key: string, state: {}, storage?: AsyncStorage | Storage) => Promise<void> | void
public reducer: (state: S) => Partial<S>
public key: string
public filter: (mutation: Payload) => boolean
public filter: (mutation: Payload) => Promise<boolean> | boolean
public modules: string[]
public strictMode: boolean
public supportCircular: boolean
Expand Down Expand Up @@ -237,7 +237,15 @@ export class VuexPersistence<S> implements PersistOptions<S> {
}

this.subscriber(store)((mutation: MutationPayload, state: S) => {
if (this.filter(mutation)) {
const result = this.filter(mutation)
const promise = result as Promise<boolean>
if (promise.then) {
promise.then((val) => {
if (val) {
this.saveState(this.key, this.reducer(state), this.storage)
}
})
} else if (this.filter(mutation)) {
this.saveState(this.key, this.reducer(state), this.storage)
}
})
Expand Down
51 changes: 51 additions & 0 deletions test/vuex-defaultstorage-asyncfilter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Created by championswimmer on 23/07/17.
*/

import { assert, expect, should } from 'chai'
import Vue from 'vue'
import { Store } from 'vuex'
import Vuex from 'vuex'
import VuexPersistence from '..'

Vue.use(Vuex)
const vuexPersist = new VuexPersistence()
vuexPersist.filter = async (mutation) => (mutation.type === 'dogBark')
console.log((vuexPersist.storage as Storage).getItem('vuex') as string)
const store = new Store<any>({
state: {
dog: {
barks: 0
},
cat: {
mews: 0
}
},
mutations: {
dogBark(state) {
state.dog.barks++
},
catMew(state) {
state.cat.mews++
}
},
plugins: [vuexPersist.plugin]
})
const getSavedStore = () => JSON.parse((vuexPersist.storage as Storage).getItem('vuex') as string)

describe('Storage: Default Storage, Test: reducer, async-filter; Strict Mode: OFF', () => {
it('should persist reduced state', async () => {
store.commit('dogBark')
await ptimeout() // wait for store.commit
expect(getSavedStore().dog.barks).to.equal(1)
store.commit('catMew')
//noinspection TsLint
expect(getSavedStore().cat.mews).to.equal(0)
})
})

function ptimeout(ms = 10) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms)
})
}