Skip to content

Commit

Permalink
Fix safari bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gosolivs committed May 11, 2019
1 parent 19810d4 commit bbbcf0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img src="https://storeon.github.io/storeon/logo.svg" align="right"
alt="Storeon logo by Anton Lovchikov" width="160" height="142">

The 165 bytes module for [Storeon](https://github.com/storeon/storeon) to sync state at different tabs of the browser. It syncs state on every change. It uses [Size Limit](https://github.com/ai/size-limit) to control the size.
The 189 bytes module for [Storeon](https://github.com/storeon/storeon) to sync state at different tabs of the browser. It syncs state on every change. It uses [Size Limit](https://github.com/ai/size-limit) to control the size.


## Installation
Expand Down
33 changes: 18 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var eSync = '@sync'

/**
* Storeon module to sync state at different tabs of the browser
* @param {Object} config The config object
Expand All @@ -9,27 +7,32 @@ var crossTab = function (config) {
config = config || {}

var key = config.key || 'storeon-crosstab'
var sync = false
var ignoreNext = false
var ignoreDate = 0

return function (store) {
store.on('@dispatch', function (_, e) {
if (e[0][0] === '@') {
sync = e[0] === eSync
store.on('@dispatch', function (_, event) {
if (event[0][0] === '@') return

if (ignoreNext) {
ignoreNext = false
return
}

if (sync) return

try {
localStorage[key] = JSON.stringify([e[0], e[1], +new Date()])
} catch (er) {}
ignoreDate = +new Date()
localStorage[key] = JSON.stringify([event[0], event[1], ignoreDate])
} catch (e) {}
})

window.addEventListener('storage', function (e) {
if (e.key === key) {
var tip = JSON.parse(e.newValue)
store.dispatch(eSync)
store.dispatch(tip[0], tip[1])
window.addEventListener('storage', function (event) {
if (event.key === key) {
var tip = JSON.parse(event.newValue)

if (ignoreDate !== tip[2]) {
ignoreNext = true
store.dispatch(tip[0], tip[1])
}
}
})
}
Expand Down

0 comments on commit bbbcf0b

Please sign in to comment.