Skip to content

Commit

Permalink
refactor: enums to union of literals
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas committed Jul 24, 2023
1 parent 3bed914 commit f9ef7c5
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 69 deletions.
26 changes: 13 additions & 13 deletions src/components/widgets/history/JobHistoryItemStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
<script lang="ts">
import { Component, Mixins, Prop } from 'vue-property-decorator'
import FilesMixin from '@/mixins/files'
import { HistoryItem, HistoryItemStatus } from '@/store/history/types'
import { HistoryItem } from '@/store/history/types'
@Component({})
export default class JobHistoryItemStatus extends Mixins(FilesMixin) {
@Prop({ type: Object, required: true })
readonly job!: HistoryItem
// get status () {
// if (this.job.status === HistoryItemStatus.Completed) return HistoryItemStatus.Completed
// if (this.job.status === HistoryItemStatus.InProgress) return HistoryItemStatus.InProgress
// if (this.job.status === 'completed') return 'completed'
// if (this.job.status === 'in_progress') return 'in_progress'
// if (this.job.status.indexOf('_')) {
// return this.job.status.split('_').pop()
// }
Expand All @@ -53,29 +53,29 @@ export default class JobHistoryItemStatus extends Mixins(FilesMixin) {
get state () {
if (
this.job.status === HistoryItemStatus.Cancelled ||
this.job.status === HistoryItemStatus.Error ||
this.job.status === HistoryItemStatus.Server_Exit
this.job.status === 'cancelled' ||
this.job.status === 'error' ||
this.job.status === 'server_exit'
) return 'error'
if (
this.job.status === HistoryItemStatus.Printing ||
this.job.status === HistoryItemStatus.Completed ||
this.job.status === HistoryItemStatus.InProgress
this.job.status === 'printing' ||
this.job.status === 'completed' ||
this.job.status === 'in_progress'
) return 'success'
if (
this.job.status === HistoryItemStatus.Klippy_Shutdown ||
this.job.status === HistoryItemStatus.Klippy_Disconnect
this.job.status === 'klippy_shutdown' ||
this.job.status === 'klippy_disconnect'
) return 'warning'
return 'success'
}
get inError () {
return (
this.job.status !== HistoryItemStatus.Completed &&
this.job.status !== HistoryItemStatus.InProgress
this.job.status !== 'completed' &&
this.job.status !== 'in_progress'
)
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/eventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue'

export const EventBus = {
bus: new Vue(),
$emit: (text?: string, options = {}): void => {
$emit: (text?: string, options: Partial<FlashMessage> = {}): void => {
const opts: FlashMessage = {
open: true,
timeout: -1,
Expand All @@ -23,10 +23,4 @@ export interface FlashMessage {
timeout?: number;
}

export enum FlashMessageTypes {
success = 'success',
error = 'error',
warning = 'warning',
primary = 'primary',
secondary = 'secondary'
}
export type FlashMessageTypes = 'success' | 'error' | 'warning' | 'primary' | 'secondary'
10 changes: 5 additions & 5 deletions src/plugins/httpClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _Vue from 'vue'
import { EventBus, FlashMessageTypes } from '@/eventBus'
import { EventBus } from '@/eventBus'
import { consola } from 'consola'
import Axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import { Globals } from '@/globals'
Expand Down Expand Up @@ -103,13 +103,13 @@ const createHttpClient = (store: any) => {
switch (error.response.status) {
case 500:
consola.debug(error.response.status, error.message, message)
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
EventBus.$emit(message || 'Server error', { type: 'error' })
break
case 502:
case 400:
consola.debug(error.response.status, error.message, message)
if (!handledErrorRequests[error.response.status].includes(url)) {
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
EventBus.$emit(message || 'Server error', { type: 'error' })
}
break
case 401:
Expand All @@ -120,11 +120,11 @@ const createHttpClient = (store: any) => {
break
case 404:
consola.debug(error.response.status, error.message, message)
// EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.warning })
// EventBus.$emit(message || 'Server error', { type: 'warning' })
break
default:
consola.debug(error.response.status, error.message)
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
EventBus.$emit(message || 'Server error', { type: 'error' })
}

return Promise.reject(error)
Expand Down
10 changes: 2 additions & 8 deletions src/store/gcodePreview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export interface ArcMove extends LinearMove {

export type Move = LinearMove | ArcMove;

export enum Rotation {
Clockwise = 'clockwise',
CounterClockwise = 'counter-clockwise',
}
export type Rotation = 'clockwise' | 'counter-clockwise'

export interface LayerPaths {
moves: string;
Expand All @@ -63,10 +60,7 @@ export interface Point3D extends Point {
z: number;
}

export enum PositioningMode {
Relative = 'relative',
Absolute = 'absolute'
}
export type PositioningMode = 'relative' | 'absolute'

export interface Layer {
move: number;
Expand Down
11 changes: 1 addition & 10 deletions src/store/history/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,4 @@ export interface HistoryRollUp {
longest_print: number;
}

export enum HistoryItemStatus {
Completed = 'completed',
Cancelled = 'cancelled',
Error = 'error',
Printing = 'printing',
InProgress = 'in_progress',
Server_Exit = 'server_exit',
Klippy_Shutdown = 'klippy_shutdown',
Klippy_Disconnect = 'klippy_disconnect'
}
export type HistoryItemStatus = 'completed' | 'cancelled' | 'error' | 'printing' | 'in_progress' | 'server_exit' | 'klippy_shutdown' | 'klippy_disconnect'
4 changes: 2 additions & 2 deletions src/store/notifications/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionTree } from 'vuex'
import { EventBus, FlashMessageTypes } from '@/eventBus'
import { EventBus } from '@/eventBus'
import { v4 as uuidv4 } from 'uuid'
import { NotificationsState, AppPushNotification, AppNotification } from './types'
import { RootState } from '../types'
Expand Down Expand Up @@ -46,7 +46,7 @@ export const actions: ActionTree<NotificationsState, RootState> = {

if (payload.snackbar) {
// Emit if snackbar is true.
EventBus.$emit(n.title, { type: FlashMessageTypes.error })
EventBus.$emit(n.title, { type: 'error' })
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/store/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RootState } from '../types'
import { SocketActions } from '@/api/socketActions'
import { Globals } from '@/globals'
import { AppPushNotification } from '../notifications/types'
import { EventBus, FlashMessageTypes } from '@/eventBus'
import { EventBus } from '@/eventBus'
import i18n from '@/plugins/i18n'

let retryTimeout: number
Expand Down Expand Up @@ -87,14 +87,14 @@ export const actions: ActionTree<ServerState, RootState> = {
const message = Object.values(payload.failed)
.join('\n')

EventBus.$emit(message, { type: FlashMessageTypes.error })
EventBus.$emit(message, { type: 'error' })
} else if (payload?.rolled_over && payload.rolled_over.length) {
const applications = payload.rolled_over
.map(Vue.$filters.startCase)
.join(', ')
const message = i18n.tc('app.general.msg.rolledover_logs', 0, { applications })

EventBus.$emit(message, { type: FlashMessageTypes.success })
EventBus.$emit(message, { type: 'success' })
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/store/socket/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SocketState } from './types'
import { RootState } from '../types'
import { Globals } from '@/globals'
import { SocketActions } from '@/api/socketActions'
import { EventBus, FlashMessageTypes } from '@/eventBus'
import { EventBus } from '@/eventBus'
import { upperFirst, camelCase } from 'lodash-es'
import IsKeyOf from '@/util/is-key-of'

Expand Down Expand Up @@ -100,7 +100,7 @@ export const actions: ActionTree<SocketState, RootState> = {
message = payload.message
}

EventBus.$emit(message, { type: FlashMessageTypes.error })
EventBus.$emit(message, { type: 'error' })
}
if (payload.code === 503) {
// This indicates klippy is non-responsive, or there's a configuration error
Expand Down
4 changes: 2 additions & 2 deletions src/store/timelapse/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TimelapseState } from './types'
import { RootState } from '../types'
import { SocketActions } from '@/api/socketActions'
import { consola } from 'consola'
import { EventBus, FlashMessageTypes } from '@/eventBus'
import { EventBus } from '@/eventBus'
import i18n from '@/plugins/i18n'

export const actions: ActionTree<TimelapseState, RootState> = {
Expand Down Expand Up @@ -39,7 +39,7 @@ export const actions: ActionTree<TimelapseState, RootState> = {
case 'newframe': {
if (payload.status === 'error') {
// open snackbar
EventBus.$emit(i18n.tc('app.timelapse.error.newframe'), { type: FlashMessageTypes.error })
EventBus.$emit(i18n.tc('app.timelapse.error.newframe'), { type: 'error' })
} else {
const count = parseInt(payload.frame)
commit('setLastFrame', {
Expand Down
6 changes: 3 additions & 3 deletions src/util/gcode-preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArcMove, Move, Point, Rotation } from '@/store/gcodePreview/types'
import { ArcMove, Move, Point } from '@/store/gcodePreview/types'

type BinarySearchComparer<T> = (item: T, index: number, array: T[]) => number

Expand Down Expand Up @@ -60,11 +60,11 @@ function arcIJMoveToSVGPath (toolhead: Point, move: ArcMove): string {
}

switch (move.direction) {
case Rotation.Clockwise:
case 'clockwise':
return 'A' + [
radius, radius, 0, Number(angle < 0), 0, destination.x, destination.y
].join(',')
case Rotation.CounterClockwise:
case 'counter-clockwise':
return '' +
'M' + [destination.x, destination.y].join(',') +
'A' + [radius, radius, 0, Number(angle > 0), 0, toolhead.x, toolhead.y].join(',') +
Expand Down
26 changes: 13 additions & 13 deletions src/workers/parseGcode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-fallthrough */
import { ArcMove, Layer, LinearMove, Move, Part, Point, PositioningMode, Rotation } from '@/store/gcodePreview/types'
import { ArcMove, Layer, LinearMove, Move, Part, Point, PositioningMode } from '@/store/gcodePreview/types'
import IsKeyOf from '@/util/is-key-of'
import { pick } from 'lodash-es'
import shlex from 'shlex'
Expand Down Expand Up @@ -70,8 +70,8 @@ const parseGcode = (gcode: string, sendProgress: (filePosition: number) => void)
const lines = gcode.split('\n')

let newLayerForNextMove = false
let extrusionMode = PositioningMode.Relative
let positioningMode = PositioningMode.Absolute
let extrusionMode: PositioningMode = 'relative'
let positioningMode: PositioningMode = 'absolute'
const toolhead = {
x: 0,
y: 0,
Expand Down Expand Up @@ -136,8 +136,8 @@ const parseGcode = (gcode: string, sendProgress: (filePosition: number) => void)
'i', 'j', 'k', 'r'
]),
direction: command === 'G2'
? Rotation.Clockwise
: Rotation.CounterClockwise,
? 'clockwise'
: 'counter-clockwise',
filePosition: toolhead.filePosition
} satisfies ArcMove
break
Expand All @@ -162,22 +162,22 @@ const parseGcode = (gcode: string, sendProgress: (filePosition: number) => void)
}
break
case 'G90':
positioningMode = PositioningMode.Absolute
positioningMode = 'absolute'
case 'M82':
extrusionMode = PositioningMode.Absolute
extrusionMode = 'absolute'
toolhead.e = 0
break
case 'G91':
positioningMode = PositioningMode.Relative
positioningMode = 'relative'
case 'M83':
extrusionMode = PositioningMode.Relative
extrusionMode = 'relative'
break
case 'G92':
if (extrusionMode === PositioningMode.Absolute) {
if (extrusionMode === 'absolute') {
toolhead.e = args.e ?? toolhead.e
}

if (positioningMode === PositioningMode.Absolute) {
if (positioningMode === 'absolute') {
toolhead.x = args.x ?? toolhead.x
toolhead.y = args.y ?? toolhead.y
toolhead.z = args.z ?? toolhead.z
Expand All @@ -191,14 +191,14 @@ const parseGcode = (gcode: string, sendProgress: (filePosition: number) => void)
}

if (move) {
if (extrusionMode === PositioningMode.Absolute && move.e !== undefined) {
if (extrusionMode === 'absolute' && move.e !== undefined) {
const extrusionLength = decimalRound(move.e - toolhead.e)

toolhead.e = move.e
move.e = extrusionLength
}

if (positioningMode === PositioningMode.Relative) {
if (positioningMode === 'relative') {
if (move.x !== undefined) {
move.x = decimalRound(move.x + toolhead.x)
}
Expand Down

0 comments on commit f9ef7c5

Please sign in to comment.