UNPKG

@adaptabletools/adaptable

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

160 lines (159 loc) 5.98 kB
import { debug } from '@infinite-table/infinite-react'; import { DevToolsTracks } from './DevToolsTracks'; const AVAILABLE_COLORS = [ 'primary', 'primary-light', 'primary-dark', 'secondary', 'secondary-light', 'secondary-dark', 'tertiary', 'tertiary-light', 'tertiary-dark', // 'error', ]; const colorMap = new Map(); function getColor(identifier) { if (colorMap.has(identifier)) { return colorMap.get(identifier); } const color = AVAILABLE_COLORS[colorMap.size % AVAILABLE_COLORS.length]; colorMap.set(identifier, color); return color; } export class DevToolsMarker { static create(adaptableId) { return new DevToolsMarker(adaptableId); } constructor(adaptableId) { this.adaptableId = adaptableId; this.markerDetails = { label: '', track: '', trackGroup: undefined, color: undefined, details: [], tooltip: undefined, }; this.stopped = false; this.start = (startDetails) => { if (this.markerDetails.startTs) { return this; } const start = performance.now(); this.markerDetails.details = startDetails?.details ?? []; this.markerDetails.startTs = start; return this; }; this.end = (markerDetails = {}) => { if (this.stopped) { return this; } this.stopped = true; const start = markerDetails.startTs ?? this.markerDetails.startTs; const end = markerDetails.endTs ?? this.markerDetails.endTs ?? performance.now(); let color = markerDetails.color || this.markerDetails.color || ''; const trackGroup = markerDetails.trackGroup || this.markerDetails.trackGroup || `AdapTable (${this.adaptableId})`; const details = [...(this.markerDetails.details || []), ...(markerDetails.details || [])]; const tooltip = markerDetails.tooltip || this.markerDetails.tooltip; const label = markerDetails.label || this.markerDetails.label || 'Unknown'; const track = markerDetails.track || this.markerDetails.track || 'Unknown'; if (!color) { const identifier = `${trackGroup}:${track}:${label}`; color = getColor(identifier); } performance.measure(label, { start, end, detail: { devtools: { dataType: 'track-entry', trackGroup, track, color: color || 'primary', properties: details.length > 0 ? details.map((detail) => [detail.name, detail.value]) : undefined, tooltipText: tooltip, }, }, }); return this; }; this.adaptableId = adaptableId; } get startTimestamp() { return this.markerDetails.startTs; } get track() { const self = this; return Object.keys(DevToolsTracks).reduce((acc, track) => { const trackObj = { start: self.start, end: (markerDetails) => { return self.end({ ...markerDetails, track: DevToolsTracks[track].track, label: markerDetails.label, }); }, get label() { const currentTrack = DevToolsTracks[track]; return Object.keys(currentTrack.labels).reduce((labelAcc, label) => { const labelObj = { start: self.start, end: (markerDetails) => { return self.end({ ...markerDetails, track: currentTrack.track, label: currentTrack.labels[label], }); }, }; Object.defineProperty(labelAcc, label, { get: () => { self.markerDetails.label = currentTrack.labels[label]; return labelObj; }, }); return labelAcc; }, {}); }, }; Object.defineProperty(acc, track, { get: () => { const currentTrack = DevToolsTracks[track]; self.markerDetails.track = currentTrack.track; return trackObj; }, }); return acc; }, {}); } } export function areAdaptableProfileTracksEnabled(adaptableId) { const trakcsSupported = typeof performance !== 'undefined' && typeof performance.now === 'function' && typeof performance.measure === 'function'; if (!trakcsSupported) { return false; } if (typeof localStorage !== 'undefined' && debug.isChannelEnabled(adaptableId ? `Adaptable:${adaptableId}:perf` : 'Adaptable:*:perf', typeof localStorage !== 'undefined' ? localStorage.debug || '' : '')) { return true; } if (typeof localStorage !== 'undefined' && localStorage.adaptableProfileTracks == 'true') { return true; } return false; } export function getMarker(adaptableId) { const enabled = areAdaptableProfileTracksEnabled(adaptableId); const marker = DevToolsMarker.create(adaptableId); if (!enabled) { marker.start = () => marker; marker.end = () => marker; } return marker; }