Skip to content

Commit 1633c47

Browse files
committed
perf: avoid reactivity on immutable objects
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 7caaf16 commit 1633c47

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

src/store/charts/mutations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const mutations = {
3737
* Inits the chart store from moonraker.
3838
*/
3939
setChartStore (state, payload: ChartData[]) {
40-
state.chart = payload
40+
state.chart = payload.map<ChartData>(Object.freeze)
4141
state.ready = true
4242
},
4343

@@ -49,7 +49,7 @@ export const mutations = {
4949
if (!state[payload.type]) {
5050
Vue.set(state, payload.type, [])
5151
}
52-
state[payload.type].push(payload.data)
52+
state[payload.type].push(Object.freeze(payload.data))
5353
const firstInRange = state[payload.type].findIndex((entry: ChartData) => (Date.now() - entry.date.valueOf()) / 1000 < payload.retention)
5454
if (firstInRange > 0) state[payload.type].splice(0, firstInRange)
5555
},

src/store/printer/mutations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const mutations = {
7575
// always evaluate true since we never
7676
// get an update unless something has changed.
7777
if (stateObject[key] !== payloadValue[key]) {
78-
Vue.set(stateObject, key, payloadValue[key])
78+
Vue.set(stateObject, key, Object.freeze(payloadValue[key]))
7979
}
8080
}
8181
}

src/store/server/actions.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,22 @@ export const actions = {
145145
commit('setMoonrakerStats', payload)
146146

147147
// Add a chart entry
148-
if (
149-
payload.moonraker_stats &&
150-
'cpu_usage' in payload.moonraker_stats &&
151-
!Array.isArray(payload.moonraker_stats)
152-
) {
153-
const d = payload.moonraker_stats
154-
if (d.cpu_usage <= 100) {
155-
commit('charts/setChartEntry', {
156-
type: 'moonraker',
157-
retention: 600,
158-
data: {
159-
date: new Date(d.time * 1000),
160-
load: d.cpu_usage.toFixed(2)
161-
}
162-
}, { root: true })
148+
if (payload.moonraker_stats) {
149+
const stats = Array.isArray(payload.moonraker_stats)
150+
? payload.moonraker_stats
151+
: [payload.moonraker_stats]
152+
153+
for (const d of stats) {
154+
if (d.cpu_usage <= 100) {
155+
commit('charts/setChartEntry', {
156+
type: 'moonraker',
157+
retention: 600,
158+
data: {
159+
date: new Date(d.time * 1000),
160+
load: d.cpu_usage.toFixed(2)
161+
}
162+
}, { root: true })
163+
}
163164
}
164165
}
165166
},

src/store/server/mutations.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,21 @@ export const mutations = {
6060
* On initial init, we get the server (moonraker) process stats and any throttled state flags.
6161
*/
6262
setMoonrakerStats (state, payload) {
63-
if (payload.cpu_temp) Vue.set(state, 'cpu_temp', payload.cpu_temp)
64-
if (payload.throttled_state) state.throttled_state = { ...state.throttled_state, ...payload.throttled_state }
65-
if (
66-
payload.moonraker_stats &&
67-
Array.isArray(payload.moonraker_stats)
68-
) {
69-
// Update with array.
70-
Vue.set(state, 'moonraker_stats', payload.moonraker_stats)
71-
} else {
72-
// Append to array.
73-
if (state.moonraker_stats) {
74-
state.moonraker_stats.push(payload.moonraker_stats)
63+
if (payload.cpu_temp) {
64+
Vue.set(state, 'cpu_temp', payload.cpu_temp)
65+
}
66+
67+
if (payload.throttled_state) {
68+
state.throttled_state = { ...state.throttled_state, ...payload.throttled_state }
69+
}
70+
71+
if (payload.moonraker_stats) {
72+
if (Array.isArray(payload.moonraker_stats)) {
73+
// Update with array.
74+
Vue.set(state, 'moonraker_stats', payload.moonraker_stats.map(Object.freeze))
75+
} else {
76+
// Append to array.
77+
state.moonraker_stats.push(Object.freeze(payload.moonraker_stats))
7578
while (state.moonraker_stats.length > 30) {
7679
state.moonraker_stats.splice(0, 1)
7780
}

0 commit comments

Comments
 (0)