Skip to content

Commit 98e295a

Browse files
committed
Live query server error management
1 parent 0f737a0 commit 98e295a

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed

Parse/Abstractions/Platform/LiveQueries/IParseLiveQueryController.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
4+
using Parse.Platform.LiveQueries;
35

46
namespace Parse.Abstractions.Platform.LiveQueries;
57

@@ -9,13 +11,105 @@ namespace Parse.Abstractions.Platform.LiveQueries;
911
/// </summary>
1012
public interface IParseLiveQueryController
1113
{
14+
/// <summary>
15+
/// Event triggered when an error occurs during the operation of the ParseLiveQueryController.
16+
/// </summary>
17+
/// <remarks>
18+
/// This event provides details about a live query operation failure, such as specific error messages,
19+
/// error codes, and whether automatic reconnection is recommended.
20+
/// It is raised in scenarios like:
21+
/// - Receiving an error response from the LiveQuery server.
22+
/// - Issues with subscriptions, unsubscriptions, or query updates.
23+
/// Subscribers to this event can use the provided <see cref="ParseLiveQueryErrorEventArgs"/> to
24+
/// understand the error and implement appropriate handling mechanisms.
25+
/// </remarks>
26+
public event EventHandler<ParseLiveQueryErrorEventArgs> Error;
27+
28+
/// <summary>
29+
/// Establishes a connection to the live query server asynchronously.
30+
/// </summary>
31+
/// <param name="cancellationToken">
32+
/// A cancellation token that can be used to cancel the connection process. If the token is triggered,
33+
/// the connection process will be terminated.
34+
/// </param>
35+
/// <returns>
36+
/// A task that represents the asynchronous connection operation.
37+
/// </returns>
38+
/// <exception cref="TimeoutException">
39+
/// Thrown when the connection request times out before receiving confirmation from the server.
40+
/// </exception>
1241
Task ConnectAsync(CancellationToken cancellationToken = default);
1342

43+
/// <summary>
44+
/// Subscribes to a live query, enabling real-time updates for the specified query object.
45+
/// </summary>
46+
/// <typeparam name="T">
47+
/// The type of the ParseObject associated with the live query.
48+
/// </typeparam>
49+
/// <param name="liveQuery">
50+
/// The live query instance to subscribe to. It contains details about the query and its parameters.
51+
/// </param>
52+
/// <param name="cancellationToken">
53+
/// A token to monitor for cancellation requests. It allows the operation to be canceled if requested.
54+
/// </param>
55+
/// <returns>
56+
/// An object representing the active subscription for the specified query, enabling interaction with the subscribed events and updates.
57+
/// </returns>
58+
/// <exception cref="InvalidOperationException">
59+
/// Thrown when attempting to subscribe while the live query connection is in a closed state.
60+
/// </exception>
61+
/// <exception cref="TimeoutException">
62+
/// Thrown when the subscription request times out before receiving confirmation from the server.
63+
/// </exception>
1464
Task<IParseLiveQuerySubscription> SubscribeAsync<T>(ParseLiveQuery<T> liveQuery, CancellationToken cancellationToken = default) where T : ParseObject;
1565

66+
/// <summary>
67+
/// Updates an active subscription. This method modifies the parameters of an existing subscription for a specific query.
68+
/// </summary>
69+
/// <param name="liveQuery">
70+
/// The live query object that holds the query parameters to be updated.
71+
/// </param>
72+
/// <param name="requestId">
73+
/// The unique identifier of the subscription to update.
74+
/// </param>
75+
/// <param name="cancellationToken">
76+
/// A token to monitor for cancellation requests, allowing the operation to be cancelled before completion.
77+
/// </param>
78+
/// <typeparam name="T">
79+
/// The type of the ParseObject that the query targets.
80+
/// </typeparam>
81+
/// <returns>
82+
/// A task that represents the asynchronous operation of updating the subscription.
83+
/// </returns>
1684
Task UpdateSubscriptionAsync<T>(ParseLiveQuery<T> liveQuery, int requestId, CancellationToken cancellationToken = default) where T : ParseObject;
1785

86+
/// <summary>
87+
/// Unsubscribes from a live query subscription associated with the given request identifier.
88+
/// </summary>
89+
/// <param name="requestId">
90+
/// The unique identifier of the subscription to unsubscribe from.
91+
/// </param>
92+
/// <param name="cancellationToken">
93+
/// A cancellation token that can be used to cancel the unsubscription operation before completion.
94+
/// </param>
95+
/// <returns>
96+
/// A task that represents the asynchronous unsubscription operation.
97+
/// </returns>
98+
/// <exception cref="TimeoutException">
99+
/// Thrown if the unsubscription process does not complete within the specified timeout period.
100+
/// </exception>
18101
Task UnsubscribeAsync(int requestId, CancellationToken cancellationToken = default);
19102

103+
/// <summary>
104+
/// Closes the live query connection asynchronously.
105+
/// </summary>
106+
/// <param name="cancellationToken">
107+
/// A token to monitor for cancellation requests while closing the live query connection.
108+
/// If the operation is canceled, the task will terminate early.
109+
/// </param>
110+
/// <returns>
111+
/// A task that represents the asynchronous operation of closing the live query connection.
112+
/// The task completes when the connection is fully closed and resources are cleaned up.
113+
/// </returns>
20114
Task CloseAsync(CancellationToken cancellationToken = default);
21115
}

