Skip to content

fix: move type registry into component state #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
useJsonViewerStore,
useJsonViewerStoreApi
} from './stores/JsonViewerStore'
import { registerType } from './stores/typeRegistry'
import {
createTypeRegistryStore, predefined,
TypeRegistryProvider, useTypeRegistryStore
} from './stores/typeRegistry'
import { darkColorspace, lightColorspace } from './theme/base16'
import type { JsonViewerProps } from './type'
import { applyValue, createDataType, isCycleReference } from './utils'
Expand All @@ -23,7 +26,7 @@ export { applyValue, createDataType, isCycleReference }
/**
* @internal
*/
function useSetIfNotUndefinedEffect <Key extends keyof JsonViewerProps> (
function useSetIfNotUndefinedEffect<Key extends keyof JsonViewerProps> (
key: Key,
value: JsonViewerProps[Key] | undefined
) {
Expand Down Expand Up @@ -66,6 +69,16 @@ const JsonViewerInner: React.FC<JsonViewerProps> = (props) => {
})
}
}, [api, props.theme])
const onceRef = useRef(true)
const registerType = useTypeRegistryStore(store => store.registerType)
// DO NOT try to dynamic add value types, that is costly. Trust me.
if (onceRef.current) {
predefined(registerType)
props.valueTypes?.forEach(type => {
registerType(type)
})
onceRef.current = false
}

const value = useJsonViewerStore(store => store.value)
const setHover = useJsonViewerStore(store => store.setHover)
Expand Down Expand Up @@ -122,24 +135,18 @@ export const JsonViewer = function JsonViewer<Value> (props: JsonViewerProps<Val
}
})
}, [themeType])
const onceRef = useRef(true)
// DO NOT try to dynamic add value types, that is costly. Trust me.
if (onceRef.current) {
props.valueTypes?.forEach(type => {
registerType(type)
})
onceRef.current = false
}
const mixedProps = { ...props, theme: themeType }
return (
<ThemeProvider theme={theme}>
<JsonViewerProvider createStore={() => {
// This function only runs once, so we don't need a memo for this.
// Refs: https://github.com/pmndrs/zustand/blob/77d14b17bc33a6f10f072802fac56aa78510710e/src/context.ts#L36-L38
return createJsonViewerStore(props)
}}>
<JsonViewerInner {...mixedProps}/>
</JsonViewerProvider>
<TypeRegistryProvider createStore={createTypeRegistryStore}>
<JsonViewerProvider createStore={() => {
// This function only runs once, so we don't need a memo for this.
// Refs: https://github.com/pmndrs/zustand/blob/77d14b17bc33a6f10f072802fac56aa78510710e/src/context.ts#L36-L38
return createJsonViewerStore(props)
}}>
<JsonViewerInner {...mixedProps}/>
</JsonViewerProvider>
</TypeRegistryProvider>
</ThemeProvider>
)
}
Expand Down
Loading