Re: [w3c/webcomponents] Scoped Custom Element Registries (#716)

I think a common pattern that might emerge is sharing a registry across a whole package rather than per-module. Since in npm generally a package can only resolve a dependency to a single version, it'll be relatively safe to have a package-wide registry that handles the element subclassing automatically and is tolerant of re-registrations:

registry.js
```js
export class AutoScopingCustomElementRegistry extends CustomElementRegistry {
  constructor() {
    this.bases = new Map();
  }

  define(name, constructor, options) {
    if (this.bases.has(tagname)) {
      const base = defined.get(tagname);
      if (base !== constructor) {
        throw new Error('Tried to redefine a tagname with a different class');
      }
      return; // already defined
    }
    super.define(name, class extends constructor {}, options);
  }
}
export const registry = new AutoScopingCustomElementRegistry();
```
Now one module can define the element without making a trivial subclass:

container-a.js
```js
import {registry} from './registry.js';
import {ChildElement) from './child-element.js';
registry.define('child-element', ChildElement);
```

And another module too, but it's safe and shares the definition:

container-b.js
```js
import {registry} from './registry.js';
import {ChildElement) from './child-element.js';
registry.define('child-element', ChildElement);
```

It's possible this is useful enough that it should make it into the proposal.

-- 
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/716#issuecomment-368049564

Received on Friday, 23 February 2018 15:55:53 UTC