Parse/Platform/LiveQueries/ParseLiveQueryController.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,17 @@ private void WebSocketClientOnMessageReceived(object sender, string e)
267267
=> ProcessMessage(JsonUtilities.Parse(e) as IDictionary<string, object>);
268268

269269
/// <summary>
270-
/// Establishes a connection to the live query server asynchronously. This method initiates the connection process,
271-
/// manages connection states, and handles any timeout scenarios if the connection cannot be established within the specified duration.
270+
/// Initiates a connection to the live query server asynchronously.
272271
/// </summary>
273272
/// <param name="cancellationToken">
274-
/// A cancellation token that can be used to cancel the connection process. If the token is triggered,
275-
/// the connection process will be terminated.
273+
/// A cancellation token to monitor for cancellation requests.
276274
/// </param>
277275
/// <returns>
278-
/// A task that represents the asynchronous connection operation. If the connection is successful,
279-
/// the task will complete when the connection is established. In the event of a timeout or error,
280-
/// it will throw the appropriate exception.
276+
/// A task representing the asynchronous operation.
281277
/// </returns>
278+
/// <exception cref="TimeoutException">
279+
/// Thrown if the connection attempt exceeds the specified timeout period.
280+
/// </exception>
282281
public async Task ConnectAsync(CancellationToken cancellationToken = default)
283282
{
284283
if (State == ParseLiveQueryState.Closed)

Parse/Utilities/ObjectServiceExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Parse.Infrastructure.Utilities;
1111
using Parse.Infrastructure.Data;
1212
using System.Diagnostics;
13+
using Parse.Platform.LiveQueries;
1314

1415
namespace Parse;
1516

@@ -293,9 +294,13 @@ public static ParseQuery<ParseObject> GetQuery(this IServiceHub serviceHub, stri
293294
/// </summary>
294295
/// <param name="serviceHub">The service hub instance containing the Live Query controller to manage the connection.</param>
295296
/// <returns>A task that represents the asynchronous operation of connecting to the Live Query server.</returns>
296-
public static async Task ConnectLiveQueryServerAsync(this IServiceHub serviceHub)
297+
public static async Task ConnectLiveQueryServerAsync(this IServiceHub serviceHub, EventHandler<ParseLiveQueryErrorEventArgs> onError = null)
297298
{
298299
await serviceHub.LiveQueryController.ConnectAsync();
300+
if (onError is not null)
301+
{
302+
serviceHub.LiveQueryController.Error += onError;
303+
}
299304
}
300305

301306
/// <summary>

0 commit comments

Comments
 (0)