Skip to content
This repository was archived by the owner on Jul 30, 2018. It is now read-only.
This repository was archived by the owner on Jul 30, 2018. It is now read-only.

@inject fails silently when Injector is missing from the registry #751

@devpaul

Description

@devpaul

Bug

When the inject decorator is missing an injector in the registry it skips calling getProperties

if (injector) {
	const registeredInjectors = registeredInjectorsMap.get(this) || [];
	if (registeredInjectors.length === 0) {
		registeredInjectorsMap.set(this, registeredInjectors);
	}
	if (registeredInjectors.indexOf(injector) === -1) {
		injector.on('invalidate', () => {
			this.emit({ type: 'invalidated', target: this });
		});
		registeredInjectors.push(injector);
	}
	return getProperties(injector.get(), properties);
}

Essentially this allows injection to fail silently and doesn't allow getProperties to react. Instead, it would be useful to pass undefined to getProperties and allow it to handle an unregistered injector. It could then

  • throw an error
  • provide default property values
  • choose to ignore it as we do today

This is especially important for Container because it transforms all of a Widget's properties into optional. So while I may have a Widget where all of it's properties are required, when @Inject skips a call to getProperties I don't have an opportunity to deal with the missing properties and my Widget will expect for those properties to exist due to its properties typings.

Code

interface MyWidgetProperties {
    descriptor: { name: string };
}

class MyWidget extends WidgetBase<MyWidgetProperties> {
    protected render() {
        return v('div', {
            'data-name': this.properties.descriptor.name;
        });
    }
}

const MyWidgetContainer = Container(MyWidget, 'state', {
    getProperties(state) {
        descriptor = state ? state.descriptor : { name: '' };
        return {
            descriptor
        };
    }
});

Expected behavior:

The mapper would provide a default descriptor to the contained widget

Actual behavior:

render() throws an error when 'state' does not exist in the registry.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions