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

Simplify Widget Registries #686

Merged
merged 15 commits into from
Sep 20, 2017
Merged

Simplify Widget Registries #686

merged 15 commits into from
Sep 20, 2017

Conversation

agubler
Copy link
Member

@agubler agubler commented Sep 19, 2017

Type: feature

The following has been addressed in the PR:

  • There is a related issue
  • All code matches the style guide
  • Unit or Functional tests are included in the PR

Description:

The more that registries have been explored, the more apparent it has become that the main use cases will be to support lazy loading widgets and for injecting values into the widget tree.

These changes remove registry as a supported property for all widgets, so that a registry is only supported for a projector (or app level). This registry will be passed as the baseRegistry for all widgets within the projector tree.

As well as the baseRegistry, each widget will have access to define entries into a local registry created per instance. These locally defined entries will take precedence over entries with the same label defined in the baseRegistry.

The registries property (the RegistryHandler) on WidgetBase has been re-named to registry, as consumers will work with the handler as is they would a single registry.

In advanced cases, it is possible to invert the default precedence on a case by case basis, by passing a boolean with the label to the get (get / getInjector) functions, so that, if there is a item in the base registry it will be returned ahead of an item defined in the local registry.

this.registry.get('widget', true);
this.registry.getInjector('widget', true);

This could be useful to enable a particular default to be overridden for a widget, for example a loading widget. Locally this would be registered as this.registry.define('my-widget-loading', DefaultLoading); but used with the extra boolean flag this.registry.get('my-widget-loading', true); so that consumers could add a custom widget to their application registry to override the DefaultLoading widget.

private _get(label: RegistryLabel, globalPrecedence: boolean, getFunctionName: 'getInjector' | 'get', labelMap: Map<Registry, RegistryLabel[]>): any {
const registries = globalPrecedence ? [ this._baseRegistry, this._registry ] : [ this._registry, this._baseRegistry ];
for (let i = 0; i < registries.length; i++) {
const registry: any = registries[i];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No other decent type than any?

Copy link
Member Author

@agubler agubler Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so we can use an index using the string function name that is passed.

If the getFunctionName (which typing I realise is a bad name), is restricted to just 'get' or 'getInjector' then using registry[getFunctionName]('') works but not if is it's typed as 'get' | 'getInjector', any ideas?

private _get(label: RegistryLabel, globalPrecedence: boolean, getFunctionName: 'getInjector' | 'get', labelMap: Map<Registry, RegistryLabel[]>): any {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... Recent versions of TypeScript allow you to access properties by method name just fine, if the variable is a string literal, unless I am missing something here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fine is I typed the argument is just 'get' or just 'getInjector' but not when I typed it as 'getInjector' | 'get'...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, the playground example types it as 'getInjector' | 'get'... wonder what is wrong.

if (event.action === 'loaded') {
else if (registeredLabels.indexOf(label) === -1) {
const handle = registry.on(label, (event: RegistryEventObject) => {
if (event.action === 'loaded' && (<any> this)[getFunctionName](label) === event.item) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this as any) and why the any cast?

@agubler agubler merged commit f8f81be into dojo:master Sep 20, 2017
@dylans dylans added this to the 2017.09 milestone Sep 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants