metal.js icon indicating copy to clipboard operation
metal.js copied to clipboard

Automate component registration for JSX components

Open eduardolundgren opened this issue 6 years ago • 6 comments

It is verbose and error-prone forcing the developer to call ComponentRegistry.register. Ideally, this should be automated by a transpilation step or a runtime operation.

With this change, the component definition will change from this

import {JSXComponent} from 'metal-jsx';
import {ComponentRegistry} from 'metal-component';

class Button extends JSXComponent {
}
ComponentRegistry.register(Button);

to this

import {JSXComponent} from 'metal-jsx';

class Button extends JSXComponent {
}

eduardolundgren avatar Oct 19 '17 14:10 eduardolundgren

I leave my 2¢ here too: component registration is leaky (there’s a single component registry) and should happen exclusively when the developer understands the implications.

yuchi avatar Oct 19 '17 14:10 yuchi

See errors such as:

customElements.define('x-x', class X extends HTMLElement {});
customElements.define('x-x', class X extends HTMLElement {});

Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': this name has already been used with this registry

yuchi avatar Oct 19 '17 14:10 yuchi

Hint: it will happen when multiple versions of the same component are loaded in the page.

yuchi avatar Oct 19 '17 14:10 yuchi

@yuchi throw a runtime error if the component is already registered is a good suggestion to make the component registry less "leaky".

eduardolundgren avatar Oct 19 '17 14:10 eduardolundgren

Actually that's what happens already. AFAIK it's in the spec.

yuchi avatar Oct 19 '17 14:10 yuchi

@yuchi I meant, throwing an error on Metal.js component registry, similar to what you described on custom elements. Wasn't that your suggestion?

eduardolundgren avatar Oct 19 '17 15:10 eduardolundgren