Skip to content

Commit 8050faa

Browse files
committed
Add assembly status helpers
1 parent 65e1d29 commit 8050faa

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

packages/node/src/alphalib/types/assemblyStatus.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { z } from 'zod'
22

3-
export const assemblyBusyCodeSchema = z.enum(['ASSEMBLY_UPLOADING'])
3+
export const assemblyBusyCodeSchema = z.enum([
4+
'ASSEMBLY_UPLOADING',
5+
'ASSEMBLY_EXECUTING',
6+
'ASSEMBLY_REPLAYING',
7+
])
48

59
export const assemblyStatusOkCodeSchema = z.enum([
610
'ASSEMBLY_CANCELED',
@@ -733,6 +737,77 @@ export function getOk(assembly: AssemblyStatus | undefined | null): string | und
733737
: undefined
734738
}
735739

740+
/**
741+
* Type guard to check if a status string is a busy (in-progress) state.
742+
*/
743+
export function isAssemblyBusyStatus(
744+
status: string | undefined | null,
745+
): status is z.infer<typeof assemblyBusyCodeSchema> {
746+
return Boolean(status) && assemblyBusyCodeSchema.safeParse(status).success
747+
}
748+
749+
/**
750+
* Type guard to check if a status string is an ok (non-error) state.
751+
*/
752+
export function isAssemblyOkStatus(
753+
status: string | undefined | null,
754+
): status is z.infer<typeof assemblyStatusOkCodeSchema> {
755+
return Boolean(status) && assemblyStatusOkCodeSchema.safeParse(status).success
756+
}
757+
758+
/**
759+
* Type guard to check if a status string is an error state.
760+
*/
761+
export function isAssemblyErrorStatus(
762+
status: string | undefined | null,
763+
): status is z.infer<typeof assemblyStatusErrCodeSchema> {
764+
return Boolean(status) && assemblyStatusErrCodeSchema.safeParse(status).success
765+
}
766+
767+
/**
768+
* Type guard to check if a status string is terminal (ok, but not busy).
769+
*/
770+
export function isAssemblyTerminalOkStatus(
771+
status: string | undefined | null,
772+
): status is z.infer<typeof assemblyStatusOkCodeSchema> {
773+
return isAssemblyOkStatus(status) && !isAssemblyBusyStatus(status)
774+
}
775+
776+
/**
777+
* Returns true if the assembly is in a busy (in-progress) state.
778+
*/
779+
export function isAssemblyBusy(assembly: AssemblyStatus | undefined | null): boolean {
780+
return isAssemblyBusyStatus(getOk(assembly))
781+
}
782+
783+
/**
784+
* Returns true if the assembly is in a terminal ok state.
785+
*/
786+
export function isAssemblyTerminalOk(assembly: AssemblyStatus | undefined | null): boolean {
787+
return isAssemblyTerminalOkStatus(getOk(assembly))
788+
}
789+
790+
/**
791+
* Returns true if the assembly has a terminal error state.
792+
*/
793+
export function isAssemblyTerminalError(assembly: AssemblyStatus | undefined | null): boolean {
794+
return isAssemblyErrorStatus(getError(assembly))
795+
}
796+
797+
/**
798+
* Returns true if the assembly is terminal (ok or error).
799+
*/
800+
export function isAssemblyTerminal(assembly: AssemblyStatus | undefined | null): boolean {
801+
return isAssemblyTerminalOk(assembly) || isAssemblyTerminalError(assembly)
802+
}
803+
804+
/**
805+
* Returns true if the assembly finished successfully.
806+
*/
807+
export function isAssemblyCompleted(assembly: AssemblyStatus | undefined | null): boolean {
808+
return getOk(assembly) === 'ASSEMBLY_COMPLETED'
809+
}
810+
736811
/**
737812
* This type and these functions below are compatibility helpers for
738813
* working with partial assembly status objects during the transition

0 commit comments

Comments
 (0)