-
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.
Refactor useHydratedStore hook. Remove mst-persist
- consolidate `useStore` and `useHydratedStore`, so that `useHydratedStore` calls `useStore` to create the new store. - add a `persist` helper to manage store snapshots. - remove `mst-persist`.
- Loading branch information
1 parent
617045a
commit 1e3cb92
Showing
9 changed files
with
54 additions
and
32 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
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 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
50 changes: 35 additions & 15 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,33 +1,53 @@ | ||
import { persist } from 'mst-persist' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { asyncSessionStorage } from '@helpers' | ||
import { asyncSessionStorage, persist } from '@helpers' | ||
import { useStore } from './' | ||
|
||
async function hydrateStore(storageKey, classifierStore) { | ||
async function hydrateStore(storageKey) { | ||
let snapshot = {} | ||
try { | ||
await persist(storageKey, classifierStore, { | ||
storage: asyncSessionStorage, | ||
whitelist: ['fieldGuide', 'projects', 'subjects', 'subjectSets', 'tutorials', 'workflows', 'workflowSteps'] | ||
}) | ||
console.log('store hydrated from local storage') | ||
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 useHydratedStore(classifierStore, cachePanoptesData = false, storageKey) { | ||
async function loadSnapshot(storageKey, storage) { | ||
const data = await storage.getItem(storageKey) | ||
const snapshot = JSON.parse(data) | ||
return snapshot | ||
} | ||
|
||
export default function useHydratedStore({ authClient, client }, cachePanoptesData = false, storageKey) { | ||
const [initialState, setInitialState] = useState(null) | ||
const [loaded, setLoaded] = useState(false) | ||
|
||
async function onMount() { | ||
if (cachePanoptesData) { | ||
await hydrateStore(storageKey, classifierStore) | ||
const classifierStore = useStore({ authClient, client, initialState }) | ||
|
||
async function onStoreCreated() { | ||
if (!loaded && classifierStore && cachePanoptesData) { | ||
await persist(storageKey, classifierStore, { storage: asyncSessionStorage }) | ||
} | ||
setLoaded(true) | ||
} | ||
|
||
useEffect(() => { | ||
onMount() | ||
onStoreCreated() | ||
}, [cachePanoptesData, classifierStore, loaded]) | ||
|
||
async function getInitialState() { | ||
let _initialState = {} | ||
if (cachePanoptesData) { | ||
_initialState = await hydrateStore(storageKey) | ||
} | ||
setInitialState(_initialState) | ||
} | ||
|
||
useEffect(() => { | ||
getInitialState() | ||
}, []) | ||
return loaded | ||
|
||
return { classifierStore, 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 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 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 @@ | ||
export { default } from './persist' |
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,8 @@ | ||
import { addDisposer, applySnapshot, onSnapshot } from 'mobx-state-tree' | ||
|
||
export default async function persist(storageKey, store, { storage }) { | ||
addDisposer(store, onSnapshot(store, snapshot => { | ||
const data = JSON.stringify(snapshot) | ||
storage.setItem(storageKey, data) | ||
})) | ||
} |
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