Skip to content

Commit

Permalink
fix(di): resolve bem#551 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
artems committed Apr 1, 2020
1 parent 4723e3d commit 0bdacc9
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/di/di.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
ComponentType,
createContext,
useContext,
useRef,
createElement,
} from 'react'

Expand All @@ -21,26 +22,32 @@ export function withRegistry() {

return function WithRegistry<P>(Component: ComponentType<P>) {
const RegistryResolver: FC<P> = (props) => {
const providedRegistriesRef = useRef<RegistryContext>(null)

return (
<RegistryConsumer>
{(contextRegistries) => {
const providedRegistries = { ...contextRegistries }

for (let i = 0; i < registries.length; i++) {
const registry = registries[i]
const overrides = contextRegistries[registry.id]
// eslint-disable-next-line no-nested-ternary
providedRegistries[registry.id] = registry.overridable
? overrides
? registry.merge(overrides)
: registry
: registry && overrides
? overrides.merge(registry)
: registry
if (providedRegistriesRef.current === null) {
const providedRegistries = { ...contextRegistries }

for (let i = 0; i < registries.length; i++) {
const registry = registries[i]
const overrides = contextRegistries[registry.id]
// eslint-disable-next-line no-nested-ternary
providedRegistries[registry.id] = registry.overridable
? overrides
? registry.merge(overrides)
: registry
: registry && overrides
? overrides.merge(registry)
: registry
}

providedRegistriesRef.current = providedRegistries;
}

return (
<RegistryProvider value={providedRegistries}>
<RegistryProvider value={providedRegistriesRef.current as RegistryContext}>
{/* Use createElement instead of jsx to avoid __assign from tslib. */}
{createElement(Component, props)}
</RegistryProvider>
Expand Down

0 comments on commit 0bdacc9

Please sign in to comment.