Skip to content

Commit 39dc137

Browse files
committed
improvement: Add timeout parameter to channel rpc actions.
1 parent e6a3451 commit 39dc137

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

lib/ash_typescript/rpc/codegen.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,8 @@ defmodule AshTypescript.Rpc.Codegen do
15151515
[
15161516
" resultHandler: #{result_handler_type};",
15171517
" errorHandler?: (error: #{error_handler_type}) => void;",
1518-
" timeoutHandler?: #{timeout_handler_type};"
1518+
" timeoutHandler?: #{timeout_handler_type};",
1519+
" timeout?: number;"
15191520
]
15201521

15211522
config_type_def = "{\n#{Enum.join(config_fields, "\n")}\n}"
@@ -1532,7 +1533,7 @@ defmodule AshTypescript.Rpc.Codegen do
15321533
"""
15331534
export function #{function_name}(config: #{config_type_def}) {
15341535
config.channel
1535-
.push("validate", #{payload_def})
1536+
.push("validate", #{payload_def}, config.timeout)
15361537
.receive("ok", config.resultHandler)
15371538
.receive(
15381539
"error",
@@ -1655,7 +1656,8 @@ defmodule AshTypescript.Rpc.Codegen do
16551656
[
16561657
" resultHandler: #{result_handler_type};",
16571658
" errorHandler?: (error: #{error_handler_type}) => void;",
1658-
" timeoutHandler?: #{timeout_handler_type};"
1659+
" timeoutHandler?: #{timeout_handler_type};",
1660+
" timeout?: number;"
16591661
]
16601662

16611663
config_type_def = "{\n#{Enum.join(config_fields, "\n")}\n}"
@@ -1669,7 +1671,7 @@ defmodule AshTypescript.Rpc.Codegen do
16691671
"""
16701672
export function #{function_name}#{generic_part}(config: #{config_type_def}) {
16711673
config.channel
1672-
.push("run", #{payload_def})
1674+
.push("run", #{payload_def}, config.timeout)
16731675
.receive("ok", config.resultHandler)
16741676
.receive(
16751677
"error",

test/ts/shouldPass.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import "./shouldPass/customFetch";
1919
import "./shouldPass/fetchOptionsAdvanced";
2020
import "./shouldPass/channelOperations";
2121
import "./shouldPass/channelValidations";
22+
import "./shouldPass/channelTimeoutTest";
2223

2324
// Import Zod schema validation tests
2425
import "./zod/shouldPass/basicZodUsage";
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Channel Timeout Test - shouldPass
2+
// Tests that timeout parameter works correctly with channel functions
3+
4+
import { Channel } from "phoenix";
5+
import { listTodosChannel, validateListTodosChannel } from "../generated";
6+
7+
// Mock channel for testing
8+
declare const mockChannel: Channel;
9+
10+
// Test 1: Channel function with timeout parameter
11+
listTodosChannel({
12+
channel: mockChannel,
13+
input: {},
14+
fields: ["id", "title"],
15+
resultHandler: (result) => {
16+
if (result.success) {
17+
console.log("Success:", result.data);
18+
}
19+
},
20+
errorHandler: (error) => console.error("Error:", error),
21+
timeoutHandler: () => console.error("Timeout"),
22+
timeout: 10000, // 10 second timeout
23+
});
24+
25+
// Test 2: Channel function without timeout parameter (should still work)
26+
listTodosChannel({
27+
channel: mockChannel,
28+
input: {},
29+
fields: ["id", "title"],
30+
resultHandler: (result) => {
31+
if (result.success) {
32+
console.log("Success:", result.data);
33+
}
34+
},
35+
errorHandler: (error) => console.error("Error:", error),
36+
timeoutHandler: () => console.error("Timeout"),
37+
// timeout parameter is optional
38+
});
39+
40+
// Test 3: Validation function with timeout parameter
41+
validateListTodosChannel({
42+
channel: mockChannel,
43+
input: {},
44+
resultHandler: (result) => {
45+
if (result.success) {
46+
console.log("Validation success");
47+
}
48+
},
49+
errorHandler: (error) => console.error("Validation error:", error),
50+
timeoutHandler: () => console.error("Validation timeout"),
51+
timeout: 5000, // 5 second timeout
52+
});
53+
54+
console.log("Channel timeout tests should compile successfully!");

0 commit comments

Comments
 (0)