Skip to content

Commit

Permalink
Use embeddable v2 - fix regression of #39126 (#41221) (#41447)
Browse files Browse the repository at this point in the history
* Use embeddable v2 - fix regression of #39126

* fix PR comment
  • Loading branch information
alexwizp authored Jul 18, 2019
1 parent 233b242 commit 4668807
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
*/
export { DisabledLabEmbeddable } from './disabled_lab_embeddable';
export { VisualizeEmbeddable, VisualizeInput } from './visualize_embeddable';
export { VisualizeEmbeddableFactory } from './visualize_embeddable_factory';
export { VISUALIZE_EMBEDDABLE_TYPE } from './constants';
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ export class VisualizeEmbeddableFactory extends EmbeddableFactory<
VisualizeEmbeddable | DisabledLabEmbeddable,
VisualizationAttributes
> {
private visTypes?: VisTypesRegistry;
public readonly type = VISUALIZE_EMBEDDABLE_TYPE;

constructor() {
static async createVisualizeEmbeddableFactory(): Promise<VisualizeEmbeddableFactory> {
const $injector = await chrome.dangerouslyGetActiveInjector();
const Private = $injector.get<IPrivate>('Private');
const visTypes = Private(VisTypesRegistryProvider);

return new VisualizeEmbeddableFactory(visTypes);
}

constructor(private visTypes: VisTypesRegistry) {
super({
savedObjectMetaData: {
name: i18n.translate('kbn.visualize.savedObjectName', { defaultMessage: 'Visualization' }),
Expand Down Expand Up @@ -117,7 +124,6 @@ export class VisualizeEmbeddableFactory extends EmbeddableFactory<
},
},
});
this.initializeVisTypes();
}

public isEditable() {
Expand All @@ -130,12 +136,6 @@ export class VisualizeEmbeddableFactory extends EmbeddableFactory<
});
}

public async initializeVisTypes() {
const $injector = await chrome.dangerouslyGetActiveInjector();
const Private = $injector.get<IPrivate>('Private');
this.visTypes = Private(VisTypesRegistryProvider);
}

public async createFromSavedObject(
savedObjectId: string,
input: Partial<VisualizeInput> & { id: string },
Expand Down Expand Up @@ -188,4 +188,6 @@ export class VisualizeEmbeddableFactory extends EmbeddableFactory<
}
}

embeddableFactories.set(VISUALIZE_EMBEDDABLE_TYPE, new VisualizeEmbeddableFactory());
VisualizeEmbeddableFactory.createVisualizeEmbeddableFactory().then(embeddableFactory => {
embeddableFactories.set(VISUALIZE_EMBEDDABLE_TYPE, embeddableFactory);
});
38 changes: 29 additions & 9 deletions src/legacy/ui/public/registry/_registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import _ from 'lodash';
import { IndexedArray } from '../indexed_array';

const notPropsOptNames = IndexedArray.OPT_NAMES.concat('constructor', 'invokeProviders');

/**
Expand Down Expand Up @@ -78,6 +79,10 @@ export function uiRegistry(spec) {
const props = _.omit(spec, notPropsOptNames);
const providers = [];

let isInstantiated = false;
let getInvokedProviders;
let modules;

/**
* This is the Private module that will be instantiated by
*
Expand All @@ -87,17 +92,21 @@ export function uiRegistry(spec) {
* defines how things will be indexed.
*/
const registry = function (Private, $injector) {
// call the registered providers to get their values
iaOpts.initialSet = invokeProviders
? $injector.invoke(invokeProviders, undefined, { providers })
: providers.map(Private);
getInvokedProviders = function (newProviders) {
let set = invokeProviders
? $injector.invoke(invokeProviders, undefined, { providers: newProviders })
: newProviders.map(Private);

if (filter && _.isFunction(filter)) {
iaOpts.initialSet = iaOpts.initialSet.filter(item => filter(item));
}
if (filter && _.isFunction(filter)) {
set = set.filter(item => filter(item));
}

return set;
};

iaOpts.initialSet = getInvokedProviders(providers);

// index all of the modules
let modules = new IndexedArray(iaOpts);
modules = new IndexedArray(iaOpts);

// mixin other props
_.assign(modules, props);
Expand All @@ -107,13 +116,24 @@ export function uiRegistry(spec) {
modules = $injector.invoke(constructor, modules) || modules;
}

isInstantiated = true;

return modules;
};

registry.displayName = '[registry ' + props.name + ']';

registry.register = function (privateModule) {
providers.push(privateModule);

if (isInstantiated) {
const [provider] = getInvokedProviders([privateModule]);

if (provider) {
modules.push(provider);
}
}

return registry;
};

Expand Down

0 comments on commit 4668807

Please sign in to comment.