From da7acd149914d78bbdff14f425041380acbeb631 Mon Sep 17 00:00:00 2001 From: "m.hosseinzade" Date: Thu, 26 Mar 2020 02:43:52 +0430 Subject: [PATCH 1/7] add tabsync option --- lib/module.js | 3 ++- lib/storage.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/module.js b/lib/module.js index dc5fa04..2762103 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,7 +11,8 @@ const defaults = { } }, localStorage: { - prefix: '' + prefix: '', + tabSync: true }, ignoreExceptions: false } diff --git a/lib/storage.js b/lib/storage.js index 841fbb0..7bb3f9b 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -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() } // ------------------------------------ @@ -146,6 +148,29 @@ 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) + this.setUniversal(key, value) + }) + }) + } + + enableTabTrack (key) { + this.trackTabKeys.push(key) + } + + disableTabTrack (key) { + const i = this.trackTabKeys.indexOf(key) + if (i >= 0) { + this.trackTabKeys.splice(i, 1) + } + } + setLocalStorage (key, value) { if (typeof localStorage === 'undefined' || !this.options.localStorage) { return From 52f74425f57e7e76558ba980c061ab722c82b757 Mon Sep 17 00:00:00 2001 From: "m.hosseinzade" Date: Thu, 26 Mar 2020 03:05:37 +0430 Subject: [PATCH 2/7] check if isset --- lib/storage.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/storage.js b/lib/storage.js index 7bb3f9b..7a6e015 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -155,7 +155,9 @@ export default class Storage { window.addEventListener('storage', () => { this.trackTabKeys.forEach(key => { const value = this.getLocalStorage(key) - this.setUniversal(key, value) + if (isSet(value)) { + this.setUniversal(key, this.getLocalStorage(key)) + } }) }) } From aaf8d856358a6c5b06311934d13deda1fb00de35 Mon Sep 17 00:00:00 2001 From: "m.hosseinzade" Date: Thu, 26 Mar 2020 03:05:45 +0430 Subject: [PATCH 3/7] update readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 70ca5e1..24ff09c 100755 --- a/README.md +++ b/README.md @@ -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 // @@ -69,7 +69,8 @@ and default to the following values: } }, localStorage: { - prefix: '' + prefix: '', + tabSync: true }, ignoreExceptions: false, } @@ -126,6 +127,10 @@ For example: - `$storage.removeUniversal(key)` +- `$storage.enableTabTrack(key)` + +- `$storage.disableTabTrack(key)` + - `$storage.getState(key)` - `$storage.setState(key, value)` From 8ef418f2d66f09de5f9169a58c2e5a2d5e693dd1 Mon Sep 17 00:00:00 2001 From: Mehdi Hosseinzade Date: Fri, 10 Jul 2020 13:27:14 +0430 Subject: [PATCH 4/7] try fix review suggestion for tabSync --- lib/storage.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index 7a6e015..aef6255 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -163,11 +163,14 @@ export default class Storage { } enableTabTrack (key) { - this.trackTabKeys.push(key) + const i = this.trackTabKeys.findIndex(item => item === key) + if (i < 0) { + this.trackTabKeys.push(key) + } } disableTabTrack (key) { - const i = this.trackTabKeys.indexOf(key) + const i = this.trackTabKeys.findIndex(item => item === key) if (i >= 0) { this.trackTabKeys.splice(i, 1) } From 3907773fc58acfaddd6ccea394539bb34ff2b576 Mon Sep 17 00:00:00 2001 From: Mehdi Hosseinzade Date: Fri, 10 Jul 2020 14:25:44 +0430 Subject: [PATCH 5/7] try fix review suggestion for tabSync --- lib/storage.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index aef6255..313a66c 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -163,8 +163,7 @@ export default class Storage { } enableTabTrack (key) { - const i = this.trackTabKeys.findIndex(item => item === key) - if (i < 0) { + if(this.trackTabKeys.find(i => i === key)) { this.trackTabKeys.push(key) } } From cf4d543beaa926af47c01283901bc5163542df49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 9 Dec 2020 13:54:08 +0100 Subject: [PATCH 6/7] Update storage.js --- lib/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/storage.js b/lib/storage.js index 313a66c..44e15c0 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -163,7 +163,7 @@ export default class Storage { } enableTabTrack (key) { - if(this.trackTabKeys.find(i => i === key)) { + if (this.trackTabKeys.find(i => i === key)) { this.trackTabKeys.push(key) } } From db874938e32cb012e42e7dba2f3a96bd32ed44f6 Mon Sep 17 00:00:00 2001 From: Mehdi HosseinZade Date: Wed, 9 Dec 2020 23:01:23 +0330 Subject: [PATCH 7/7] fix problems --- lib/storage.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index 44e15c0..4643cf6 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -156,20 +156,20 @@ export default class Storage { this.trackTabKeys.forEach(key => { const value = this.getLocalStorage(key) if (isSet(value)) { - this.setUniversal(key, this.getLocalStorage(key)) + this.setUniversal(key, value) } }) }) } enableTabTrack (key) { - if (this.trackTabKeys.find(i => i === key)) { + if (!this.trackTabKeys.find(i => i === key)) { this.trackTabKeys.push(key) } } disableTabTrack (key) { - const i = this.trackTabKeys.findIndex(item => item === key) + const i = this.trackTabKeys.findIndex(i => i === key) if (i >= 0) { this.trackTabKeys.splice(i, 1) }