Skip to content

Commit 7766c2f

Browse files
committed
Added unwrap to wait.forToken() as well
1 parent 71d88b9 commit 7766c2f

File tree

1 file changed

+72
-64
lines changed

1 file changed

+72
-64
lines changed

packages/trigger-sdk/src/v3/wait.ts

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ export const wait = {
580580
* @param options - The options for the waitpoint token.
581581
* @returns The waitpoint token.
582582
*/
583-
forToken: async <T>(
583+
forToken: <T>(
584584
/**
585585
* The token to wait for.
586586
* This can be a string token ID or an object with an `id` property.
@@ -599,76 +599,84 @@ export const wait = {
599599
*/
600600
releaseConcurrency?: boolean;
601601
}
602-
): Promise<Prettify<WaitpointTokenTypedResult<T>>> => {
603-
const ctx = taskContext.ctx;
602+
): ManualWaitpointPromise<T> => {
603+
return new ManualWaitpointPromise<T>(async (resolve, reject) => {
604+
try {
605+
const ctx = taskContext.ctx;
604606

605-
if (!ctx) {
606-
throw new Error("wait.forToken can only be used from inside a task.run()");
607-
}
607+
if (!ctx) {
608+
throw new Error("wait.forToken can only be used from inside a task.run()");
609+
}
608610

609-
const apiClient = apiClientManager.clientOrThrow();
611+
const apiClient = apiClientManager.clientOrThrow();
610612

611-
const tokenId = typeof token === "string" ? token : token.id;
613+
const tokenId = typeof token === "string" ? token : token.id;
612614

613-
return tracer.startActiveSpan(
614-
`wait.forToken()`,
615-
async (span) => {
616-
const response = await apiClient.waitForWaitpointToken({
617-
runFriendlyId: ctx.run.id,
618-
waitpointFriendlyId: tokenId,
619-
releaseConcurrency: options?.releaseConcurrency,
620-
});
615+
const result = await tracer.startActiveSpan(
616+
`wait.forToken()`,
617+
async (span) => {
618+
const response = await apiClient.waitForWaitpointToken({
619+
runFriendlyId: ctx.run.id,
620+
waitpointFriendlyId: tokenId,
621+
releaseConcurrency: options?.releaseConcurrency,
622+
});
621623

622-
if (!response.success) {
623-
throw new Error(`Failed to wait for wait token ${tokenId}`);
624-
}
624+
if (!response.success) {
625+
throw new Error(`Failed to wait for wait token ${tokenId}`);
626+
}
625627

626-
const result = await runtime.waitUntil(tokenId);
627-
628-
const data = result.output
629-
? await conditionallyImportAndParsePacket(
630-
{ data: result.output, dataType: result.outputType ?? "application/json" },
631-
apiClient
632-
)
633-
: undefined;
634-
635-
if (result.ok) {
636-
return {
637-
ok: result.ok,
638-
output: data,
639-
} as WaitpointTokenTypedResult<T>;
640-
} else {
641-
const error = new WaitpointTimeoutError(data.message);
642-
643-
span.recordException(error);
644-
span.setStatus({
645-
code: SpanStatusCode.ERROR,
646-
});
647-
648-
return {
649-
ok: result.ok,
650-
error,
651-
} as WaitpointTokenTypedResult<T>;
652-
}
653-
},
654-
{
655-
attributes: {
656-
[SemanticInternalAttributes.STYLE_ICON]: "wait",
657-
[SemanticInternalAttributes.ENTITY_TYPE]: "waitpoint",
658-
[SemanticInternalAttributes.ENTITY_ID]: tokenId,
659-
id: tokenId,
660-
...accessoryAttributes({
661-
items: [
662-
{
663-
text: tokenId,
664-
variant: "normal",
665-
},
666-
],
667-
style: "codepath",
668-
}),
669-
},
628+
const result = await runtime.waitUntil(tokenId);
629+
630+
const data = result.output
631+
? await conditionallyImportAndParsePacket(
632+
{ data: result.output, dataType: result.outputType ?? "application/json" },
633+
apiClient
634+
)
635+
: undefined;
636+
637+
if (result.ok) {
638+
return {
639+
ok: result.ok,
640+
output: data,
641+
} as WaitpointTokenTypedResult<T>;
642+
} else {
643+
const error = new WaitpointTimeoutError(data.message);
644+
645+
span.recordException(error);
646+
span.setStatus({
647+
code: SpanStatusCode.ERROR,
648+
});
649+
650+
return {
651+
ok: result.ok,
652+
error,
653+
} as WaitpointTokenTypedResult<T>;
654+
}
655+
},
656+
{
657+
attributes: {
658+
[SemanticInternalAttributes.STYLE_ICON]: "wait",
659+
[SemanticInternalAttributes.ENTITY_TYPE]: "waitpoint",
660+
[SemanticInternalAttributes.ENTITY_ID]: tokenId,
661+
id: tokenId,
662+
...accessoryAttributes({
663+
items: [
664+
{
665+
text: tokenId,
666+
variant: "normal",
667+
},
668+
],
669+
style: "codepath",
670+
}),
671+
},
672+
}
673+
);
674+
675+
resolve(result);
676+
} catch (error) {
677+
reject(error);
670678
}
671-
);
679+
});
672680
},
673681
/**
674682
* This allows you to start some work on another API (or one of your own services)

0 commit comments

Comments
 (0)