Skip to content

Commit a656677

Browse files
committed
refactor: typing improvements
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 6a253dc commit a656677

File tree

6 files changed

+79
-86
lines changed

6 files changed

+79
-86
lines changed

src/components/widgets/job-queue/JobQueueMultiplyJobDialog.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<script lang="ts">
2626
import { Component, Vue, VModel, Prop } from 'vue-property-decorator'
2727
import type { QueuedJob } from '@/store/jobQueue/types'
28-
import { isArray } from 'lodash-es'
2928
3029
@Component({})
3130
export default class JobQueueMultiplyJobDialog extends Vue {
@@ -38,7 +37,7 @@ export default class JobQueueMultiplyJobDialog extends Vue {
3837
readonly job!: QueuedJob | QueuedJob[]
3938
4039
get jobCount () {
41-
return isArray(this.job)
40+
return Array.isArray(this.job)
4241
? this.job.length
4342
: 1
4443
}

src/store/chart_helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { RootState } from './types'
33
import type { ChartData } from './charts/types'
44
import type { KlipperPrinterMcuState, KlipperPrinterState, KlipperPrinterSystemStatsState } from './printer/types'
55

6-
export const handleMcuStatsChange = (payload: KlipperPrinterState, state: RootState, commit: Commit) => {
6+
export const handleMcuStatsChange = (payload: Partial<KlipperPrinterState>, state: RootState, commit: Commit) => {
77
for (const key in payload) {
88
if (key.startsWith('mcu')) {
99
// Combine existing with the update.
@@ -60,7 +60,7 @@ export const handleMcuStatsChange = (payload: KlipperPrinterState, state: RootSt
6060
}
6161
}
6262

63-
export const handleSystemStatsChange = (payload: KlipperPrinterState, state: RootState, commit: Commit) => {
63+
export const handleSystemStatsChange = (payload: Partial<KlipperPrinterState>, state: RootState, commit: Commit) => {
6464
if (payload.system_stats != null) {
6565
// Combine existing with the update.
6666
const stats: KlipperPrinterSystemStatsState = {

src/store/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import getFilePaths from '@/util/get-file-paths'
88

99
const isTmc = (item: string): item is TmcKey => /^tmc\d{4} /.test(item)
1010

11-
export const handleTrinamicDriversChange = (payload: KlipperPrinterState, state: RootState, dispatch: Dispatch, getters: any) => {
11+
export const handleTrinamicDriversChange = (payload: Partial<KlipperPrinterState>, state: RootState, dispatch: Dispatch, getters: any) => {
1212
for (const item in payload) {
1313
if (
1414
isTmc(item) &&
@@ -37,7 +37,7 @@ export const handleTrinamicDriversChange = (payload: KlipperPrinterState, state:
3737
}
3838
}
3939

40-
export const handlePrintStateChange = (payload: KlipperPrinterState, state: RootState, dispatch: Dispatch) => {
40+
export const handlePrintStateChange = (payload: Partial<KlipperPrinterState>, state: RootState, dispatch: Dispatch) => {
4141
// For every notify - if print_stats.state changes from standby -> printing,
4242
// then record an entry in our print history.
4343
// If the state changes from printing -> complete, then record the finish time.
@@ -66,7 +66,7 @@ export const handlePrintStateChange = (payload: KlipperPrinterState, state: Root
6666
}
6767
}
6868

69-
export const handleCurrentFileChange = (payload: KlipperPrinterState, state: RootState) => {
69+
export const handleCurrentFileChange = (payload: Partial<KlipperPrinterState>, state: RootState) => {
7070
if (
7171
payload.print_stats?.filename &&
7272
payload.print_stats.filename !== state.printer.printer.print_stats?.filename

src/store/printer/actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const actions = {
200200
*/
201201

202202
/** Automated notify events via socket */
203-
async onNotifyStatusUpdate ({ rootState, commit, getters, dispatch }, payload: KlipperPrinterState) {
203+
async onNotifyStatusUpdate ({ rootState, commit, getters, dispatch }, payload: Partial<KlipperPrinterState>) {
204204
// TODO: We potentially get many updates here.
205205
// Consider caching the updates and sending them every <interval>.
206206
// We don't want to miss an update - but also don't need all of them

src/store/printer/mutations.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Vue from 'vue'
22
import type { MutationTree } from 'vuex'
3-
import type { PrinterState } from './types'
3+
import type { KlipperPrinterState, PrinterState } from './types'
44
import { defaultState } from './state'
5-
import { get } from 'lodash-es'
65

76
export const mutations = {
87
/**
@@ -62,28 +61,22 @@ export const mutations = {
6261
}
6362
},
6463

65-
setSocketNotify (state, payload) {
66-
if (typeof payload.payload === 'object') {
67-
const o = get(state.printer, payload.key)
68-
if (o === undefined) {
69-
// Object is not set yet, so create it.
70-
Vue.set(state.printer, payload.key, payload.payload)
71-
} else {
72-
Object.keys(payload.payload).forEach((p) => {
73-
// Leaving the if here, although it should
74-
// always evaluate true since we never
75-
// get an update unless something has changed.
76-
if (
77-
o[p] !== payload.payload[p]
78-
) {
79-
Vue.set(state.printer[payload.key], p, payload.payload[p])
80-
}
81-
})
82-
}
64+
setSocketNotify<T extends keyof KlipperPrinterState> (state: PrinterState, payload: { key: T, payload: KlipperPrinterState[T] }) {
65+
const { key: payloadKey, payload: payloadValue } = payload
66+
67+
const stateObject = state.printer[payloadKey]
68+
69+
if (stateObject == null) {
70+
// Object is not set yet, so create it.
71+
Vue.set(state.printer, payloadKey, payloadValue)
8372
} else {
84-
// I don't think this'd get called.
85-
if (get(state.printer, payload.key) !== payload.payload) {
86-
Vue.set(state.printer, payload.key, payload.payload)
73+
for (const key in payloadValue) {
74+
// Leaving the if here, although it should
75+
// always evaluate true since we never
76+
// get an update unless something has changed.
77+
if (stateObject[key] !== payloadValue[key]) {
78+
Vue.set(stateObject, key, payloadValue[key])
79+
}
8780
}
8881
}
8982
}

src/store/printer/types.ts

+56-55
Original file line numberDiff line numberDiff line change
@@ -603,110 +603,111 @@ export interface KlipperPrinterBeaconState {
603603
export interface KlipperPrinterConfig extends Record<string, Record<string, string | undefined> | undefined> {
604604
}
605605

606-
type KlipperPrinterSettingsBaseType = {
607-
[key in ExtruderKey]?: KlipperPrinterExtruderSettings
608-
}
606+
type KlipperPrinterSettingsBaseType =
607+
Partial<{
608+
[key in ExtruderKey]: KlipperPrinterExtruderSettings;
609+
} & {
610+
mcu: KlipperPrinterMcuSettings;
609611

610-
export interface KlipperPrinterSettings extends KlipperPrinterSettingsBaseType {
611-
[key: string]: Record<string, any> | undefined;
612+
[key: `mcu ${Lowercase<string>}`]: KlipperPrinterMcuSettings;
612613

613-
mcu?: KlipperPrinterMcuSettings;
614+
[key: Lowercase<TmcKey>]: KlipperPrinterTmcSettings;
614615

615-
[key: `mcu ${Lowercase<string>}`]: KlipperPrinterMcuSettings | undefined;
616+
fan: KlipperPrinterFanSettings;
616617

617-
[key: Lowercase<TmcKey>]: KlipperPrinterTmcSettings;
618+
[key: `heater_fan ${Lowercase<string>}`]: KlipperPrinterHeaterFanSettings;
618619

619-
fan?: KlipperPrinterFanSettings;
620+
[key: `controller_fan ${Lowercase<string>}`]: KlipperPrinterControllerFanSettings;
620621

621-
[key: `heater_fan ${Lowercase<string>}`]: KlipperPrinterHeaterFanSettings | undefined;
622+
[key: `gcode_button ${string}`]: KlipperPrinterGcodeButtonSettings;
622623

623-
[key: `controller_fan ${Lowercase<string>}`]: KlipperPrinterControllerFanSettings | undefined;
624+
[key: `output_pin ${Lowercase<string>}`]: KlipperPrinterOutputPinSettings;
624625

625-
[key: `gcode_button ${string}`]: KlipperPrinterGcodeButtonSettings | undefined;
626+
[key: `${'led' | 'neopixel' | 'dotstar' | 'pca9533' | 'pca9632'} ${Lowercase<string>}`]: KlipperPrinterLedSettings;
626627

627-
[key: `output_pin ${Lowercase<string>}`]: KlipperPrinterOutputPinSettings | undefined;
628+
[key: `temperature_sensor ${Lowercase<string>}`]: KlipperPrinterTemperatureSensorSettings;
628629

629-
[key: `${'led' | 'neopixel' | 'dotstar' | 'pca9533' | 'pca9632'} ${Lowercase<string>}`]: KlipperPrinterLedSettings | undefined;
630+
safe_z_home: KlipperPrinterSafeZHomeSettings;
630631

631-
[key: `temperature_sensor ${Lowercase<string>}`]: KlipperPrinterTemperatureSensorSettings | undefined;
632+
z_tilt: KlipperPrinterZTiltSettings;
632633

633-
safe_z_home?: KlipperPrinterSafeZHomeSettings;
634+
bed_mesh: KlipperPrinterBedMeshSettings;
634635

635-
z_tilt?: KlipperPrinterZTiltSettings;
636+
board_pins: KlipperPrinterBoardPinsSettings;
636637

637-
bed_mesh?: KlipperPrinterBedMeshSettings;
638+
[key: `bed_mesh ${Lowercase<string>}`]: KlipperPrinterBedMeshModelSettings;
638639

639-
board_pins?: KlipperPrinterBoardPinsSettings;
640+
bed_screws: KlipperPrinterBedScrewsSettings;
640641

641-
[key: `bed_mesh ${Lowercase<string>}`]: KlipperPrinterBedMeshModelSettings | undefined;
642+
screws_tilt_adjust: KlipperPrinterScrewsTiltAdjustSettings;
642643

643-
bed_screws?: KlipperPrinterBedScrewsSettings;
644+
firmware_retraction: KlipperPrinterFirmwareRetractionSettings;
644645

645-
screws_tilt_adjust?: KlipperPrinterScrewsTiltAdjustSettings;
646+
force_move: KlipperPrinterForceMoveSettings;
646647

647-
firmware_retraction?: KlipperPrinterFirmwareRetractionSettings;
648+
gcode_arcs: KlipperPrinterGcodeArcsSettings;
648649

649-
force_move?: KlipperPrinterForceMoveSettings;
650+
respond: KlipperPrinterRespondSettings;
650651

651-
gcode_arcs?: KlipperPrinterGcodeArcsSettings;
652+
virtual_sdcard: KlipperPrinterVirtualSdcardSettings;
652653

653-
respond?: KlipperPrinterRespondSettings;
654+
pause_resume: KlipperPrinterPauseResumeSettings;
654655

655-
virtual_sdcard?: KlipperPrinterVirtualSdcardSettings;
656+
delta_calibrate: KlipperPrinterDeltaCalibrateSettings;
656657

657-
pause_resume?: KlipperPrinterPauseResumeSettings;
658+
[key: `gcode_macro ${Lowercase<string>}`]: KlipperPrinterGcodeMacroSettings;
658659

659-
delta_calibrate?: KlipperPrinterDeltaCalibrateSettings;
660+
heater_bed: KlipperPrinterHeaterBedSettings;
660661

661-
[key: `gcode_macro ${Lowercase<string>}`]: KlipperPrinterGcodeMacroSettings | undefined;
662+
[key: `verify_heater ${Lowercase<string>}`]: KlipperPrinterVerifyHeaterSettings;
662663

663-
heater_bed?: KlipperPrinterHeaterBedSettings;
664+
probe: KlipperPrinterProbeSettings;
664665

665-
[key: `verify_heater ${Lowercase<string>}`]: KlipperPrinterVerifyHeaterSettings | undefined;
666+
bltouch: KlipperPrinterBltouchSettings;
666667

667-
probe?: KlipperPrinterProbeSettings;
668+
smart_effector: KlipperPrinterSmartEffectorSettings;
668669

669-
bltouch?: KlipperPrinterBltouchSettings;
670+
[key: `probe_eddy_current ${Lowercase<string>}`]: KlipperPrinterProbeEddyCurrentSettings;
670671

671-
smart_effector?: KlipperPrinterSmartEffectorSettings;
672+
input_shaper: KlipperPrinterInputShaperSettings;
672673

673-
[key: `probe_eddy_current ${Lowercase<string>}`]: KlipperPrinterProbeEddyCurrentSettings | undefined;
674+
printer: KlipperPrinterPrinterSettings;
674675

675-
input_shaper?: KlipperPrinterInputShaperSettings;
676+
[key: `stepper_${Lowercase<string>}`]: KlipperPrinterStepperSettings;
676677

677-
printer?: KlipperPrinterPrinterSettings;
678+
[key: `extruder_stepper ${Lowercase<string>}`]: KlipperPrinterExtruderStepperSettings;
678679

679-
[key: `stepper_${Lowercase<string>}`]: KlipperPrinterStepperSettings | undefined;
680+
idle_timeout: KlipperPrinterIdleTimeoutSettings;
680681

681-
[key: `extruder_stepper ${Lowercase<string>}`]: KlipperPrinterExtruderStepperSettings | undefined;
682+
exclude_object: KlipperPrinterExcludeObjectSettings;
682683

683-
idle_timeout?: KlipperPrinterIdleTimeoutSettings;
684+
[key: `endstop_phase ${Lowercase<string>}`]: KlipperPrinterEndstopPhaseSettings;
684685

685-
exclude_object?: KlipperPrinterExcludeObjectSettings;
686+
[key: `display_template ${Lowercase<string>}`]: KlipperPrinterDisplayTemplateSettings;
686687

687-
[key: `endstop_phase ${Lowercase<string>}`]: KlipperPrinterEndstopPhaseSettings | undefined;
688+
load_cell: KlipperPrinterLoadCellSettings;
688689

689-
[key: `display_template ${Lowercase<string>}`]: KlipperPrinterDisplayTemplateSettings | undefined;
690+
[key: `load_cell ${Lowercase<string>}`]: KlipperPrinterLoadCellSettings;
690691

691-
load_cell?: KlipperPrinterLoadCellSettings;
692+
// These keys are for kalico modules
692693

693-
[key: `load_cell ${Lowercase<string>}`]: KlipperPrinterLoadCellSettings | undefined;
694+
danger_options: KalicoPrinterDangerOptionsSettings;
694695

695-
// These keys are for kalico modules
696+
constants: KalicoPrinterConstantsSettings;
696697

697-
danger_options?: KalicoPrinterDangerOptionsSettings;
698+
z_calibration: KalicoPrinterZCalibrationSettings;
698699

699-
constants?: KalicoPrinterConstantsSettings;
700+
z_tilt_ng: KalicoPrinterZTiltNgSettings;
700701

701-
z_calibration?: KalicoPrinterZCalibrationSettings;
702+
// These keys are for external modules
702703

703-
z_tilt_ng?: KalicoPrinterZTiltNgSettings;
704+
beacon: KlipperPrinterBeaconSettings;
704705

705-
// These keys are for external modules
706-
707-
beacon?: KlipperPrinterBeaconSettings;
706+
[key: `beacon model ${Lowercase<string>}`]: KlipperPrinterBeaconModelSettings;
707+
}>
708708

709-
[key: `beacon model ${Lowercase<string>}`]: KlipperPrinterBeaconModelSettings | undefined;
709+
export interface KlipperPrinterSettings extends KlipperPrinterSettingsBaseType {
710+
[key: string]: Record<string, any> | undefined;
710711
}
711712

712713
export interface KlipperPrinterMcuSettings {

0 commit comments

Comments
 (0)