Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/stats/stats-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ import { describe, it, expect } from 'vitest';
import { StatData, StatDataModel, StatsProvider } from './stats-provider';

describe('StatsProvider', () => {
it('emits each child statistic once to each listener', () => {
const provider = new StatsProvider('test', 'test');
const firstListenerStats: StatData[] = [];
const secondListenerStats: StatData[] = [];

provider.addStatListener({
onStatUpdated: (stat: StatData) => firstListenerStats.push(stat),
});
provider.addStatListener({
onStatUpdated: (stat: StatData) => secondListenerStats.push(stat),
});

provider.setStats({
tick: 1,
type: 'stats',
stats: [{
name: 'parent',
should_aggregate: false,
children: [{
name: 'child',
should_aggregate: false,
values: [1],
}],
}],
});

expect(firstListenerStats.map((stat: StatData) => stat.name)).toEqual(['parent', 'child']);
expect(secondListenerStats.map((stat: StatData) => stat.name)).toEqual(['parent', 'child']);
});

it('aggregateData successful', async () => {
const childDataA: StatDataModel = {
name: 'childA',
Expand Down
10 changes: 6 additions & 4 deletions src/stats/stats-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ export class StatsProvider {

if (aggregateChildData !== undefined) {
listener.onStatUpdated?.(aggregateChildData);
} else if (stat.children) {
stat.children.forEach((child: StatDataModel) => {
this._fireStatUpdated(child, tick, statData);
});
}
});

if (aggregateChildData === undefined && stat.children) {
stat.children.forEach((child: StatDataModel) => {
this._fireStatUpdated(child, tick, statData);
});
}
}
}
Loading