Skip to content

Commit d978cac

Browse files
pedrolamasCopilot
andauthored
refactor: enforce printer state immutability (#1874)
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 85e105f commit d978cac

4 files changed

Lines changed: 16 additions & 27 deletions

File tree

src/store/printer/actions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,19 @@ export const actions = {
168168
*/
169169

170170
/**
171-
* Stores the printers object list.
171+
* Builds the subscription map from the printer object list and subscribes to updates.
172172
*/
173-
async onPrinterObjectsList ({ commit }, payload: Moonraker.KlippyApis.ObjectsListResponse) {
173+
async onPrinterObjectsList (_, payload: Moonraker.KlippyApis.ObjectsListResponse) {
174174
// Given our object list, subscribe to any data we'd want constant updates for
175175
// and prepopulate our store.
176176
const subscriptions: Record<string, null> = {}
177177

178178
for (const key of payload.objects) {
179-
if (!key.includes('menu')) {
180-
subscriptions[key] = null
179+
if (key === 'menu' || key.startsWith('menu ')) {
180+
continue
181181
}
182182

183-
commit('setPrinterObjectList', key.replace(' ', '.'))
183+
subscriptions[key] = null
184184
}
185185

186186
SocketActions.printerObjectsSubscribe(subscriptions)

src/store/printer/mutations.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,29 @@ export const mutations = {
2828
},
2929

3030
setPrinterInfo (state, payload: Moonraker.KlippyApis.Info) {
31-
state.info = payload
32-
},
33-
34-
setPrinterObjectList (state, payload) {
35-
if (!state.printer.objects.includes(payload)) {
36-
state.printer.objects.push(payload)
37-
}
31+
state.info = Object.freeze(payload)
3832
},
3933

4034
setClearEndStops (state) {
41-
if (state.printer.query_endstops == null) {
35+
const queryEndstops = state.printer.query_endstops
36+
37+
if (queryEndstops == null) {
4238
return
4339
}
4440

45-
state.printer.query_endstops = {
46-
...state.printer.query_endstops,
47-
last_query: {}
48-
}
41+
queryEndstops.last_query = Object.freeze({})
4942
},
5043

5144
setClearScrewsTiltAdjust (state) {
52-
if (state.printer.screws_tilt_adjust == null) {
45+
const screwsTiltAdjust = state.printer.screws_tilt_adjust
46+
47+
if (screwsTiltAdjust == null) {
5348
return
5449
}
5550

56-
state.printer.screws_tilt_adjust = {
57-
...state.printer.screws_tilt_adjust,
58-
error: false,
59-
max_deviation: null,
60-
results: {}
61-
}
51+
screwsTiltAdjust.error = false
52+
screwsTiltAdjust.max_deviation = null
53+
screwsTiltAdjust.results = Object.freeze({})
6254
},
6355

6456
setSocketNotify<T extends keyof Klipper.PrinterState> (state: PrinterState, payload: { key: T, payload: Klipper.PrinterState[T] }) {

src/store/printer/state.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const defaultState = (): PrinterState => {
1919
config: {},
2020
settings: {}
2121
},
22-
objects: [],
2322
idle_timeout: {
2423
state: 'Idle',
2524
printing_time: 0

src/typings/klipper.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ declare namespace Klipper {
1414
{
1515
// These keys are always available
1616

17-
objects: string[];
18-
1917
configfile: ConfigFileState;
2018

2119
gcode_move: GcodeMoveState;

0 commit comments

Comments
 (0)