|
1 | 1 | import { z } from 'zod' |
2 | 2 |
|
3 | | -export const assemblyBusyCodeSchema = z.enum(['ASSEMBLY_UPLOADING']) |
| 3 | +export const assemblyBusyCodeSchema = z.enum([ |
| 4 | + 'ASSEMBLY_UPLOADING', |
| 5 | + 'ASSEMBLY_EXECUTING', |
| 6 | + 'ASSEMBLY_REPLAYING', |
| 7 | +]) |
4 | 8 |
|
5 | 9 | export const assemblyStatusOkCodeSchema = z.enum([ |
6 | 10 | 'ASSEMBLY_CANCELED', |
@@ -733,6 +737,77 @@ export function getOk(assembly: AssemblyStatus | undefined | null): string | und |
733 | 737 | : undefined |
734 | 738 | } |
735 | 739 |
|
| 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 | + |
736 | 811 | /** |
737 | 812 | * This type and these functions below are compatibility helpers for |
738 | 813 | * working with partial assembly status objects during the transition |
|
0 commit comments