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

Add tabSync option #79

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Options are defined as following:
```js
storage: {
vuex, // boolean or {namespace}
localStorage, // boolean or {prefix }
localStorage, // boolean or {prefix, tabSync }
cookie, // boolean or {prefix, options }
initialState, // Object {}
ignoreExceptions //
Expand All @@ -69,7 +69,8 @@ and default to the following values:
}
},
localStorage: {
prefix: ''
prefix: '',
tabSync: true
},
ignoreExceptions: false,
}
Expand Down Expand Up @@ -126,6 +127,10 @@ For example:

- `$storage.removeUniversal(key)`

- `$storage.enableTabTrack(key)`

- `$storage.disableTabTrack(key)`

- `$storage.getState(key)`

- `$storage.setState(key, value)`
Expand Down
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const defaults = {
}
},
localStorage: {
prefix: ''
prefix: '',
tabSync: true
},
ignoreExceptions: false
}
Expand Down
30 changes: 30 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export default class Storage {
constructor (ctx, options) {
this.ctx = ctx
this.options = options
this.trackTabKeys = []

this._initState(options.initialState)
this._initTabSync()
}

// ------------------------------------
Expand Down Expand Up @@ -146,6 +148,34 @@ export default class Storage {
// Local storage
// ------------------------------------

_initTabSync () {
if (typeof localStorage === 'undefined' || !this.options.localStorage || !this.options.localStorage.tabSync || !process.client) {
return
}
window.addEventListener('storage', () => {
this.trackTabKeys.forEach(key => {
const value = this.getLocalStorage(key)
if (isSet(value)) {
this.setUniversal(key, this.getLocalStorage(key))
}
})
})
}

enableTabTrack (key) {
const i = this.trackTabKeys.findIndex(item => item === key)
if (i < 0) {
atinux marked this conversation as resolved.
Show resolved Hide resolved
this.trackTabKeys.push(key)
}
}

disableTabTrack (key) {
const i = this.trackTabKeys.findIndex(item => item === key)
if (i >= 0) {
this.trackTabKeys.splice(i, 1)
}
}

setLocalStorage (key, value) {
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
return
Expand Down