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

only Main process tries to set state #30

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion dist/index.js

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

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 dist/shared-mutations.js

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

21 changes: 19 additions & 2 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"
Expand All @@ -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")
Expand All @@ -28,7 +30,8 @@ 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) {
Expand Down Expand Up @@ -89,11 +92,25 @@ 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)
}
})
}
}
Expand Down