Skip to content

Bump electron-store from 2.0.0 to 6.0.1 #62

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion dist/persisted-state.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -56,6 +56,6 @@
},
"dependencies": {
"deepmerge": "^2.1.1",
"electron-store": "^2.0.0"
"electron-store": "^8.0.1"
}
}
23 changes: 20 additions & 3 deletions src/persisted-state.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import merge from "deepmerge"
import Store from "electron-store"

const SET_STATE_THROTTLE = 0
const STORAGE_NAME = "vuex"
const STORAGE_KEY = "state"
const STORAGE_TEST_KEY = "test"
@@ -14,6 +15,7 @@ class PersistedState {
loadOptions() {
if (!this.options.storage) this.options.storage = this.createStorage()
if (!this.options.storageKey) this.options.storageKey = STORAGE_KEY
if (!this.options.throttle) this.options.throttle = SET_STATE_THROTTLE

this.whitelist = this.loadFilter(this.options.whitelist, "whitelist")
this.blacklist = this.loadFilter(this.options.blacklist, "blacklist")
@@ -28,7 +30,9 @@ class PersistedState {
}

setState(state) {
this.options.storage.set(this.options.storageKey, state)
if (process.env.NODE_ENV === "test" || process.type === "browser") {
this.options.storage.set(this.options.storageKey, state)
}
}

loadFilter(filter, name) {
@@ -89,11 +93,24 @@ class PersistedState {
}

subscribeOnChanges() {
let lastSetStateDate = 0
let trailingEventTimeout = -1

this.store.subscribe((mutation, state) => {
if (this.blacklist && this.blacklist(mutation)) return
if (this.whitelist && !this.whitelist(mutation)) return

this.setState(state)
if (this.options.throttle) {
const now = Date.now()
if (!lastSetStateDate || now > lastSetStateDate + this.options.throttle) {
lastSetStateDate = now
this.setState(state)
} else {
clearTimeout(trailingEventTimeout)
trailingEventTimeout = setTimeout(() => this.setState(state), this.options.throttle)
}
} else {
this.setState(state)
}
})
}
}