-
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`. Move Classifier hooks to @hooks Load hooks from `/src/hooks`, aliased to `@hooks`. Test the useHydratedStore hook Consolidate store hooks Replace the store hooks with a single `useHydratedStore` hook. Use synchronous storage.
- Loading branch information
1 parent
3fd91bd
commit 05b87b0
Showing
16 changed files
with
174 additions
and
125 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
26 changes: 0 additions & 26 deletions
26
packages/lib-classifier/src/components/Classifier/hooks/Readme.md
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
packages/lib-classifier/src/components/Classifier/hooks/useHydratedStore.js
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
packages/lib-classifier/src/components/Classifier/hooks/useStore.js
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Classifier hooks | ||
|
||
## useHydratedStore | ||
|
||
Create a `mobx-state-tree` store from an optional stored [snapshot](https://mobx-state-tree.js.org/concepts/snapshots). Adds an `onSnapshot` handler to keep the stored snapshot updated when the store changes. | ||
|
||
Returns the new store when hydration is complete. Snapshots are stored in session storage, so that they don't persist across tabs or windows. | ||
|
||
```js | ||
const classifierStore = useHydratedStore({ authClient, client }, cachePanoptesData = false, storageKey) | ||
``` | ||
|
||
## useWorkflowSnapshot | ||
|
||
A wrapper for [`useSWR`](https://swr.vercel.app/), which fetches a workflow by ID, using the default SWR options. The workflow will refresh on visibility change (eg. waking from sleep), or when the classifier receives focus. | ||
|
||
```js | ||
const workflowSnapshot = useWorkflowSnapshot(workflowID) | ||
``` |
1 change: 0 additions & 1 deletion
1
.../src/components/Classifier/hooks/index.js → packages/lib-classifier/src/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,2 @@ | ||
export { default as useHydratedStore } from './useHydratedStore' | ||
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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) { | ||
const _store = useMemo(() => initStore({ authClient, cachePanoptesData, client, storageKey }), [authClient, cachePanoptesData, client, storageKey]) | ||
return _store | ||
} |
98 changes: 98 additions & 0 deletions
98
packages/lib-classifier/src/hooks/useHydratedStore.spec.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,98 @@ | ||
import { applySnapshot, getSnapshot } from 'mobx-state-tree' | ||
import React from 'react' | ||
import { renderHook } from '@testing-library/react-hooks/pure' | ||
|
||
import mockStore from '@test/mockStore' | ||
import RootStore from '@store/RootStore' | ||
import { cleanStore } from './useHydratedStore' | ||
import { useHydratedStore } from '.' | ||
|
||
describe('Hooks > useHydratedStore', function () { | ||
describe('without an existing store', function () { | ||
let store | ||
|
||
beforeEach(function () { | ||
const { authClient, client } = mockStore() | ||
const { result } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
store = result.current | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should create a new store', function () { | ||
const mockStore = getSnapshot(RootStore.create({})) | ||
const snapshot = getSnapshot(store) | ||
expect(snapshot).to.deep.equal(mockStore) | ||
}) | ||
}) | ||
|
||
describe('with an existing store', function () { | ||
let store | ||
let newStore | ||
|
||
beforeEach(function () { | ||
const { authClient, client } = mockStore() | ||
const { result: firstRun } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
store = firstRun.current | ||
const { result: secondRun } = renderHook(() => useHydratedStore({ authClient, client }, false, 'test-key')) | ||
newStore = secondRun.current | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should use the existing store', function () { | ||
expect(newStore).to.deep.equal(store) | ||
}) | ||
}) | ||
|
||
describe('with session storage enabled', function () { | ||
let store | ||
|
||
beforeEach(function () { | ||
const expectedStore = mockStore() | ||
const { authClient, client } = expectedStore | ||
const { result } = renderHook(() => useHydratedStore({ authClient, client }, true, 'test-key')) | ||
store = result.current | ||
const mockSnapshot = getSnapshot(expectedStore) | ||
applySnapshot(store.projects, mockSnapshot.projects) | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should save snapshots in session storage', function () { | ||
const mockSnapshot = window.sessionStorage.getItem('test-key') | ||
const snapshot = getSnapshot(store) | ||
expect(JSON.stringify(snapshot)).to.equal(mockSnapshot) | ||
}) | ||
}) | ||
|
||
describe('with a saved snapshot', function () { | ||
let store | ||
let mockSnapshot | ||
|
||
beforeEach(function () { | ||
const expectedStore = mockStore() | ||
mockSnapshot = getSnapshot(expectedStore) | ||
window.sessionStorage.setItem('test-key', JSON.stringify(mockSnapshot)) | ||
|
||
const { authClient, client } = expectedStore | ||
const { result } = renderHook(() => useHydratedStore({ authClient, client }, true, 'test-key')) | ||
store = result.current | ||
}) | ||
|
||
afterEach(function () { | ||
cleanStore() | ||
}) | ||
|
||
it('should load the snapshot into the store', function () { | ||
const snapshot = getSnapshot(store) | ||
expect(snapshot.projects).to.deep.equal(mockSnapshot.projects) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
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