-
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. - `useSessionStorage` adds an `onSnapshot` listener that persists store snapshots in storage.
- Loading branch information
1 parent
735c2bb
commit ddcff57
Showing
8 changed files
with
89 additions
and
57 deletions.
There are no files selected for viewing
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
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 useSavedSnapshot } from './useSavedSnapshot' | ||
export { default as useSessionStorage } from './useSessionStorage' | ||
export { default as useStore } from './useStore' | ||
export { default as useWorkflowSnapshot } from './useWorkflowSnapshot' |
53 changes: 6 additions & 47 deletions
53
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,12 @@ | ||
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 { useSavedSnapshot, useSessionStorage, useStore } from './' | ||
|
||
export default function useHydratedStore({ authClient, client }, cachePanoptesData = false, storageKey) { | ||
const [initialState, setInitialState] = useState(null) | ||
const [loaded, setLoaded] = useState(false) | ||
// Asynchronously load a store snapshot from an external source. | ||
const initialState = useSavedSnapshot(cachePanoptesData, storageKey) | ||
// Create a new store from the initialState snapshot. | ||
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() | ||
}, []) | ||
// Write store snapshots to session storage. | ||
const loaded = useSessionStorage(cachePanoptesData, classifierStore, storageKey) | ||
|
||
return { classifierStore, 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 | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/lib-classifier/src/components/Classifier/hooks/useSessionStorage.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,26 @@ | ||
import { addDisposer, onSnapshot } from 'mobx-state-tree' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { asyncSessionStorage } from '@helpers' | ||
|
||
function persist(storageKey, store, storage) { | ||
function _saveSnapshot(snapshot) { | ||
const data = JSON.stringify(snapshot) | ||
storage.setItem(storageKey, data) | ||
} | ||
const snapshotDisposer = onSnapshot(store, _saveSnapshot) | ||
return addDisposer(store, snapshotDisposer) | ||
} | ||
|
||
export default function useSessionStorage(enableStorage, store, storageKey) { | ||
const [loaded, setLoaded] = useState(false) | ||
|
||
useEffect(() => { | ||
if (!loaded && store && enableStorage) { | ||
persist(storageKey, store, asyncSessionStorage) | ||
} | ||
setLoaded(true) | ||
}, [enableStorage, store, loaded]) | ||
|
||
return loaded | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.