Skip to content

Commit 612b2e0

Browse files
Merge pull request #1027 from appwrite/fix-pong-null-safety
fix: added null checks for message.data & pong type checking
2 parents c385fa7 + b0b797e commit 612b2e0

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

templates/android/library/src/main/java/io/package/models/RealtimeModels.kt.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ data class RealtimeCallback(
1717

1818
open class RealtimeResponse(
1919
val type: String,
20-
val data: Any
20+
val data: Any?
2121
)
2222

2323
data class RealtimeResponseEvent<T>(

templates/android/library/src/main/java/io/package/services/Realtime.kt.twig

+3-2
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,17 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
175175
when (message.type) {
176176
TYPE_ERROR -> handleResponseError(message)
177177
TYPE_EVENT -> handleResponseEvent(message)
178+
TYPE_PONG -> {}
178179
}
179180
}
180181
}
181182

182183
private fun handleResponseError(message: RealtimeResponse) {
183-
throw message.data.jsonCast<{{ spec.title | caseUcfirst }}Exception>()
184+
throw message.data?.jsonCast<{{ spec.title | caseUcfirst }}Exception>() ?: RuntimeException("Data is not present")
184185
}
185186

186187
private suspend fun handleResponseEvent(message: RealtimeResponse) {
187-
val event = message.data.jsonCast<RealtimeResponseEvent<Any>>()
188+
val event = message.data?.jsonCast<RealtimeResponseEvent<Any>>() ?: return
188189
if (event.channels.isEmpty()) {
189190
return
190191
}

templates/flutter/lib/src/realtime_response.dart.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RealtimeResponse {
3030
factory RealtimeResponse.fromMap(Map<String, dynamic> map) {
3131
return RealtimeResponse(
3232
type: map['type'],
33-
data: Map<String, dynamic>.from(map['data']),
33+
data: Map<String, dynamic>.from(map['data'] ?? {}),
3434
);
3535
}
3636

templates/react-native/src/client.ts.twig

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Headers = {
1111
}
1212

1313
type RealtimeResponse = {
14-
type: 'error' | 'event' | 'connected' | 'response';
15-
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
14+
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
15+
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
1616
}
1717

1818
type RealtimeRequest = {
@@ -301,6 +301,8 @@ class Client {
301301
})
302302
}
303303
break;
304+
case 'pong':
305+
break; // Handle pong response if needed
304306
case 'error':
305307
throw message.data;
306308
default:

templates/web/src/client.ts.twig

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ type Headers = {
1919
*/
2020
type RealtimeResponse = {
2121
/**
22-
* Type of the response: 'error', 'event', 'connected', 'pong', or 'response'.
22+
* Type of the response: 'error', 'event', 'connected', 'response' or 'pong'.
2323
*/
2424
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
2525

2626
/**
2727
* Data associated with the response based on the response type.
2828
*/
29-
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
29+
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
3030
}
3131

3232
/**
@@ -496,6 +496,8 @@ class Client {
496496
})
497497
}
498498
break;
499+
case 'pong':
500+
break; // Handle pong response if needed
499501
case 'error':
500502
throw message.data;
501503
default:

0 commit comments

Comments
 (0)