-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
46 lines (38 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function crossTab (config = {}) {
let key = config.key || 'storeon-crosstab'
let ignoreNext = false
let ignoreDate = 0
let counter = 0
return function (store) {
store.on('@dispatch', (_, [eventName, data]) => {
if (eventName[0] === '@') {
return
}
if (ignoreNext) {
ignoreNext = false
return
}
if (config.filter && !config.filter(eventName, data)) {
return
}
try {
ignoreDate = Date.now() + '' + counter++
localStorage[key] = JSON.stringify([eventName, data, ignoreDate])
} catch (e) {}
})
if (typeof window !== 'undefined') {
window.addEventListener('storage', event => {
if (event.key === key) {
let [eventName, data, ignoreDateFromEvent] = JSON.parse(
event.newValue
)
if (ignoreDate !== ignoreDateFromEvent) {
ignoreNext = true
store.dispatch(eventName, data)
}
}
})
}
}
}
module.exports = { crossTab }