Skip to content

DI: Typed registries

Compare
Choose a tag to compare
@awinogradov awinogradov released this 10 Jan 12:21
· 331 commits to master since this release

Features

Typed registries for components

From now you don't need to pass interface as generic with get method. See the new short API:

const compositorRegistry = new Registry({ id: 'Compositor' });
const Element: React.SFC<ICommonProps> = () => <span>content</span>;

interface ICompositorRegistry {
    Element: React.ComponentType<ICommonProps>;
}

compositorRegistry.set('Element', Element);

const CompositorPresenter: React.SFC<ICommonProps> = () => (
    <ComponentRegistryConsumer id="Compositor">
        {({ Element }: ICompositorRegistry) => <Element/>}
    </ComponentRegistryConsumer>
);

const Compositor = withRegistry(compositorRegistry)(CompositorPresenter);

Element is typed in render prop!

Partially registries merging

From now you don't need to override all entities in component registry. Replace only what you need to replace:

const compositorRegistry = new Registry({ id: 'Compositor', inverted: true });
const Element1: React.SFC<ICommonProps> = () => <span>content</span>;
const Element2: React.SFC<ICommonProps> = () => <span>extra</span>;

const overridedCompositorRegistry = new Registry({ id: 'Compositor' });
const OverridedElement: React.SFC<ICommonProps> = () => <span>overrided</span>;

interface ICompositorRegistry {
    Element1: React.ComponentType<ICommonProps>;
    Element2: React.ComponentType<ICommonProps>;
}

compositorRegistry.set('Element1', Element1);
compositorRegistry.set('Element2', Element2);
overridedCompositorRegistry.set('Element1', OverridedElement);

const CompositorPresenter: React.SFC<ICommonProps> = () => (
    <ComponentRegistryConsumer id="Compositor">
        {({ Element1, Element2 }: ICompositorRegistry) => (
            <>
                <Element1/>
                <Element2/>
            </>
         )}
    </ComponentRegistryConsumer>
);

const Compositor = withRegistry(compositorRegistry)(CompositorPresenter);
const OverridedCompositor = withRegistry(overridedCompositorRegistry)(Compositor);

Check more cases in specs!

PR: #384