internal: Add ArtifactId type (#3752)

* Add ArtifactId type

* Use ArtifactId type in more places

---------

Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
This commit is contained in:
Jonathan Tran
2024-09-04 11:49:13 -04:00
committed by GitHub
parent 35f9b82a65
commit 57f4e1b79c

View File

@ -3,6 +3,8 @@ import { Models } from '@kittycad/lib'
import { getNodePathFromSourceRange } from 'lang/queryAst' import { getNodePathFromSourceRange } from 'lang/queryAst'
import { err } from 'lib/trap' import { err } from 'lib/trap'
export type ArtifactId = string
interface CommonCommandProperties { interface CommonCommandProperties {
range: SourceRange range: SourceRange
pathToNode: PathToNode pathToNode: PathToNode
@ -10,7 +12,7 @@ interface CommonCommandProperties {
export interface PlaneArtifact { export interface PlaneArtifact {
type: 'plane' type: 'plane'
pathIds: Array<string> pathIds: Array<ArtifactId>
codeRef: CommonCommandProperties codeRef: CommonCommandProperties
} }
export interface PlaneArtifactRich { export interface PlaneArtifactRich {
@ -21,16 +23,16 @@ export interface PlaneArtifactRich {
export interface PathArtifact { export interface PathArtifact {
type: 'path' type: 'path'
planeId: string planeId: ArtifactId
segIds: Array<string> segIds: Array<ArtifactId>
extrusionId: string extrusionId: ArtifactId
solid2dId?: string solid2dId?: ArtifactId
codeRef: CommonCommandProperties codeRef: CommonCommandProperties
} }
interface solid2D { interface solid2D {
type: 'solid2D' type: 'solid2D'
pathId: string pathId: ArtifactId
} }
export interface PathArtifactRich { export interface PathArtifactRich {
type: 'path' type: 'path'
@ -42,10 +44,10 @@ export interface PathArtifactRich {
interface SegmentArtifact { interface SegmentArtifact {
type: 'segment' type: 'segment'
pathId: string pathId: ArtifactId
surfaceId: string surfaceId: ArtifactId
edgeIds: Array<string> edgeIds: Array<ArtifactId>
edgeCutId?: string edgeCutId?: ArtifactId
codeRef: CommonCommandProperties codeRef: CommonCommandProperties
} }
interface SegmentArtifactRich { interface SegmentArtifactRich {
@ -59,9 +61,9 @@ interface SegmentArtifactRich {
interface ExtrusionArtifact { interface ExtrusionArtifact {
type: 'extrusion' type: 'extrusion'
pathId: string pathId: ArtifactId
surfaceIds: Array<string> surfaceIds: Array<ArtifactId>
edgeIds: Array<string> edgeIds: Array<ArtifactId>
codeRef: CommonCommandProperties codeRef: CommonCommandProperties
} }
interface ExtrusionArtifactRich { interface ExtrusionArtifactRich {
@ -74,23 +76,23 @@ interface ExtrusionArtifactRich {
interface WallArtifact { interface WallArtifact {
type: 'wall' type: 'wall'
segId: string segId: ArtifactId
edgeCutEdgeIds: Array<string> edgeCutEdgeIds: Array<ArtifactId>
extrusionId: string extrusionId: ArtifactId
pathIds: Array<string> pathIds: Array<ArtifactId>
} }
interface CapArtifact { interface CapArtifact {
type: 'cap' type: 'cap'
subType: 'start' | 'end' subType: 'start' | 'end'
edgeCutEdgeIds: Array<string> edgeCutEdgeIds: Array<ArtifactId>
extrusionId: string extrusionId: ArtifactId
pathIds: Array<string> pathIds: Array<ArtifactId>
} }
interface ExtrudeEdge { interface ExtrudeEdge {
type: 'extrudeEdge' type: 'extrudeEdge'
segId: string segId: ArtifactId
extrusionId: string extrusionId: ArtifactId
subType: 'opposite' | 'adjacent' subType: 'opposite' | 'adjacent'
} }
@ -98,16 +100,16 @@ interface ExtrudeEdge {
interface EdgeCut { interface EdgeCut {
type: 'edgeCut' type: 'edgeCut'
subType: 'fillet' | 'chamfer' subType: 'fillet' | 'chamfer'
consumedEdgeId: string consumedEdgeId: ArtifactId
edgeIds: Array<string> edgeIds: Array<ArtifactId>
surfaceId: string surfaceId: ArtifactId
codeRef: CommonCommandProperties codeRef: CommonCommandProperties
} }
interface EdgeCutEdge { interface EdgeCutEdge {
type: 'edgeCutEdge' type: 'edgeCutEdge'
edgeCutId: string edgeCutId: ArtifactId
surfaceId: string surfaceId: ArtifactId
} }
export type Artifact = export type Artifact =
@ -122,7 +124,7 @@ export type Artifact =
| EdgeCutEdge | EdgeCutEdge
| solid2D | solid2D
export type ArtifactGraph = Map<string, Artifact> export type ArtifactGraph = Map<ArtifactId, Artifact>
export type EngineCommand = Models['WebSocketRequest_type'] export type EngineCommand = Models['WebSocketRequest_type']
@ -149,7 +151,7 @@ export function createArtifactGraph({
responseMap: ResponseMap responseMap: ResponseMap
ast: Program ast: Program
}) { }) {
const myMap = new Map<string, Artifact>() const myMap = new Map<ArtifactId, Artifact>()
/** see docstring for {@link getArtifactsToUpdate} as to why this is needed */ /** see docstring for {@link getArtifactsToUpdate} as to why this is needed */
let currentPlaneId = '' let currentPlaneId = ''
@ -166,7 +168,7 @@ export function createArtifactGraph({
const artifactsToUpdate = getArtifactsToUpdate({ const artifactsToUpdate = getArtifactsToUpdate({
orderedCommand, orderedCommand,
responseMap, responseMap,
getArtifact: (id: string) => myMap.get(id), getArtifact: (id: ArtifactId) => myMap.get(id),
currentPlaneId, currentPlaneId,
ast, ast,
}) })
@ -224,11 +226,11 @@ export function getArtifactsToUpdate({
orderedCommand: OrderedCommand orderedCommand: OrderedCommand
responseMap: ResponseMap responseMap: ResponseMap
/** Passing in a getter because we don't wan this function to update the map directly */ /** Passing in a getter because we don't wan this function to update the map directly */
getArtifact: (id: string) => Artifact | undefined getArtifact: (id: ArtifactId) => Artifact | undefined
currentPlaneId: string currentPlaneId: ArtifactId
ast: Program ast: Program
}): Array<{ }): Array<{
id: string id: ArtifactId
artifact: Artifact artifact: Artifact
}> { }> {
const pathToNode = getNodePathFromSourceRange(ast, range) const pathToNode = getNodePathFromSourceRange(ast, range)
@ -514,7 +516,7 @@ export function filterArtifacts<T extends Artifact['type'][]>(
(!predicate || (!predicate ||
predicate(value as Extract<Artifact, { type: T[number] }>)) predicate(value as Extract<Artifact, { type: T[number] }>))
) )
) as Map<string, Extract<Artifact, { type: T[number] }>> ) as Map<ArtifactId, Extract<Artifact, { type: T[number] }>>
} }
export function getArtifactsOfTypes<T extends Artifact['type'][]>( export function getArtifactsOfTypes<T extends Artifact['type'][]>(
@ -528,7 +530,7 @@ export function getArtifactsOfTypes<T extends Artifact['type'][]>(
predicate?: (value: Extract<Artifact, { type: T[number] }>) => boolean predicate?: (value: Extract<Artifact, { type: T[number] }>) => boolean
}, },
map: ArtifactGraph map: ArtifactGraph
): Map<string, Extract<Artifact, { type: T[number] }>> { ): Map<ArtifactId, Extract<Artifact, { type: T[number] }>> {
return new Map( return new Map(
[...map].filter( [...map].filter(
([key, value]) => ([key, value]) =>
@ -537,7 +539,7 @@ export function getArtifactsOfTypes<T extends Artifact['type'][]>(
(!predicate || (!predicate ||
predicate(value as Extract<Artifact, { type: T[number] }>)) predicate(value as Extract<Artifact, { type: T[number] }>))
) )
) as Map<string, Extract<Artifact, { type: T[number] }>> ) as Map<ArtifactId, Extract<Artifact, { type: T[number] }>>
} }
export function getArtifactOfTypes<T extends Artifact['type'][]>( export function getArtifactOfTypes<T extends Artifact['type'][]>(
@ -545,7 +547,7 @@ export function getArtifactOfTypes<T extends Artifact['type'][]>(
key, key,
types, types,
}: { }: {
key: string key: ArtifactId
types: T types: T
}, },
map: ArtifactGraph map: ArtifactGraph
@ -718,7 +720,7 @@ export function getExtrudeEdgeCodeRef(
} }
export function getExtrusionFromSuspectedExtrudeSurface( export function getExtrusionFromSuspectedExtrudeSurface(
id: string, id: ArtifactId,
artifactGraph: ArtifactGraph artifactGraph: ArtifactGraph
): ExtrusionArtifact | Error { ): ExtrusionArtifact | Error {
const artifact = getArtifactOfTypes( const artifact = getArtifactOfTypes(
@ -733,7 +735,7 @@ export function getExtrusionFromSuspectedExtrudeSurface(
} }
export function getExtrusionFromSuspectedPath( export function getExtrusionFromSuspectedPath(
id: string, id: ArtifactId,
artifactGraph: ArtifactGraph artifactGraph: ArtifactGraph
): ExtrusionArtifact | Error { ): ExtrusionArtifact | Error {
const path = getArtifactOfTypes({ key: id, types: ['path'] }, artifactGraph) const path = getArtifactOfTypes({ key: id, types: ['path'] }, artifactGraph)