Skip to content

Commit

Permalink
Add usePersistedStore and useSavedSnapshot
Browse files Browse the repository at this point in the history
- `useSavedSnapshot` loads a store snapshot from storage.
- `usePersistedStore` adds an `onSnapshot` listener that persists store snapshots in storage.
  • Loading branch information
eatyourgreens committed Feb 18, 2022
1 parent b73fa68 commit aef3ad0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as useHydratedStore } from './useHydratedStore'
export { default as usePersistedStore } from './usePersistedStore'
export { default as useSavedSnapshot } from './useSavedSnapshot'
export { default as useStore } from './useStore'
export { default as useWorkflowSnapshot } from './useWorkflowSnapshot'
Original file line number Diff line number Diff line change
@@ -1,53 +1,9 @@
import { useEffect, useState } from 'react'

import { asyncSessionStorage, persist } from '@helpers'
import { useStore } from './'

async function hydrateStore(storageKey) {
let snapshot = {}
try {
snapshot = await loadSnapshot(storageKey, asyncSessionStorage)
console.log('store loaded from local storage')
} catch (error) {
console.log('store snapshot error.')
console.error(error)
}
return snapshot
}

async function loadSnapshot(storageKey, storage) {
const data = await storage.getItem(storageKey)
const snapshot = JSON.parse(data)
return snapshot
}
import { usePersistedStore, useSavedSnapshot, useStore } from './'

export default function useHydratedStore({ authClient, client }, cachePanoptesData = false, storageKey) {
const [initialState, setInitialState] = useState(null)
const [loaded, setLoaded] = useState(false)
const initialState = useSavedSnapshot(cachePanoptesData, storageKey)
const classifierStore = useStore({ authClient, client, initialState })

async function onStoreCreated() {
if (!loaded && classifierStore && cachePanoptesData) {
await persist(storageKey, classifierStore, { storage: asyncSessionStorage })
}
setLoaded(true)
}

useEffect(() => {
onStoreCreated()
}, [cachePanoptesData, classifierStore, loaded])

async function getInitialState() {
let _initialState = {}
if (cachePanoptesData) {
_initialState = await hydrateStore(storageKey)
}
setInitialState(_initialState)
}

useEffect(() => {
getInitialState()
}, [])
const loaded = usePersistedStore(cachePanoptesData, classifierStore, storageKey)

return { classifierStore, loaded }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, useState } from 'react'

import { asyncSessionStorage, persist } from '@helpers'

export default function usePersistedStore(enableStorage, store, storageKey) {
const [loaded, setLoaded] = useState(false)
async function onStoreCreated() {
if (!loaded && store && enableStorage) {
await persist(storageKey, store, { storage: asyncSessionStorage })
}
setLoaded(true)
}

useEffect(() => {
onStoreCreated()
}, [enableStorage, store, loaded])

return loaded
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect, useState } from 'react'

import { asyncSessionStorage } from '@helpers'

async function loadSnapshot(storageKey, storage) {
const data = await storage.getItem(storageKey)
const snapshot = JSON.parse(data)
return snapshot
}

async function hydrateStore(storageKey) {
let snapshot = {}
try {
snapshot = await loadSnapshot(storageKey, asyncSessionStorage)
console.log('store loaded from local storage')
} catch (error) {
console.log('store snapshot error.')
console.error(error)
}
return snapshot
}

export default function useSavedSnapshot(enableStorage, storageKey) {
const [initialState, setInitialState] = useState(null)

async function getInitialState() {
let _initialState = {}
if (enableStorage) {
_initialState = await hydrateStore(storageKey)
}
setInitialState(_initialState)
}

useEffect(() => {
getInitialState()
}, [storageKey])

return initialState
}

0 comments on commit aef3ad0

Please sign in to comment.