Skip to content

Commit c1de4cd

Browse files
committed
examples
1 parent f4f6755 commit c1de4cd

19 files changed

Lines changed: 477 additions & 517 deletions

examples/_run_all.sh

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/advanced/fork-join.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,46 @@ import {
2020
import type { Task, TaskResult } from "../../src/open-api";
2121

2222
// ── Workers for parallel branches ───────────────────────────────────
23-
@worker({ taskDefName: "fj_fetch_user", registerTaskDef: true })
24-
async function fetchUser(task: Task): Promise<TaskResult> {
25-
const userId = task.inputData?.userId as string;
26-
return {
27-
status: "COMPLETED",
28-
outputData: { userId, name: "Jane Doe", email: "jane@example.com" },
29-
};
30-
}
31-
32-
@worker({ taskDefName: "fj_fetch_orders", registerTaskDef: true })
33-
async function fetchOrders(task: Task): Promise<TaskResult> {
34-
const userId = task.inputData?.userId as string;
35-
// Simulate fetching orders
36-
await new Promise((resolve) => setTimeout(resolve, 50));
37-
return {
38-
status: "COMPLETED",
39-
outputData: {
40-
userId,
41-
orders: [
42-
{ id: "ORD-1", total: 99.99 },
43-
{ id: "ORD-2", total: 149.50 },
44-
],
45-
},
46-
};
47-
}
48-
49-
@worker({ taskDefName: "fj_fetch_preferences", registerTaskDef: true })
50-
async function fetchPreferences(task: Task): Promise<TaskResult> {
51-
const userId = task.inputData?.userId as string;
52-
return {
53-
status: "COMPLETED",
54-
outputData: {
55-
userId,
56-
preferences: { theme: "dark", notifications: true, language: "en" },
57-
},
58-
};
59-
}
23+
const fetchUser = worker({ taskDefName: "fj_fetch_user", registerTaskDef: true })(
24+
async (task: Task) => {
25+
const userId = task.inputData?.userId as string;
26+
return {
27+
status: "COMPLETED",
28+
outputData: { userId, name: "Jane Doe", email: "jane@example.com" },
29+
};
30+
}
31+
);
32+
33+
const fetchOrders = worker({ taskDefName: "fj_fetch_orders", registerTaskDef: true })(
34+
async (task: Task) => {
35+
const userId = task.inputData?.userId as string;
36+
// Simulate fetching orders
37+
await new Promise((resolve) => setTimeout(resolve, 50));
38+
return {
39+
status: "COMPLETED",
40+
outputData: {
41+
userId,
42+
orders: [
43+
{ id: "ORD-1", total: 99.99 },
44+
{ id: "ORD-2", total: 149.50 },
45+
],
46+
},
47+
};
48+
}
49+
);
50+
51+
const fetchPreferences = worker({ taskDefName: "fj_fetch_preferences", registerTaskDef: true })(
52+
async (task: Task) => {
53+
const userId = task.inputData?.userId as string;
54+
return {
55+
status: "COMPLETED",
56+
outputData: {
57+
userId,
58+
preferences: { theme: "dark", notifications: true, language: "en" },
59+
},
60+
};
61+
}
62+
);
6063

6164
async function main() {
6265
const clients = await OrkesClients.from();

examples/advanced/http-poll.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import {
1919
} from "../../src/sdk";
2020
import type { Task, TaskResult } from "../../src/open-api";
2121

22-
@worker({ taskDefName: "hp_process_result", registerTaskDef: true })
23-
async function processResult(task: Task): Promise<TaskResult> {
24-
const pollResult = task.inputData?.pollResult;
25-
return {
26-
status: "COMPLETED",
27-
outputData: { processed: true, data: pollResult },
28-
};
29-
}
22+
const processResult = worker({ taskDefName: "hp_process_result", registerTaskDef: true })(
23+
async (task: Task) => {
24+
const pollResult = task.inputData?.pollResult;
25+
return {
26+
status: "COMPLETED",
27+
outputData: { processed: true, data: pollResult },
28+
};
29+
}
30+
);
3031

3132
async function main() {
3233
const clients = await OrkesClients.from();

examples/advanced/sub-workflows.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,60 @@ import {
2020
import type { Task, TaskResult } from "../../src/open-api";
2121

2222
// ── Workers ─────────────────────────────────────────────────────────
23-
@worker({ taskDefName: "sw_validate", registerTaskDef: true })
24-
async function validateOrder(task: Task): Promise<TaskResult> {
25-
const orderId = task.inputData?.orderId as string;
26-
return {
27-
status: "COMPLETED",
28-
outputData: { orderId, valid: true, validatedAt: new Date().toISOString() },
29-
};
30-
}
31-
32-
@worker({ taskDefName: "sw_charge", registerTaskDef: true })
33-
async function chargePayment(task: Task): Promise<TaskResult> {
34-
const orderId = task.inputData?.orderId as string;
35-
const amount = (task.inputData?.amount as number) ?? 0;
36-
return {
37-
status: "COMPLETED",
38-
outputData: {
39-
orderId,
40-
amount,
41-
charged: true,
42-
transactionId: `TXN-${Date.now()}`,
43-
},
44-
};
45-
}
46-
47-
@worker({ taskDefName: "sw_ship", registerTaskDef: true })
48-
async function shipOrder(task: Task): Promise<TaskResult> {
49-
const orderId = task.inputData?.orderId as string;
50-
return {
51-
status: "COMPLETED",
52-
outputData: {
53-
orderId,
54-
shipped: true,
55-
trackingNumber: `TRACK-${Date.now()}`,
56-
},
57-
};
58-
}
59-
60-
@worker({ taskDefName: "sw_notify", registerTaskDef: true })
61-
async function notifyCustomer(task: Task): Promise<TaskResult> {
62-
const orderId = task.inputData?.orderId as string;
63-
const tracking = task.inputData?.trackingNumber as string;
64-
return {
65-
status: "COMPLETED",
66-
outputData: {
67-
orderId,
68-
notified: true,
69-
message: `Order ${orderId} shipped. Tracking: ${tracking}`,
70-
},
71-
};
72-
}
23+
const validateOrder = worker({ taskDefName: "sw_validate", registerTaskDef: true })(
24+
async (task: Task) => {
25+
const orderId = task.inputData?.orderId as string;
26+
return {
27+
status: "COMPLETED",
28+
outputData: { orderId, valid: true, validatedAt: new Date().toISOString() },
29+
};
30+
}
31+
);
32+
33+
const chargePayment = worker({ taskDefName: "sw_charge", registerTaskDef: true })(
34+
async (task: Task) => {
35+
const orderId = task.inputData?.orderId as string;
36+
const amount = (task.inputData?.amount as number) ?? 0;
37+
return {
38+
status: "COMPLETED",
39+
outputData: {
40+
orderId,
41+
amount,
42+
charged: true,
43+
transactionId: `TXN-${Date.now()}`,
44+
},
45+
};
46+
}
47+
);
48+
49+
const shipOrder = worker({ taskDefName: "sw_ship", registerTaskDef: true })(
50+
async (task: Task) => {
51+
const orderId = task.inputData?.orderId as string;
52+
return {
53+
status: "COMPLETED",
54+
outputData: {
55+
orderId,
56+
shipped: true,
57+
trackingNumber: `TRACK-${Date.now()}`,
58+
},
59+
};
60+
}
61+
);
62+
63+
const notifyCustomer = worker({ taskDefName: "sw_notify", registerTaskDef: true })(
64+
async (task: Task) => {
65+
const orderId = task.inputData?.orderId as string;
66+
const tracking = task.inputData?.trackingNumber as string;
67+
return {
68+
status: "COMPLETED",
69+
outputData: {
70+
orderId,
71+
notified: true,
72+
message: `Order ${orderId} shipped. Tracking: ${tracking}`,
73+
},
74+
};
75+
}
76+
);
7377

7478
async function main() {
7579
const clients = await OrkesClients.from();

examples/advanced/sync-updates.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ import {
2020
} from "../../src/sdk";
2121
import type { Task, TaskResult } from "../../src/open-api";
2222

23-
@worker({ taskDefName: "su_check_vars", registerTaskDef: true })
24-
async function checkVars(task: Task): Promise<TaskResult> {
25-
return {
26-
status: "COMPLETED",
27-
outputData: {
28-
receivedInput: task.inputData,
29-
timestamp: new Date().toISOString(),
30-
},
31-
};
32-
}
23+
const checkVars = worker({ taskDefName: "su_check_vars", registerTaskDef: true })(
24+
async (task: Task) => {
25+
return {
26+
status: "COMPLETED",
27+
outputData: {
28+
receivedInput: task.inputData,
29+
timestamp: new Date().toISOString(),
30+
},
31+
};
32+
}
33+
);
3334

3435
async function sleep(ms: number) {
3536
return new Promise((resolve) => setTimeout(resolve, ms));

examples/advanced/wait-for-webhook.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ import {
1919
} from "../../src/sdk";
2020
import type { Task, TaskResult } from "../../src/open-api";
2121

22-
@worker({ taskDefName: "wh_process_payload", registerTaskDef: true })
23-
async function processWebhookPayload(task: Task): Promise<TaskResult> {
24-
const payload = task.inputData?.webhookPayload as Record<string, unknown>;
25-
return {
26-
status: "COMPLETED",
27-
outputData: {
28-
processed: true,
29-
paymentId: payload?.paymentId,
30-
status: payload?.status,
31-
amount: payload?.amount,
32-
},
33-
};
34-
}
22+
const processWebhookPayload = worker({ taskDefName: "wh_process_payload", registerTaskDef: true })(
23+
async (task: Task) => {
24+
const payload = task.inputData?.webhookPayload as Record<string, unknown>;
25+
return {
26+
status: "COMPLETED",
27+
outputData: {
28+
processed: true,
29+
paymentId: payload?.paymentId,
30+
status: payload?.status,
31+
amount: payload?.amount,
32+
},
33+
};
34+
}
35+
);
3536

3637
async function sleep(ms: number) {
3738
return new Promise((resolve) => setTimeout(resolve, ms));

0 commit comments

Comments
 (0)