-
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.
Replace the three store hooks with a single `useHydratedStore` hook. Use synchronous storage.
- Loading branch information
1 parent
7a7788a
commit c9fac2e
Showing
10 changed files
with
56 additions
and
168 deletions.
There are no files selected for viewing
25 changes: 0 additions & 25 deletions
25
packages/lib-classifier/src/helpers/asyncSessionStorage/asyncSessionStorage.js
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/lib-classifier/src/helpers/asyncSessionStorage/index.js
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
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' |
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,10 +1,51 @@ | ||
import { useSavedSnapshot, useSessionStorage, useStore } from './' | ||
import makeInspectable from 'mobx-devtools-mst' | ||
import { addDisposer, onSnapshot } from 'mobx-state-tree' | ||
import { useMemo } from 'react' | ||
|
||
import RootStore from '@store' | ||
|
||
function loadSnapshot(storageKey, storage) { | ||
const data = storage.getItem(storageKey) | ||
return JSON.parse(data) || {} | ||
} | ||
|
||
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) | ||
} | ||
|
||
let store = null | ||
|
||
function initStore({ authClient, cachePanoptesData, client, storageKey }) { | ||
if (store === null) { | ||
let initialState = {} | ||
|
||
if (cachePanoptesData) { | ||
initialState = loadSnapshot(storageKey, window.sessionStorage) | ||
} | ||
|
||
store = RootStore.create(initialState, { | ||
authClient, | ||
client | ||
}) | ||
|
||
if (cachePanoptesData) { | ||
persist(storageKey, store, window.sessionStorage) | ||
} | ||
makeInspectable(store) | ||
} | ||
return store | ||
} | ||
|
||
export function cleanStore() { | ||
store = null | ||
} | ||
|
||
export default function useHydratedStore({ authClient, client }, cachePanoptesData = false, storageKey) { | ||
// 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 }) | ||
// Write store snapshots to session storage. | ||
return useSessionStorage(cachePanoptesData, classifierStore, storageKey) | ||
const _store = useMemo(() => initStore({ authClient, cachePanoptesData, client, storageKey }), [authClient, cachePanoptesData, client, storageKey]) | ||
return _store | ||
} |
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.
This file was deleted.
Oops, something went wrong.