Skip to content

Commit

Permalink
chore(stats-widget): Convert module to .ts (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Oct 6, 2021
1 parent 1a54535 commit a115366
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 79 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
node_modules/
modules/stats/src/lib/stats.ts
File renamed without changes.
4 changes: 0 additions & 4 deletions modules/stats-widget/src/index.d.ts

This file was deleted.

21 changes: 0 additions & 21 deletions modules/stats-widget/src/stats-widget.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Built on https://github.com/Erkaman/regl-stats-widget (MIT license)
// widget styling constants.
import {formatMemory, formatTime} from './format-utils';
import {Stats} from '@probe.gl/stats';
import {Stats, Stat} from '@probe.gl/stats';

const DEFAULT_CSS = {
css: {
Expand Down Expand Up @@ -30,12 +30,34 @@ export const DEFAULT_FORMATTERS = {
memory: stat => `${stat.name}: ${formatMemory(stat.count)}`
};

export default class StatsWidget {
constructor(stats, options = {}) {
this.stats = stats;
this.title = options.title;
export type StatWidgetProps = {
title?: string;
framesPerUpdate?: number;
css?: object;
container?: HTMLElement;
formatters?: {[type: string]: string | ((stat: Stat) => string)};
resetOnUpdate?: {[statName: string]: boolean};
};

this._framesPerUpdate = Math.round(Math.max(options.framesPerUpdate || 1, 1));
export default class StatsWidget {
stats: Stats;
title: string;
_framesPerUpdate: number;
_formatters;
_resetOnUpdate = {};
_counter = 0;
_css;
_headerCSS;
_itemCSS;
_container = null;
_header = null;
_items = {};

constructor(stats: Stats, options?: StatWidgetProps) {
stats = stats;
this.title = options?.title || 'Stats';

this._framesPerUpdate = Math.round(Math.max(options?.framesPerUpdate || 1, 1));
this._formatters = DEFAULT_FORMATTERS;
this._resetOnUpdate = {};

Expand All @@ -45,7 +67,7 @@ export default class StatsWidget {
this._initializeUpdateConfigs(options);

// UI
this._css = Object.assign({}, DEFAULT_CSS.css, options.css);
this._css = Object.assign({}, DEFAULT_CSS.css, options?.css);
this._headerCSS = Object.assign({}, DEFAULT_CSS.headerCSS, this._css.header);
this._itemCSS = Object.assign({}, DEFAULT_CSS.itemCSS, this._css.item);

Expand All @@ -56,12 +78,12 @@ export default class StatsWidget {
this._header = null;
this._items = {};

this._createDOM(options.container);
this._createDOM(options?.container);
this._createDOMHeader();
this._createDOMStats();
}

setStats(stats) {
setStats(stats: Stats): void {
this.stats = stats;
// @ts-ignore
this._createDOMStats(this._container);
Expand All @@ -70,9 +92,10 @@ export default class StatsWidget {
this.update();
}

update() {
update(): void {
// compatible with the old API
// TODO should call stats.size
// @ts-expect-error
const stats = this.stats && this.stats.stats;
if (!stats || Object.keys(stats).length === 0) {
return;
Expand All @@ -85,7 +108,7 @@ export default class StatsWidget {
this._update();
}

_update() {
_update(): void {
// make sure that we clear the old text before drawing new text.
this.stats.forEach(stat => {
this._createDOMItem(stat.name);
Expand All @@ -97,8 +120,8 @@ export default class StatsWidget {
});
}

_initializeFormatters(options) {
if (options.formatters) {
_initializeFormatters(options?: StatWidgetProps): void {
if (options?.formatters) {
for (const name in options.formatters) {
let formatter = options.formatters[name];

Expand All @@ -111,15 +134,15 @@ export default class StatsWidget {
}
}

_initializeUpdateConfigs(options) {
if (options.resetOnUpdate) {
_initializeUpdateConfigs(options?: StatWidgetProps): void {
if (options?.resetOnUpdate) {
for (const name in options.resetOnUpdate) {
this._resetOnUpdate[name] = options.resetOnUpdate[name];
}
}
}

_createDOM(container) {
_createDOM(container: HTMLElement) {
if (typeof document === 'undefined' || !document) {
return;
}
Expand All @@ -136,7 +159,7 @@ export default class StatsWidget {
}
}

_createDOMHeader() {
_createDOMHeader(): void {
// header
if (!this._header) {
this._header = document.createElement('div');
Expand All @@ -149,21 +172,21 @@ export default class StatsWidget {
this._setHeaderContent();
}

_setHeaderContent(title) {
_setHeaderContent(title?: string): void {
if (this._header) {
this._header.innerText = title || this.title || (this.stats && this.stats.id) || '';
}
}

_createDOMStats() {
_createDOMStats(): void {
if (this.stats instanceof Stats) {
this.stats.forEach(stat => {
this._createDOMItem(stat.name);
});
}
}

_createDOMItem(statName) {
_createDOMItem(statName: string): void {
if (!this._container) {
return;
}
Expand All @@ -179,7 +202,7 @@ export default class StatsWidget {
this._container.appendChild(this._items[statName]);
}

_getLines(stat) {
_getLines(stat: Stat): string[] {
const formatter =
this._formatters[stat.name] || this._formatters[stat.type] || DEFAULT_FORMATTERS.count;
return formatter(this.stats.get(stat.name)).split('\n');
Expand Down
25 changes: 0 additions & 25 deletions modules/stats/src/lib/stats.d.ts

This file was deleted.

19 changes: 11 additions & 8 deletions modules/stats/src/lib/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class Stats {
readonly id: string;
private stats = {};

constructor(options: {id: string; stats?: Stat[] | {name: string; type?: string}[]}) {
constructor(options: {id: string; stats?: Stats | Stat[] | {name: string; type?: string}[]}) {
this.id = options.id;
this.stats = {};

Expand Down Expand Up @@ -38,12 +38,15 @@ export default class Stats {
}
}

getTable(): Record<string, {
time: number;
count: number;
average: number;
hz: number;
}> {
getTable(): Record<
string,
{
time: number;
count: number;
average: number;
hz: number;
}
> {
const table = {};
this.forEach(stat => {
table[stat.name] = {
Expand All @@ -57,7 +60,7 @@ export default class Stats {
return table;
}

_initializeStats(stats = []): void {
_initializeStats(stats: Stats | Stat[] | {name: string; type?: string}[] = []): void {
stats.forEach(stat => this._getOrCreate(stat));
}

Expand Down

0 comments on commit a115366

Please sign in to comment.