Skip to content

Commit fe96985

Browse files
committed
perf: avoid redundant internal state snapshots
1 parent c474d1c commit fe96985

14 files changed

Lines changed: 60 additions & 18 deletions

File tree

src/helpers/mustache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ function parseContext(key: string) {
3232

3333
class CustomContext extends Context {
3434
readonly #nodeContext: NodeContext;
35-
readonly #entities: HassEntities;
35+
readonly #entities: Readonly<HassEntities>;
3636

3737
constructor(
3838
view: any,
3939
parentContext: CustomContext | undefined,
4040
nodeContext: NodeContext,
41-
entities: HassEntities,
41+
entities: Readonly<HassEntities>,
4242
) {
4343
super(view, parentContext);
4444
this.#nodeContext = nodeContext;
@@ -93,7 +93,7 @@ export function renderTemplate(
9393
str: string,
9494
message: NodeMessage,
9595
nodeContext: NodeContext,
96-
entities: HassEntities,
96+
entities: Readonly<HassEntities>,
9797
altTags = false,
9898
): string {
9999
if (
@@ -115,7 +115,7 @@ export function renderTemplate(
115115
export function generateRenderTemplate(
116116
message: NodeMessage,
117117
context: NodeContext,
118-
states: HassEntities,
118+
states: Readonly<HassEntities>,
119119
) {
120120
return (template: string, altTags = false) =>
121121
renderTemplate(template, message, context, states, altTags);

src/homeAssistant/Websocket.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,15 @@ export default class Websocket {
716716
return cloneDeep(this.states);
717717
}
718718

719+
/**
720+
* Return the current state map for internal read-only consumers.
721+
* Callers must not modify the returned object. Use getStates() when an
722+
* independent snapshot is required.
723+
*/
724+
getStatesReadOnly(): Readonly<HassEntities> {
725+
return this.states;
726+
}
727+
719728
getState(entityId: string): HassEntity | undefined {
720729
return cloneDeep(this.states[entityId]);
721730
}

src/nodes/action/ActionController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class ActionController extends InputOutputController<
3737
throw new NoConnectionError();
3838
}
3939

40-
const states = this.homeAssistant.websocket.getStates();
40+
const states = this.homeAssistant.websocket.getStatesReadOnly();
4141

4242
const render = generateRenderTemplate(
4343
message,
@@ -245,7 +245,7 @@ export default class ActionController extends InputOutputController<
245245
const render = generateRenderTemplate(
246246
message,
247247
this.node.context(),
248-
this.homeAssistant.websocket.getStates(),
248+
this.homeAssistant.websocket.getStatesReadOnly(),
249249
);
250250

251251
const map: Record<string, string> = {

src/nodes/api/ApiController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class ApiController extends InputOutputController<
2626
const renderTemplate = generateRenderTemplate(
2727
message,
2828
this.node.context(),
29-
this.homeAssistant.websocket.getStates(),
29+
this.homeAssistant.websocket.getStatesReadOnly(),
3030
);
3131

3232
let data;

src/nodes/current-state/CurrentStateController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class CurrentStateController extends InputOutputController<
4747
parsedMessage.entityId.value,
4848
message,
4949
this.node.context(),
50-
this.#homeAssistant.websocket.getStates(),
50+
this.#homeAssistant.websocket.getStatesReadOnly(),
5151
);
5252

5353
const entity = this.#homeAssistant.websocket.getState(

src/nodes/fire-event/FireEventController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class FireEventController extends InputOutputController<
2828
parsedMessage.event.value,
2929
message,
3030
this.node.context(),
31-
this.homeAssistant.websocket.getStates(),
31+
this.homeAssistant.websocket.getStatesReadOnly(),
3232
);
3333

3434
let eventData: unknown;
@@ -49,7 +49,7 @@ export default class FireEventController extends InputOutputController<
4949
: parsedMessage.data.value,
5050
message,
5151
this.node.context(),
52-
this.homeAssistant.websocket.getStates(),
52+
this.homeAssistant.websocket.getStatesReadOnly(),
5353
);
5454
try {
5555
eventData = JSON.parse(dataString);

src/nodes/get-entities/GetEntitiesController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export default class GetEntitiesController extends SendSplitController {
150150
): Promise<HassEntity[]> {
151151
const currentTime = Date.now();
152152
const filteredEntities: HassEntity[] = [];
153-
const states = this.#homeAssistant.websocket.getStates();
153+
const states = this.#homeAssistant.websocket.getStatesReadOnly();
154154
const sortedConditions = sortConditions(conditions);
155155

156156
const stateKeys = Object.keys(states);

src/nodes/get-history/GetHistoryController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ export default class GetHistoryController extends SendSplitController {
3939
parsedMessage.entityId.value,
4040
message,
4141
this.node.context(),
42-
this.homeAssistant.websocket.getStates(),
42+
this.homeAssistant.websocket.getStatesReadOnly(),
4343
);
4444
}
4545
if (parsedMessage.entityIdType.value === EntityFilterType.Regex) {
4646
const entities = Object.keys(
47-
this.homeAssistant.websocket.getStates(),
47+
this.homeAssistant.websocket.getStatesReadOnly(),
4848
);
4949
const regex = new RegExp(entityId);
5050
entityId = entities

src/nodes/tag/issue-check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function issueCheck(config: TagNodeProperties): Issue[] {
1212
if (!ha) {
1313
return issues;
1414
}
15-
const states = ha.websocket.getStates();
15+
const states = ha.websocket.getStatesReadOnly();
1616
const tagStates = Object.values(states).filter((state) =>
1717
state.entity_id.startsWith('tag.'),
1818
);

src/nodes/trigger-state/TriggerStateController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export default class TriggerStateController extends ExposeAsController {
288288
output.messageValue,
289289
eventMessage.event as NodeMessage,
290290
this.node.context(),
291-
this.homeAssistant.websocket.getStates(),
291+
this.homeAssistant.websocket.getStatesReadOnly(),
292292
);
293293
}
294294

0 commit comments

Comments
 (0)