1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-06-05 22:09:25 +02:00

feature: sync multiple tabs using local storage

This commit is contained in:
Ondřej Synáček
2020-12-15 21:16:30 +01:00
parent 61985990d0
commit e28b378e07
5 changed files with 59 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
import storageListener from './storageListener'
export {
storageListener,
}

View File

@@ -0,0 +1,45 @@
import { configStore } from '../stores'
const STORAGE_KEYS = {
CONFIG: 'fb-to-ical-config',
}
class StorageListener {
constructor() {
this._storeSubscribers = new Set()
}
register() {
window.addEventListener('storage', this._handleStorageChange)
const unsubscribeConfigStore = configStore.subscribe(this._handleConfigStoreChange)
this._storeSubscribers = new Set([
...this._storeSubscribers,
unsubscribeConfigStore,
])
}
unregister() {
window.removeEventListener('storage', this._handleStorageChange)
this._storeSubscribers.forEach((unsubscribe) => unsubscribe())
this._storeSubscribers.clear()
}
_handleStorageChange(event) {
switch (event.key) {
case STORAGE_KEYS.CONFIG:
configStore.set(JSON.parse(event.newValue))
break
default:
return
}
}
_handleConfigStoreChange(value) {
localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(value))
}
}
export default new StorageListener()