[w3c/webcomponents] CustomElementRegistry.prototype.define<bulk>() (#892)

Refs:
- [Idea](https://github.com/justinfagnani/webcomponents/pull/1#discussion_r418763448)
- [Original issue](https://github.com/justinfagnani/scoped-custom-elements/issues/2)

In the light of discussing [Scoped Custom Element Registries](https://github.com/w3c/webcomponents/issues/759), some folks, including @caridy, @justinfagnani, and I, believe it might be interesting to design a method for multiple elements definition. 

This would allow us to define a batch of components within a single method call.

```js
customElements.define<bulk>({ // naming TBD
  'x-foo': Foo,
  'x-bar': Bar,
});

// equivalent to

customElements.define('x-foo', Foo);
customElements.define('x-bar', Bar);
```

## Existing Feedback

From @justinfagnani:

> This would be a welcome feature even for the global registry. We've had users hit problems when trying to define multiple elements that interact (ie. children firing events to register with parents). They end up being sensitive to the registration order. There's also a performance concern with multiple tree walks to register multiple elements. Bulk registration would help with both.

## Compatibility

Unfortunately, we cannot simply modify the existing `CustomElementRegistry#define()` without breaking changes. Taking this, we might need a new method, using a new name, without replacing `.define()`, which remains designed for single definitions. 

Today, `define` casts the first argument to string and this also applies to object values:

```js
customElements.define({
    toString() {
      return 'my-element';
    }
  },
  MyElement
);

// similar to

customElements.define('my-element', MyElement);
```

## Naming Bikeshed

_Naming is hard, and this idea is not an exception._

For now I can think of `.makeDefinitions`, `.defineBulk`, `.defineMultiple`, `.defineMany`, but I'd love better suggestions out of not being a big fan of any of the current ideas.

## Alternative

We could just modify `.define` to not cast the first argument to string if the method is called with only a single parameter.



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/892

Received on Wednesday, 12 August 2020 22:54:26 UTC