1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-02-21 14:10:36 +01:00

feature: create event store in preparation of table component

replacement
This commit is contained in:
Ondřej Synáček 2020-12-15 21:48:38 +01:00
parent e28b378e07
commit 985b580d7d
4 changed files with 52 additions and 3 deletions

View File

@ -1,7 +1,8 @@
import { configStore } from '../stores' import { configStore, eventStore } from '../stores'
const STORAGE_KEYS = { export const STORAGE_KEYS = {
CONFIG: 'fb-to-ical-config', CONFIG: 'fb-to-ical-config',
EVENTS: 'fb-to-ical-events',
} }
class StorageListener { class StorageListener {
@ -13,10 +14,12 @@ class StorageListener {
window.addEventListener('storage', this._handleStorageChange) window.addEventListener('storage', this._handleStorageChange)
const unsubscribeConfigStore = configStore.subscribe(this._handleConfigStoreChange) const unsubscribeConfigStore = configStore.subscribe(this._handleConfigStoreChange)
const unsubscribeEventStore = eventStore.subscribe(this._handleEventStoreChange)
this._storeSubscribers = new Set([ this._storeSubscribers = new Set([
...this._storeSubscribers, ...this._storeSubscribers,
unsubscribeConfigStore, unsubscribeConfigStore,
unsubscribeEventStore,
]) ])
} }
@ -32,6 +35,9 @@ class StorageListener {
case STORAGE_KEYS.CONFIG: case STORAGE_KEYS.CONFIG:
configStore.set(JSON.parse(event.newValue)) configStore.set(JSON.parse(event.newValue))
break break
case STORAGE_KEYS.EVENTS:
eventStore.set(JSON.parse(event.newValue))
break
default: default:
return return
} }
@ -40,6 +46,10 @@ class StorageListener {
_handleConfigStoreChange(value) { _handleConfigStoreChange(value) {
localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(value)) localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(value))
} }
_handleEventStoreChange(value) {
localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value))
}
} }
export default new StorageListener() export default new StorageListener()

View File

@ -1,7 +1,9 @@
import { writable } from 'svelte/store' import { writable } from 'svelte/store'
import { STORAGE_KEYS } from '../services/storageListener'
const createConfigStore = () => { const createConfigStore = () => {
const state = JSON.parse(localStorage.getItem('fb-to-ical-config') || '{}') const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.CONFIG) || '{}')
const { subscribe, set, update } = writable(state) const { subscribe, set, update } = writable(state)

View File

@ -0,0 +1,35 @@
import { writable } from 'svelte/store'
import { STORAGE_KEYS } from '../services/storageListener'
const createEventStore = () => {
const state = JSON.parse(localStorage.getItem(STORAGE_KEYS.EVENTS) || '[]')
const { subscribe, set, update } = writable(state)
const setCalculation = ({ id, link, createdAt, startTime, title }) => {
update((prevState) => ([
...prevState,
{
id,
link,
createdAt: createdAt.toString(),
startTime: startTime.toString(),
title,
},
]))
}
const getState = () => state
return {
...state,
subscribe,
set,
update,
setCalculation,
getState,
}
}
export default createEventStore()

View File

@ -1,5 +1,7 @@
import configStore from './configStore' import configStore from './configStore'
import eventStore from './eventStore'
export { export {
configStore, configStore,
eventStore,
} }