Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Only trigger a state change if the state is definitely different #1569

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
12 changes: 10 additions & 2 deletions src/card-controller/conditions-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CurrentUser } from '@dermotduffy/custom-card-helpers';
import { HassEntities } from 'home-assistant-js-websocket';
import { isEqual } from 'lodash-es';
import merge from 'lodash-es/merge';
import { ZodSchema } from 'zod';
import {
Expand All @@ -10,10 +11,10 @@ import {
} from '../config/management';
import {
FrigateCardCondition,
RawFrigateCardConfig,
ViewDisplayMode,
frigateConditionalSchema,
Overrides,
RawFrigateCardConfig,
ViewDisplayMode,
} from '../config/types';
import { desparsifyArrays } from '../utils/basic';
import { CardConditionAPI, KeysState } from './types';
Expand Down Expand Up @@ -227,6 +228,13 @@ export class ConditionsManager {
}

public setState(state: Partial<ConditionState>): void {
// Performance: Compare the new state with the existing state and only
// trigger a change if the new state is different. Only the new keys are
// compared, since some of the values (e.g. 'state') will be large.
if (Object.keys(state).every((key) => isEqual(state[key], this._state[key]))) {
return;
}

this._state = {
...this._state,
...state,
Expand Down
54 changes: 54 additions & 0 deletions tests/card-controller/conditions-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,60 @@ describe('ConditionsManager', () => {
expect(manager.getState()).toEqual(state);
});

describe('should set state', () => {
it('should set and be able to get it again', () => {
const state = {
fullscreen: true,
};

const manager = new ConditionsManager(createCardAPI());

manager.setState(state);
expect(manager.getState()).toEqual(state);
});

it('should set but only trigger when necessary', () => {
const state_1 = {
fullscreen: true,
state: {
'binary_sensor.foo': createStateEntity(),
},
};

const listener = vi.fn();
const manager = new ConditionsManager(createCardAPI(), listener);

manager.setState(state_1);
expect(listener).toBeCalledTimes(1);

manager.setState(state_1);
expect(listener).toBeCalledTimes(1);

manager.setState({ fullscreen: true });
expect(listener).toBeCalledTimes(1);

manager.setState({
state: {
'binary_sensor.foo': createStateEntity(),
},
});
expect(listener).toBeCalledTimes(1);

manager.setState({ fullscreen: false });
expect(listener).toBeCalledTimes(2);

manager.setState({ fullscreen: false });
expect(listener).toBeCalledTimes(2);

manager.setState({
state: {
'binary_sensor.foo': createStateEntity({ state: 'off' }),
},
});
expect(listener).toBeCalledTimes(3);
});
});

describe('should handle hasHAStateConditions', () => {
beforeEach(() => {
vi.spyOn(window, 'matchMedia').mockReturnValueOnce({
Expand Down
Loading