Skip to content

Commit

Permalink
internal: Add ArtifactId type (#3752)
Browse files Browse the repository at this point in the history
* Add ArtifactId type

* Use ArtifactId type in more places

---------

Co-authored-by: Kurt Hutten <[email protected]>
  • Loading branch information
jtran and Irev-Dev authored Sep 4, 2024
1 parent 35f9b82 commit 57f4e1b
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions src/lang/std/artifactGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Models } from '@kittycad/lib'
import { getNodePathFromSourceRange } from 'lang/queryAst'
import { err } from 'lib/trap'

export type ArtifactId = string

interface CommonCommandProperties {
range: SourceRange
pathToNode: PathToNode
}

export interface PlaneArtifact {
type: 'plane'
pathIds: Array<string>
pathIds: Array<ArtifactId>
codeRef: CommonCommandProperties
}
export interface PlaneArtifactRich {
Expand All @@ -21,16 +23,16 @@ export interface PlaneArtifactRich {

export interface PathArtifact {
type: 'path'
planeId: string
segIds: Array<string>
extrusionId: string
solid2dId?: string
planeId: ArtifactId
segIds: Array<ArtifactId>
extrusionId: ArtifactId
solid2dId?: ArtifactId
codeRef: CommonCommandProperties
}

interface solid2D {
type: 'solid2D'
pathId: string
pathId: ArtifactId
}
export interface PathArtifactRich {
type: 'path'
Expand All @@ -42,10 +44,10 @@ export interface PathArtifactRich {

interface SegmentArtifact {
type: 'segment'
pathId: string
surfaceId: string
edgeIds: Array<string>
edgeCutId?: string
pathId: ArtifactId
surfaceId: ArtifactId
edgeIds: Array<ArtifactId>
edgeCutId?: ArtifactId
codeRef: CommonCommandProperties
}
interface SegmentArtifactRich {
Expand All @@ -59,9 +61,9 @@ interface SegmentArtifactRich {

interface ExtrusionArtifact {
type: 'extrusion'
pathId: string
surfaceIds: Array<string>
edgeIds: Array<string>
pathId: ArtifactId
surfaceIds: Array<ArtifactId>
edgeIds: Array<ArtifactId>
codeRef: CommonCommandProperties
}
interface ExtrusionArtifactRich {
Expand All @@ -74,40 +76,40 @@ interface ExtrusionArtifactRich {

interface WallArtifact {
type: 'wall'
segId: string
edgeCutEdgeIds: Array<string>
extrusionId: string
pathIds: Array<string>
segId: ArtifactId
edgeCutEdgeIds: Array<ArtifactId>
extrusionId: ArtifactId
pathIds: Array<ArtifactId>
}
interface CapArtifact {
type: 'cap'
subType: 'start' | 'end'
edgeCutEdgeIds: Array<string>
extrusionId: string
pathIds: Array<string>
edgeCutEdgeIds: Array<ArtifactId>
extrusionId: ArtifactId
pathIds: Array<ArtifactId>
}

interface ExtrudeEdge {
type: 'extrudeEdge'
segId: string
extrusionId: string
segId: ArtifactId
extrusionId: ArtifactId
subType: 'opposite' | 'adjacent'
}

/** A edgeCut is a more generic term for both fillet or chamfer */
interface EdgeCut {
type: 'edgeCut'
subType: 'fillet' | 'chamfer'
consumedEdgeId: string
edgeIds: Array<string>
surfaceId: string
consumedEdgeId: ArtifactId
edgeIds: Array<ArtifactId>
surfaceId: ArtifactId
codeRef: CommonCommandProperties
}

interface EdgeCutEdge {
type: 'edgeCutEdge'
edgeCutId: string
surfaceId: string
edgeCutId: ArtifactId
surfaceId: ArtifactId
}

export type Artifact =
Expand All @@ -122,7 +124,7 @@ export type Artifact =
| EdgeCutEdge
| solid2D

export type ArtifactGraph = Map<string, Artifact>
export type ArtifactGraph = Map<ArtifactId, Artifact>

export type EngineCommand = Models['WebSocketRequest_type']

Expand All @@ -149,7 +151,7 @@ export function createArtifactGraph({
responseMap: ResponseMap
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 */
let currentPlaneId = ''
Expand All @@ -166,7 +168,7 @@ export function createArtifactGraph({
const artifactsToUpdate = getArtifactsToUpdate({
orderedCommand,
responseMap,
getArtifact: (id: string) => myMap.get(id),
getArtifact: (id: ArtifactId) => myMap.get(id),
currentPlaneId,
ast,
})
Expand Down Expand Up @@ -224,11 +226,11 @@ export function getArtifactsToUpdate({
orderedCommand: OrderedCommand
responseMap: ResponseMap
/** Passing in a getter because we don't wan this function to update the map directly */
getArtifact: (id: string) => Artifact | undefined
currentPlaneId: string
getArtifact: (id: ArtifactId) => Artifact | undefined
currentPlaneId: ArtifactId
ast: Program
}): Array<{
id: string
id: ArtifactId
artifact: Artifact
}> {
const pathToNode = getNodePathFromSourceRange(ast, range)
Expand Down Expand Up @@ -514,7 +516,7 @@ export function filterArtifacts<T extends Artifact['type'][]>(
(!predicate ||
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'][]>(
Expand All @@ -528,7 +530,7 @@ export function getArtifactsOfTypes<T extends Artifact['type'][]>(
predicate?: (value: Extract<Artifact, { type: T[number] }>) => boolean
},
map: ArtifactGraph
): Map<string, Extract<Artifact, { type: T[number] }>> {
): Map<ArtifactId, Extract<Artifact, { type: T[number] }>> {
return new Map(
[...map].filter(
([key, value]) =>
Expand All @@ -537,15 +539,15 @@ export function getArtifactsOfTypes<T extends Artifact['type'][]>(
(!predicate ||
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'][]>(
{
key,
types,
}: {
key: string
key: ArtifactId
types: T
},
map: ArtifactGraph
Expand Down Expand Up @@ -718,7 +720,7 @@ export function getExtrudeEdgeCodeRef(
}

export function getExtrusionFromSuspectedExtrudeSurface(
id: string,
id: ArtifactId,
artifactGraph: ArtifactGraph
): ExtrusionArtifact | Error {
const artifact = getArtifactOfTypes(
Expand All @@ -733,7 +735,7 @@ export function getExtrusionFromSuspectedExtrudeSurface(
}

export function getExtrusionFromSuspectedPath(
id: string,
id: ArtifactId,
artifactGraph: ArtifactGraph
): ExtrusionArtifact | Error {
const path = getArtifactOfTypes({ key: id, types: ['path'] }, artifactGraph)
Expand Down

0 comments on commit 57f4e1b

Please sign in to comment.