[FieldFormats] fix register
on start contract (#106828)
This commit is contained in:
parent
ecea2e7982
commit
deefe1ac83
|
@ -8,7 +8,7 @@
|
|||
<b>Signature:</b>
|
||||
|
||||
```typescript
|
||||
export declare type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
|
||||
export declare type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
|
||||
deserialize: FormatFactory;
|
||||
};
|
||||
```
|
||||
|
|
|
@ -76,6 +76,27 @@ describe('FieldFormatsRegistry', () => {
|
|||
expect(registeredFieldFormatters.get(StringFormat.id)).toBe(StringFormat);
|
||||
expect(registeredFieldFormatters.get(PercentFormat.id)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('should throw if registering a formatter with existing id ', () => {
|
||||
fieldFormatsRegistry.register([BoolFormat]);
|
||||
|
||||
expect(() => fieldFormatsRegistry.register([BoolFormat])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Failed to register field format with id \\"boolean\\" as it already has been registered"`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('has', () => {
|
||||
test('should provide an public "has" method', () => {
|
||||
expect(fieldFormatsRegistry.has).toBeDefined();
|
||||
expect(typeof fieldFormatsRegistry.has).toBe('function');
|
||||
});
|
||||
|
||||
test('should check if field format registered', () => {
|
||||
fieldFormatsRegistry.register([StringFormat]);
|
||||
expect(fieldFormatsRegistry.has(StringFormat.id)).toBe(true);
|
||||
expect(fieldFormatsRegistry.has(BoolFormat.id)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getType', () => {
|
||||
|
|
|
@ -253,7 +253,21 @@ export class FieldFormatsRegistry {
|
|||
}
|
||||
|
||||
register(fieldFormats: FieldFormatInstanceType[]) {
|
||||
fieldFormats.forEach((fieldFormat) => this.fieldFormats.set(fieldFormat.id, fieldFormat));
|
||||
fieldFormats.forEach((fieldFormat) => {
|
||||
if (this.fieldFormats.has(fieldFormat.id))
|
||||
throw new Error(
|
||||
`Failed to register field format with id "${fieldFormat.id}" as it already has been registered`
|
||||
);
|
||||
this.fieldFormats.set(fieldFormat.id, fieldFormat);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if field format with id already registered
|
||||
* @param id
|
||||
*/
|
||||
has(id: string): boolean {
|
||||
return this.fieldFormats.has(id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,6 +25,7 @@ export const fieldFormatsMock: IFieldFormatsRegistry = {
|
|||
getTypeNameByEsTypes: jest.fn(),
|
||||
init: jest.fn(),
|
||||
register: jest.fn(),
|
||||
has: jest.fn(),
|
||||
parseDefaultTypeMap: jest.fn(),
|
||||
deserialize: jest.fn().mockImplementation(() => {
|
||||
const DefaultFieldFormat = FieldFormat.from(identity);
|
||||
|
|
|
@ -94,4 +94,4 @@ export interface IFieldFormatMetaParams {
|
|||
};
|
||||
}
|
||||
|
||||
export type FieldFormatsStartCommon = Omit<FieldFormatsRegistry, 'init' & 'register'>;
|
||||
export type FieldFormatsStartCommon = Omit<FieldFormatsRegistry, 'init' | 'register'>;
|
||||
|
|
|
@ -49,9 +49,9 @@ export class FieldFormatsService {
|
|||
}
|
||||
|
||||
/** @public */
|
||||
export type FieldFormatsSetup = Pick<FieldFormatsRegistry, 'register'>;
|
||||
export type FieldFormatsSetup = Pick<FieldFormatsRegistry, 'register' | 'has'>;
|
||||
|
||||
/** @public */
|
||||
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
|
||||
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
|
||||
deserialize: FormatFactory;
|
||||
};
|
||||
|
|
|
@ -1077,7 +1077,7 @@ export type FieldFormatsContentType = 'html' | 'text';
|
|||
export type FieldFormatsGetConfigFn = GetConfigFn;
|
||||
|
||||
// @public (undocumented)
|
||||
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
|
||||
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
|
||||
deserialize: FormatFactory;
|
||||
};
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ export class IndexPatternDatasource {
|
|||
|
||||
setup(
|
||||
core: CoreSetup<IndexPatternDatasourceStartPlugins>,
|
||||
{ expressions, editorFrame, charts }: IndexPatternDatasourceSetupPlugins
|
||||
{ expressions, editorFrame, charts, data: dataSetup }: IndexPatternDatasourceSetupPlugins
|
||||
) {
|
||||
editorFrame.registerDatasource(async () => {
|
||||
const {
|
||||
|
@ -48,8 +48,11 @@ export class IndexPatternDatasource {
|
|||
} = await import('../async_services');
|
||||
return core
|
||||
.getStartServices()
|
||||
.then(([coreStart, { data, indexPatternFieldEditor, uiActions }]) => {
|
||||
data.fieldFormats.register([getSuffixFormatter(data.fieldFormats.deserialize)]);
|
||||
.then(([coreStart, { indexPatternFieldEditor, uiActions, data }]) => {
|
||||
const suffixFormatter = getSuffixFormatter(data.fieldFormats.deserialize);
|
||||
if (!dataSetup.fieldFormats.has(suffixFormatter.id)) {
|
||||
dataSetup.fieldFormats.register([suffixFormatter]);
|
||||
}
|
||||
expressions.registerFunction(timeScale);
|
||||
expressions.registerFunction(counterRate);
|
||||
expressions.registerFunction(renameColumns);
|
||||
|
|
Loading…
Reference in a new issue