Skip to content

Commit e60cef1

Browse files
committed
refactor: typing improvements and fixes
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 1d18a6b commit e60cef1

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/store/printer/getters.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import type { GetterTree } from 'vuex'
33
import type { RootState } from '../types'
4-
import type { PrinterState, Heater, Fan, Led, OutputPin, Sensor, RunoutSensor, KnownExtruder, MCU, Endstop, ExtruderStepper, Extruder, Stepper, ScrewsTiltAdjustScrew, ScrewsTiltAdjust, BedScrews, BedSize, GcodeCommands, TimeEstimates, KlippyApp, ExcludeObjectPart, KlipperPrinterConfig, KlipperPrinterProbeState, BeaconModel, BedScrewsScrew } from './types'
4+
import type { PrinterState, Heater, Fan, Led, OutputPin, Sensor, RunoutSensor, KnownExtruder, MCU, Endstop, ExtruderStepper, Extruder, Stepper, ScrewsTiltAdjustScrew, ScrewsTiltAdjust, BedScrews, BedSize, GcodeCommands, TimeEstimates, KlippyApp, ExcludeObjectPart, KlipperPrinterConfig, KlipperPrinterProbeState, BeaconModel, BedScrewsScrew, ExtruderKey } from './types'
55
import getKlipperType from '@/util/get-klipper-type'
66
import i18n from '@/plugins/i18n'
77
import type { GcodeHelp } from '../console/types'
@@ -346,15 +346,16 @@ export const getters: GetterTree<PrinterState, RootState> = {
346346
/**
347347
* Return known extruders, giving them a friendly name.
348348
*/
349-
getExtruders: (state) => {
350-
const extruderCount = Object.keys(state.printer)
351-
.filter(key => /^extruder\d{0,2}$/.exec(key))
352-
.length
353-
354-
return [...Array(extruderCount).keys()]
355-
.map((index): KnownExtruder => ({
356-
key: `extruder${index === 0 ? '' : index}`,
357-
name: extruderCount === 1 ? 'Extruder' : `Extruder ${index}`
349+
getExtruders: (state): KnownExtruder[] => {
350+
const extruderKeys = Object.keys(state.printer)
351+
.filter((key): key is ExtruderKey => /^extruder\d{0,2}$/.test(key))
352+
.sort((a, b) => +a.substring(8) - +b.substring(8))
353+
const hasMultipleExtruders = extruderKeys.length > 1
354+
355+
return extruderKeys
356+
.map((key, index) => ({
357+
key,
358+
name: hasMultipleExtruders ? `Extruder ${index}` : 'Extruder'
358359
}))
359360
},
360361

@@ -368,7 +369,7 @@ export const getters: GetterTree<PrinterState, RootState> = {
368369
},
369370

370371
// Returns an extruder by name.
371-
getExtruderByName: (state) => (name: 'extruder' | `extruder${number}`) => {
372+
getExtruderByName: (state) => (name: ExtruderKey) => {
372373
const e = state.printer[name]
373374
const c = state.printer.configfile.settings[name.toLowerCase()]
374375

src/store/printer/types.ts

+33-28
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ export interface PrinterInfo {
2828

2929
export type PrinterInfoState = 'ready' | 'startup' | 'shutdown' | 'error'
3030

31-
export interface KlipperPrinterState {
31+
type NonZeroDigit = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
32+
type Digit = '0' | NonZeroDigit
33+
34+
export type ExtruderKey = 'extruder' | `extruder${NonZeroDigit}` | `extruder${NonZeroDigit}${Digit}`
35+
36+
type KlipperPrinterStateBaseType = {
37+
[key in ExtruderKey]?: KlipperPrinterExtruderState
38+
}
39+
40+
export interface KlipperPrinterState extends KlipperPrinterStateBaseType {
3241
[key: string]: any;
3342

3443
// These keys are always available
@@ -81,10 +90,6 @@ export interface KlipperPrinterState {
8190

8291
hall_filament_width_sensor?: KlipperPrinterHallFilamentWidthSensorState;
8392

84-
extruder?: KlipperPrinterExtruderState;
85-
86-
[key: `extruder${number}`]: KlipperPrinterExtruderState;
87-
8893
heater_bed?: KlipperPrinterHeaterBedState;
8994

9095
[key: `heater_generic ${string}`]: KlipperPrinterHeaterGenericState;
@@ -198,7 +203,7 @@ export interface KlipperPrinterToolheadState {
198203
print_time: number;
199204
stalls: number;
200205
estimated_print_time: number;
201-
extruder: string;
206+
extruder: '' | ExtruderKey;
202207
position: [number, number, number, number];
203208
max_velocity: number;
204209
max_accel: number;
@@ -564,26 +569,30 @@ export interface KlipperPrinterBeaconState {
564569
export interface KlipperPrinterConfig extends Record<string, Record<string, string>> {
565570
}
566571

567-
export interface KlipperPrinterSettings {
572+
type KlipperPrinterSettingsBaseType = {
573+
[key in ExtruderKey]?: KlipperPrinterExtruderSettings
574+
}
575+
576+
export interface KlipperPrinterSettings extends KlipperPrinterSettingsBaseType {
568577
[key: string]: any;
569578

570579
mcu?: KlipperPrinterMcuSettings;
571580

572-
[key: `mcu ${string}`]: KlipperPrinterMcuSettings;
581+
[key: `mcu ${Lowercase<string>}`]: KlipperPrinterMcuSettings;
573582

574-
[key: `tmc${'2130' | '2208' | '2209' | '2660' | '2240' | '5160'} ${string}`]: KlipperPrinterTmcSettings;
583+
[key: `tmc${'2130' | '2208' | '2209' | '2660' | '2240' | '5160'} ${Lowercase<string>}`]: KlipperPrinterTmcSettings;
575584

576585
fan?: KlipperPrinterFanSettings;
577586

578-
[key: `${'heater_fan'} ${string}`]: KlipperPrinterHeaterFanSettings;
587+
[key: `heater_fan ${Lowercase<string>}`]: KlipperPrinterHeaterFanSettings;
579588

580-
[key: `${'controller_fan'} ${string}`]: KlipperPrinterControllerFanSettings;
589+
[key: `controller_fan ${Lowercase<string>}`]: KlipperPrinterControllerFanSettings;
581590

582-
[key: `output_pin ${string}`]: KlipperPrinterOutputPinSettings;
591+
[key: `output_pin ${Lowercase<string>}`]: KlipperPrinterOutputPinSettings;
583592

584-
[key: `${'led' | 'neopixel' | 'dotstar' | 'pca9533' | 'pca9632'} ${string}`]: KlipperPrinterLedSettings;
593+
[key: `${'led' | 'neopixel' | 'dotstar' | 'pca9533' | 'pca9632'} ${Lowercase<string>}`]: KlipperPrinterLedSettings;
585594

586-
[key: `temperature_sensor ${string}`]: KlipperPrinterTemperatureSensorSettings;
595+
[key: `temperature_sensor ${Lowercase<string>}`]: KlipperPrinterTemperatureSensorSettings;
587596

588597
safe_z_home?: KlipperPrinterSafeZHomeSettings;
589598

@@ -593,7 +602,7 @@ export interface KlipperPrinterSettings {
593602

594603
board_pins?: KlipperPrinterBoardPinsSettings;
595604

596-
[key: `bed_mesh ${string}`]: KlipperPrinterBedMeshModelSettings;
605+
[key: `bed_mesh ${Lowercase<string>}`]: KlipperPrinterBedMeshModelSettings;
597606

598607
bed_screws?: KlipperPrinterBedScrewsSettings;
599608

@@ -613,11 +622,11 @@ export interface KlipperPrinterSettings {
613622

614623
delta_calibrate?: KlipperPrinterDeltaCalibrateSettings;
615624

616-
[key: `gcode_macro ${string}`]: KlipperPrinterGcodeMacroSettings;
625+
[key: `gcode_macro ${Lowercase<string>}`]: KlipperPrinterGcodeMacroSettings;
617626

618627
heater_bed?: KlipperPrinterHeaterBedSettings;
619628

620-
[key: `verify_heater ${string}`]: KlipperPrinterVerifyHeaterSettings;
629+
[key: `verify_heater ${Lowercase<string>}`]: KlipperPrinterVerifyHeaterSettings;
621630

622631
probe?: KlipperPrinterProbeSettings;
623632

@@ -627,27 +636,23 @@ export interface KlipperPrinterSettings {
627636

628637
printer?: KlipperPrinterPrinterSettings;
629638

630-
[key: `stepper_${string}`]: KlipperPrinterStepperSettings;
639+
[key: `stepper_${Lowercase<string>}`]: KlipperPrinterStepperSettings;
631640

632-
[key: `extruder_stepper ${string}`]: KlipperPrinterExtruderStepperSettings;
641+
[key: `extruder_stepper ${Lowercase<string>}`]: KlipperPrinterExtruderStepperSettings;
633642

634643
idle_timeout?: KlipperPrinterIdleTimeoutSettings;
635644

636-
extruder?: KlipperPrinterExtruderSettings;
637-
638-
[key: `extruder${number}`]: KlipperPrinterExtruderSettings;
639-
640645
exclude_object?: KlipperPrinterExcludeObjectSettings;
641646

642-
[key: `endstop_phase ${string}`]: KlipperPrinterEndstopPhaseSettings;
647+
[key: `endstop_phase ${Lowercase<string>}`]: KlipperPrinterEndstopPhaseSettings;
643648

644-
[key: `display_template ${string}`]: KlipperPrinterDisplayTemplateSettings;
649+
[key: `display_template ${Lowercase<string>}`]: KlipperPrinterDisplayTemplateSettings;
645650

646651
// These keys are for external modules
647652

648653
beacon?: KlipperPrinterBeaconSettings;
649654

650-
[key: `beacon model ${string}`]: KlipperPrinterBeaconModelSettings;
655+
[key: `beacon model ${Lowercase<string>}`]: KlipperPrinterBeaconModelSettings;
651656
}
652657

653658
export interface KlipperPrinterMcuSettings {
@@ -1033,7 +1038,7 @@ export interface KlipperPrinterBeaconModelSettings extends Record<string, any> {
10331038

10341039
export interface KnownExtruder {
10351040
name: string;
1036-
key: 'extruder' | `extruder${number}`;
1041+
key: ExtruderKey;
10371042
}
10381043

10391044
export interface Extruder extends KlipperPrinterExtruderState {
@@ -1042,7 +1047,7 @@ export interface Extruder extends KlipperPrinterExtruderState {
10421047
}
10431048

10441049
export interface ExtruderStepper extends StepperType<ExtruderStepperConfig> {
1045-
motion_queue?: string | null;
1050+
motion_queue?: ExtruderKey | null;
10461051
pressure_advance?: number;
10471052
smooth_time?: number;
10481053
}

0 commit comments

Comments
 (0)