diff --git a/src/stats/stats-provider.test.ts b/src/stats/stats-provider.test.ts index 82a5be3..5f2820c 100644 --- a/src/stats/stats-provider.test.ts +++ b/src/stats/stats-provider.test.ts @@ -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', diff --git a/src/stats/stats-provider.ts b/src/stats/stats-provider.ts index ab0a6fc..670569f 100644 --- a/src/stats/stats-provider.ts +++ b/src/stats/stats-provider.ts @@ -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); + }); + } } }