-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add usePersistedStore and useSavedSnapshot
- `useSavedSnapshot` loads a store snapshot from storage. - `usePersistedStore` adds an `onSnapshot` listener that persists store snapshots in storage.
- Loading branch information
1 parent
b73fa68
commit 6107d19
Showing
4 changed files
with
64 additions
and
47 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
packages/lib-classifier/src/components/Classifier/hooks/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
50 changes: 3 additions & 47 deletions
50
packages/lib-classifier/src/components/Classifier/hooks/useHydratedStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/lib-classifier/src/components/Classifier/hooks/usePersistedStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/lib-classifier/src/components/Classifier/hooks/useSavedSnapshot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |