Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 18x 18x 18x 18x 18x 18x 18x 18x 2x 2x 7x 3x 3x 1x 1x 3x 4x 1x 7x 5x 5x 8x 3x 1x 2x 1x 9x 9x 5x 2x 3x 4x 9x 9x 4x 4x 3x 1x 8x 8x 1x 1x 2x 2x 1x 9x 5x 3x 5x 4x | import { Config } from '../config';
import { InstanceNotFoundError } from '../error/instance-not-found';
import { Http } from '../http';
import { JsonCacheService } from '../json-cache';
import { IDictionary } from '../utils';
/**
* Class
*/
export class Metadata {
private static enabledConsoleValue = true;
private static instances: IDictionary = {};
private static instancesCount = 0;
public static getEnabledConsole() {
return Metadata.enabledConsoleValue;
}
public static setEnabledConsole(enabled: boolean) {
Metadata.enabledConsoleValue = enabled;
}
public static async get(path: string, local?: boolean, JSONCache?: boolean | string): Promise<IDictionary> {
if (JSONCache) {
let metadata = await Metadata.getMetadataFromCache(path);
if (!metadata) {
metadata = await Metadata.getMetadataFromRequest(path, local);
JsonCacheService.saveJSONCache([{ path }], metadata);
}
return metadata as IDictionary;
}
return Metadata.getMetadataFromRequest(path, local);
}
public static async post(path: string, params?: any): Promise<IDictionary> {
return (
await Http.post(`${Config.metadataEndPoint}${path}`, params, {
baseURL: '',
headers: {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '0',
},
})
).data;
}
public static getMetadataUrl(path: string, local = false): string {
return local ? `/metadata/${path}.json` : `${Config.metadataEndPoint}${path}`;
}
public static clearInstance(instanceName: string, instanceId: number) {
const instance = Metadata.instances[instanceName];
if (Array.isArray(instance)) {
Metadata.instances[instanceName] = instance.filter((item: any) => item.componentId !== instanceId);
if (Metadata.instances[instanceName].length === 1) {
[Metadata.instances[instanceName]] = Metadata.instances[instanceName];
}
} else if (Metadata.instances[instanceName].componentId === instanceId) {
delete Metadata.instances[instanceName];
}
}
public static addInstance(instance: { name: string }, allowDuplicate = false) {
Metadata.instancesCount += 1;
if (Metadata.instances[instance.name]) {
if (Array.isArray(Metadata.instances[instance.name])) {
Metadata.instances[instance.name].push(instance);
} else {
Metadata.instances[instance.name] = [Metadata.instances[instance.name], instance];
}
} else {
Metadata.instances[instance.name] = instance;
}
if (!allowDuplicate) Metadata.searchDuplicateInstanceName(instance.name);
return Metadata.instancesCount;
}
public static getInstance<T>(name: string): T {
const instance = Array.isArray(Metadata.instances[name]) ? Metadata.instances[name][0] : Metadata.instances[name];
if (instance) {
return instance;
}
throw new InstanceNotFoundError(name);
}
public static getInstances(name: string): any[] {
const instance = Metadata.instances[name];
return Array.isArray(instance) ? instance : [instance];
}
public static updateInstance<T extends Record<string, any>>(name: string, props: T) {
const instance = Metadata.getInstance<T>(name);
for (const key in props) {
if (Object.prototype.hasOwnProperty.call(instance, key) && props[key] !== undefined) {
instance[key] = props[key];
}
}
return instance;
}
private static searchDuplicateInstanceName(name: string) {
if (Array.isArray(Metadata.instances[name]) && Metadata.enabledConsoleValue === true) {
// eslint-disable-next-line no-console
console.warn(`Duplicate component name detected: ${name}`);
}
}
private static async getMetadataFromCache(path: string): Promise<IDictionary | boolean> {
return JsonCacheService.getJSONCache(path);
}
private static async getMetadataFromRequest(path: string, local?: boolean): Promise<IDictionary> {
const getMetadata = await (
await Http.get(`${Metadata.getMetadataUrl(path, local)}?v=${new Date().getTime()}`, {
baseURL: Config.baseUrl,
headers: {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '0',
},
})
).data;
return getMetadata;
}
}
|