perf: Deduplicate concurrent Home Assistant registry list fetches (#2650)

This commit is contained in:
Dermot Duffy
2026-08-02 21:35:48 -07:00
committed by GitHub
parent 5d247d4074
commit 4a7c1b62b0
6 changed files with 265 additions and 26 deletions
+33
View File
@@ -45,6 +45,23 @@ describe('DeviceRegistryManager', () => {
expect(hass.callWS).toHaveBeenCalledTimes(1);
});
it('should fetch once for callers that arrive while a fetch is running', async () => {
const testDevice = createRegistryDevice({ id: 'test' });
const hass = createHASS();
const manager = new DeviceRegistryManager(new DeviceCache());
vi.mocked(hass.callWS).mockResolvedValueOnce([testDevice]);
expect(
await Promise.all([
manager.getDevice(hass, 'test'),
manager.getDevice(hass, 'test'),
]),
).toEqual([testDevice, testDevice]);
expect(hass.callWS).toHaveBeenCalledTimes(1);
});
it('should return null when fetch fails', async () => {
const hass = createHASS();
vi.mocked(hass.callWS).mockRejectedValueOnce(new Error('Fetch error'));
@@ -57,6 +74,22 @@ describe('DeviceRegistryManager', () => {
expect.anything(),
);
});
it('should fetch again after a failure', async () => {
const testDevice = createRegistryDevice({ id: 'test' });
const hass = createHASS();
vi.mocked(hass.callWS)
.mockRejectedValueOnce(new Error('Fetch error'))
.mockResolvedValueOnce([testDevice]);
const manager = new DeviceRegistryManager(new DeviceCache());
expect(await manager.getDevice(hass, 'test')).toBeNull();
expect(await manager.getDevice(hass, 'test')).toEqual(testDevice);
expect(hass.callWS).toHaveBeenCalledTimes(2);
});
});
it('getMatchingDevices', async () => {