Skip to content

Commit

Permalink
simplify changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jul 6, 2020
1 parent 6428455 commit 1789afc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,4 @@ describe('FieldFormatsRegistry', () => {
expect(params).toHaveProperty('parsedUrl');
});
});

describe('setCustomParams', () => {
test('should provide custom setting to formatters: timezone', () => {
fieldFormatsRegistry = new FieldFormatsRegistry();
fieldFormatsRegistry.init(
getConfig,
{
parsedUrl: {
origin: '',
pathname: '',
basePath: '',
},
},
[]
);
fieldFormatsRegistry.setCustomParams({ timezone: 'America/New_York' });
});
});
});
26 changes: 9 additions & 17 deletions src/plugins/data/common/field_formats/field_formats_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class FieldFormatsRegistry {
protected defaultMap: Record<string, FieldFormatConfig> = {};
protected metaParamsOptions: Record<string, any> = {};
protected getConfig?: FieldFormatsGetConfigFn;
protected customParams: Record<string, any> = {};
// overriden on the public contract
public deserialize: (mapping: SerializedFieldFormat) => IFieldFormat = () => {
return new (FieldFormat.from(identity))();
Expand All @@ -58,13 +57,6 @@ export class FieldFormatsRegistry {
this.metaParamsOptions = metaParamsOptions;
}

/*
* Allow use-case specific params that are reflected in getInstance / getDefaultInstancePlain
*/
setCustomParams(params: Record<string, any>) {
this.customParams = params;
}

/**
* Get the id of the default type for this field type
* using the format:defaultTypeMap config map
Expand Down Expand Up @@ -165,11 +157,7 @@ export class FieldFormatsRegistry {
* @return {FieldFormat}
*/
getInstance = memoize(
(formatId: FieldFormatId, instanceParams: Record<string, any> = {}): FieldFormat => {
const params = {
...instanceParams,
...this.customParams,
};
(formatId: FieldFormatId, params: Record<string, any> = {}): FieldFormat => {
const ConcreteFieldFormat = this.getType(formatId);

if (!ConcreteFieldFormat) {
Expand All @@ -192,14 +180,18 @@ export class FieldFormatsRegistry {
* @param {ES_FIELD_TYPES[]} esTypes
* @return {FieldFormat}
*/
getDefaultInstancePlain(fieldType: KBN_FIELD_TYPES, esTypes?: ES_FIELD_TYPES[]): FieldFormat {
getDefaultInstancePlain(
fieldType: KBN_FIELD_TYPES,
esTypes?: ES_FIELD_TYPES[],
params: Record<string, any> = {}
): FieldFormat {
const conf = this.getDefaultConfig(fieldType, esTypes);
const defaultParams = {
const instanceParams = {
...conf.params,
...this.customParams,
...params,
};

return this.getInstance(conf.id, defaultParams);
return this.getInstance(conf.id, instanceParams);
}
/**
* Returns a cache key built by the given variables for caching in memoized
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
* under the License.
*/

import { coreMock } from '../../../../core/server/mocks';
import { FIELD_FORMAT_IDS } from '../../common';
import { DateFormat } from './converters/date_server';
import { FieldFormatsService } from './field_formats_service';
import { DateFormat } from './converters/date_server';
import { coreMock } from '../../../../core/server/mocks';

describe('FieldFormatService', () => {
test('DateFormat is server version', async () => {
Expand All @@ -33,22 +32,3 @@ describe('FieldFormatService', () => {
expect(DateFormatFromRegsitry).toEqual(DateFormat);
});
});

describe('DateFormat with custom timezone', () => {
test('should provide custom setting to formatters: timezone', async () => {
const service = new FieldFormatsService();
const fieldFormatsService = await service.start();
const uiSettings = coreMock.createStart().uiSettings.asScopedToClient({} as any);
const fieldFormatsRegistry = await fieldFormatsService.fieldFormatServiceFactory(uiSettings);

fieldFormatsRegistry.setCustomParams({ timezone: 'America/Phoenix' }); // set the timezone into the registry

const fieldFormats = fieldFormatsRegistry.getByFieldType(FIELD_FORMAT_IDS.DATE as any);
const DateFormatInstance = fieldFormats.find((F) => F.id === 'date');
expect(DateFormatInstance).toBeDefined();
if (DateFormatInstance) {
const formatter = new DateFormatInstance();
// how to call the formatter?
}
});
});

0 comments on commit 1789afc

Please sign in to comment.