Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragAgg5k committed Jan 23, 2025
1 parent d6611d5 commit a4b0de2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class RealtimeCallback(

open class RealtimeResponse(
val type: String,
val data: Any
val data: Any? = null
)

data class RealtimeResponseEvent<T>(
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/src/realtime_response.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RealtimeResponse {
factory RealtimeResponse.fromMap(Map<String, dynamic> map) {
return RealtimeResponse(
type: map['type'] ?? '',
data: (map.containsKey('data')) ? Map<String, dynamic>.from(map['data']) : {},
data: Map<String, dynamic>.from(map['data'] ?? {}),
);
}

Expand Down
24 changes: 13 additions & 11 deletions templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Headers = {

type RealtimeResponse = {
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
}

type RealtimeRequest = {
Expand Down Expand Up @@ -291,16 +291,18 @@ class Client {
switch (message.type) {
case 'event':
if(!message.data) return;
let data = <RealtimeResponseEvent<unknown>>message.data;
if (data?.channels) {
const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
if (!isSubscribed) return;
this.realtime.subscriptions.forEach(subscription => {
if (data.channels.some(channel => subscription.channels.includes(channel))) {
setTimeout(() => subscription.callback(data));
}
})
}

const data = message.data as RealtimeResponseEvent<unknown>;
if (!data?.channels) return;

const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
if (!isSubscribed) return;

this.realtime.subscriptions.forEach(subscription => {
if (data.channels.some(channel => subscription.channels.includes(channel))) {
setTimeout(() => subscription.callback(data));
}
});
break;
case 'pong':
break; // Handle pong response if needed
Expand Down
4 changes: 2 additions & 2 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type Headers = {
*/
type RealtimeResponse = {
/**
* Type of the response: 'error', 'event', 'connected', 'pong', or 'response'.
* Type of the response: 'error', 'event', 'connected', 'response' or 'pong'.
*/
type: 'error' | 'event' | 'connected' | 'response' | 'pong';

/**
* Data associated with the response based on the response type.
*/
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
}

/**
Expand Down

0 comments on commit a4b0de2

Please sign in to comment.