mirror of
https://github.com/comatory/fb2iCal
synced 2025-02-17 12:10:36 +01:00
feature: create event store in preparation of table component
replacement
This commit is contained in:
parent
e28b378e07
commit
985b580d7d
@ -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',
|
||||
EVENTS: 'fb-to-ical-events',
|
||||
}
|
||||
|
||||
class StorageListener {
|
||||
@ -13,10 +14,12 @@ class StorageListener {
|
||||
window.addEventListener('storage', this._handleStorageChange)
|
||||
|
||||
const unsubscribeConfigStore = configStore.subscribe(this._handleConfigStoreChange)
|
||||
const unsubscribeEventStore = eventStore.subscribe(this._handleEventStoreChange)
|
||||
|
||||
this._storeSubscribers = new Set([
|
||||
...this._storeSubscribers,
|
||||
unsubscribeConfigStore,
|
||||
unsubscribeEventStore,
|
||||
])
|
||||
}
|
||||
|
||||
@ -32,6 +35,9 @@ class StorageListener {
|
||||
case STORAGE_KEYS.CONFIG:
|
||||
configStore.set(JSON.parse(event.newValue))
|
||||
break
|
||||
case STORAGE_KEYS.EVENTS:
|
||||
eventStore.set(JSON.parse(event.newValue))
|
||||
break
|
||||
default:
|
||||
return
|
||||
}
|
||||
@ -40,6 +46,10 @@ class StorageListener {
|
||||
_handleConfigStoreChange(value) {
|
||||
localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(value))
|
||||
}
|
||||
|
||||
_handleEventStoreChange(value) {
|
||||
localStorage.setItem(STORAGE_KEYS.EVENTS, JSON.stringify(value))
|
||||
}
|
||||
}
|
||||
|
||||
export default new StorageListener()
|
||||
|
@ -1,7 +1,9 @@
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
import { STORAGE_KEYS } from '../services/storageListener'
|
||||
|
||||
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)
|
||||
|
||||
|
35
lib/frontend/stores/eventStore.js
Normal file
35
lib/frontend/stores/eventStore.js
Normal 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()
|
@ -1,5 +1,7 @@
|
||||
import configStore from './configStore'
|
||||
import eventStore from './eventStore'
|
||||
|
||||
export {
|
||||
configStore,
|
||||
eventStore,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user