Skip to content

Commit a267f63

Browse files
committed
Code quality
1 parent c0a6bec commit a267f63

File tree

6 files changed

+47
-89
lines changed

6 files changed

+47
-89
lines changed

Parse/Abstractions/Platform/LiveQueries/IParseLiveQuerySubscription.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@ public interface IParseLiveQuerySubscription
1717
/// Represents the Create event for a live query subscription.
1818
/// This event is triggered when a new object matching the subscription's query is created.
1919
/// </summary>
20-
public event EventHandler<ParseLiveQueryEventArgs> Create;
20+
event EventHandler<ParseLiveQueryEventArgs> Create;
2121

2222
/// <summary>
2323
/// Represents the Enter event for a live query subscription.
2424
/// This event is triggered when an object that did not previously match the query (and was thus not part of the subscription)
2525
/// starts matching the query, typically due to an update.
2626
/// </summary>
27-
public event EventHandler<ParseLiveQueryDualEventArgs> Enter;
27+
event EventHandler<ParseLiveQueryDualEventArgs> Enter;
2828

2929
/// <summary>
3030
/// Represents the Update event for a live query subscription.
3131
/// This event is triggered when an existing object matching the subscription's query is updated.
3232
/// </summary>
33-
public event EventHandler<ParseLiveQueryDualEventArgs> Update;
33+
event EventHandler<ParseLiveQueryDualEventArgs> Update;
3434

3535
/// <summary>
3636
/// Represents the Leave event for a live query subscription.
3737
/// This event is triggered when an object that previously matched the subscription's query
3838
/// no longer matches the criteria and is removed.
3939
/// </summary>
40-
public event EventHandler<ParseLiveQueryDualEventArgs> Leave;
40+
event EventHandler<ParseLiveQueryDualEventArgs> Leave;
4141

4242
/// <summary>
4343
/// Represents the Delete event for a live query subscription.
4444
/// This event is triggered when an object matching the subscription's query is deleted.
4545
/// </summary>
46-
public event EventHandler<ParseLiveQueryEventArgs> Delete;
46+
event EventHandler<ParseLiveQueryEventArgs> Delete;
4747

4848
/// <summary>
4949
/// Updates the current live query subscription with new query parameters,

Parse/Infrastructure/Execution/TextWebSocketClient.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TextWebSocketClient : IWebSocketClient
2121
/// when establishing a connection and is used internally for operations such as sending messages
2222
/// and listening for incoming data.
2323
/// </summary>
24-
private ClientWebSocket _webSocket;
24+
private ClientWebSocket webSocket;
2525

2626
/// <summary>
2727
/// A private instance of the Task class representing the background operation
@@ -31,7 +31,7 @@ class TextWebSocketClient : IWebSocketClient
3131
/// It is initialized when the listening process starts and monitored to prevent
3232
/// multiple concurrent listeners from being created.
3333
/// </summary>
34-
private Task _listeningTask;
34+
private Task listeningTask;
3535

3636
/// <summary>
3737
/// An event triggered whenever a message is received from the WebSocket server.
@@ -52,11 +52,11 @@ class TextWebSocketClient : IWebSocketClient
5252
/// </returns>
5353
public async Task OpenAsync(string serverUri, CancellationToken cancellationToken = default)
5454
{
55-
_webSocket ??= new ClientWebSocket();
55+
webSocket ??= new ClientWebSocket();
5656

57-
if (_webSocket.State != WebSocketState.Open && _webSocket.State != WebSocketState.Connecting)
57+
if (webSocket.State != WebSocketState.Open && webSocket.State != WebSocketState.Connecting)
5858
{
59-
await _webSocket.ConnectAsync(new Uri(serverUri), cancellationToken);
59+
await webSocket.ConnectAsync(new Uri(serverUri), cancellationToken);
6060
StartListening(cancellationToken);
6161
}
6262
}
@@ -69,8 +69,8 @@ public async Task OpenAsync(string serverUri, CancellationToken cancellationToke
6969
/// <returns>
7070
/// A task representing the asynchronous operation of closing the WebSocket connection.
7171
/// </returns>
72-
public async Task CloseAsync(CancellationToken cancellationToken = default)
73-
=> await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancellationToken);
72+
public async Task CloseAsync(CancellationToken cancellationToken = default) =>
73+
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancellationToken);
7474

7575
private async Task ListenForMessages(CancellationToken cancellationToken)
7676
{
@@ -79,9 +79,9 @@ private async Task ListenForMessages(CancellationToken cancellationToken)
7979
try
8080
{
8181
while (!cancellationToken.IsCancellationRequested &&
82-
_webSocket.State == WebSocketState.Open)
82+
webSocket.State == WebSocketState.Open)
8383
{
84-
WebSocketReceiveResult result = await _webSocket.ReceiveAsync(
84+
WebSocketReceiveResult result = await webSocket.ReceiveAsync(
8585
new ArraySegment<byte>(buffer),
8686
cancellationToken);
8787

@@ -102,21 +102,20 @@ private async Task ListenForMessages(CancellationToken cancellationToken)
102102
}
103103
}
104104

105-
106105
/// <summary>
107106
/// Starts listening for incoming messages from the WebSocket connection. This method ensures that only one listener task is running at a time.
108107
/// </summary>
109108
/// <param name="cancellationToken">A cancellation token to signal the listener task to stop.</param>
110109
private void StartListening(CancellationToken cancellationToken)
111110
{
112111
// Make sure we don't start multiple listeners
113-
if (_listeningTask is { IsCompleted: false })
112+
if (listeningTask is { IsCompleted: false })
114113
{
115114
return;
116115
}
117116

118117
// Start the listener task
119-
_listeningTask = Task.Run(async () =>
118+
listeningTask = Task.Run(async () =>
120119
{
121120
if (cancellationToken.IsCancellationRequested)
122121
{
@@ -138,9 +137,9 @@ private void StartListening(CancellationToken cancellationToken)
138137
/// </returns>
139138
public async Task SendAsync(string message, CancellationToken cancellationToken = default)
140139
{
141-
if (_webSocket is not null && _webSocket.State == WebSocketState.Open)
140+
if (webSocket is not null && webSocket.State == WebSocketState.Open)
142141
{
143-
await _webSocket.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, cancellationToken);
142+
await webSocket.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, cancellationToken);
144143
}
145144
}
146145
}

Parse/Platform/LiveQueries/ParseLiveQuery.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
using System.Threading.Tasks;
77
using Parse.Abstractions.Infrastructure;
88
using Parse.Abstractions.Platform.LiveQueries;
9-
using Parse.Infrastructure.Data;
10-
using Parse.Infrastructure.Utilities;
119

1210
namespace Parse;
1311

1412
/// <summary>
15-
/// The ParseLiveQuery class allows subscribing to a Query.
13+
/// The ParseLiveQuery class provides functionality to create and manage real-time queries on the Parse Server.
14+
/// It allows tracking changes on objects of a specified class that match query constraints, such as filters
15+
/// and watched fields, delivering updates in real-time as changes occur.
1616
/// </summary>
17-
/// <typeparam name="T"></typeparam>
17+
/// <typeparam name="T">Represents the type of ParseObject that this query operates on. T must inherit from ParseObject.</typeparam>
1818
public class ParseLiveQuery<T> where T : ParseObject
1919
{
20-
2120
/// <summary>
2221
/// Serialized <see langword="where"/> clauses.
2322
/// </summary>
@@ -37,8 +36,6 @@ public class ParseLiveQuery<T> where T : ParseObject
3736

3837
internal IServiceHub Services { get; }
3938

40-
private int RequestId = 0;
41-
4239
public ParseLiveQuery(IServiceHub serviceHub, string className, IDictionary<string, object> filters, IEnumerable<string> selectedKeys = null, IEnumerable<string> watchedKeys = null)
4340
{
4441
Services = serviceHub;
@@ -61,7 +58,7 @@ public ParseLiveQuery(IServiceHub serviceHub, string className, IDictionary<stri
6158
/// but the remaining values can be null if they aren't changed in this
6259
/// composition.
6360
/// </summary>
64-
internal ParseLiveQuery(ParseLiveQuery<T> source, IEnumerable<string> watchedKeys = null, Func<IDictionary<string, object>> onCreate = null)
61+
private ParseLiveQuery(ParseLiveQuery<T> source, IEnumerable<string> watchedKeys = null)
6562
{
6663
if (source == null)
6764
{
@@ -74,13 +71,13 @@ internal ParseLiveQuery(ParseLiveQuery<T> source, IEnumerable<string> watchedKey
7471
KeySelections = source.KeySelections;
7572
KeyWatchers = source.KeyWatchers;
7673

77-
if (watchedKeys is { })
74+
if (watchedKeys is not null)
7875
{
7976
KeyWatchers = new ReadOnlyCollection<string>(MergeWatchers(watchedKeys).ToList());
8077
}
8178
}
8279

83-
HashSet<string> MergeWatchers(IEnumerable<string> keys) => new((KeyWatchers ?? Enumerable.Empty<string>()).Concat(keys));
80+
private HashSet<string> MergeWatchers(IEnumerable<string> keys) => [..(KeyWatchers ?? Enumerable.Empty<string>()).Concat(keys)];
8481

8582
/// <summary>
8683
/// Add the provided key to the watched fields of returned ParseObjects.
@@ -93,9 +90,7 @@ internal ParseLiveQuery(ParseLiveQuery<T> source, IEnumerable<string> watchedKey
9390

9491
internal IDictionary<string, object> BuildParameters()
9592
{
96-
Dictionary<string, object> result = new Dictionary<string, object>();
97-
result["className"] = ClassName;
98-
result["where"] = Filters;
93+
Dictionary<string, object> result = new Dictionary<string, object> { ["className"] = ClassName, ["where"] = Filters };
9994
if (KeySelections != null)
10095
result["keys"] = KeySelections.ToArray();
10196
if (KeyWatchers != null)
@@ -111,8 +106,6 @@ internal IDictionary<string, object> BuildParameters()
111106
/// A task representing the asynchronous subscription operation. Upon completion
112107
/// of the task, the subscription is successfully registered.
113108
/// </returns>
114-
public async Task<IParseLiveQuerySubscription> SubscribeAsync()
115-
{
116-
return await Services.LiveQueryController.SubscribeAsync(this, CancellationToken.None);
117-
}
109+
public async Task<IParseLiveQuerySubscription> SubscribeAsync() =>
110+
await Services.LiveQueryController.SubscribeAsync(this, CancellationToken.None);
118111
}

Parse/Platform/LiveQueries/ParseLiveQueryClient.cs

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

Parse/Platform/LiveQueries/ParseLiveQueryController.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public enum ParseLiveQueryState
9595
public ParseLiveQueryState State { get; private set; }
9696

9797
CancellationTokenSource ConnectionSignal { get; set; }
98-
private IDictionary<int, CancellationTokenSource> SubscriptionSignals { get; } = new Dictionary<int, CancellationTokenSource> { };
99-
private IDictionary<int, CancellationTokenSource> UnsubscriptionSignals { get; } = new Dictionary<int, CancellationTokenSource> { };
100-
private IDictionary<int, IParseLiveQuerySubscription> Subscriptions { get; set; } = new Dictionary<int, IParseLiveQuerySubscription> { };
98+
private Dictionary<int, CancellationTokenSource> SubscriptionSignals { get; } = new Dictionary<int, CancellationTokenSource> { };
99+
private Dictionary<int, CancellationTokenSource> UnsubscriptionSignals { get; } = new Dictionary<int, CancellationTokenSource> { };
100+
private Dictionary<int, IParseLiveQuerySubscription> Subscriptions { get; set; } = new Dictionary<int, IParseLiveQuerySubscription> { };
101101

102102
/// <summary>
103103
/// Initializes a new instance of the <see cref="ParseLiveQueryController"/> class.
@@ -243,10 +243,8 @@ private async Task<IDictionary<string, object>> AppendSessionToken(IDictionary<s
243243
}).ToDictionary();
244244
}
245245

246-
private async Task SendMessage(IDictionary<string, object> message, CancellationToken cancellationToken)
247-
{
246+
private async Task SendMessage(IDictionary<string, object> message, CancellationToken cancellationToken) =>
248247
await WebSocketClient.SendAsync(JsonUtilities.Encode(message), cancellationToken);
249-
}
250248

251249
private async Task OpenAsync(CancellationToken cancellationToken = default)
252250
{
@@ -263,8 +261,8 @@ private async Task OpenAsync(CancellationToken cancellationToken = default)
263261
await WebSocketClient.OpenAsync(ParseClient.Instance.Services.LiveQueryServerConnectionData.ServerURI, cancellationToken);
264262
}
265263

266-
private void WebSocketClientOnMessageReceived(object sender, string e)
267-
=> ProcessMessage(JsonUtilities.Parse(e) as IDictionary<string, object>);
264+
private void WebSocketClientOnMessageReceived(object sender, string e) =>
265+
ProcessMessage(JsonUtilities.Parse(e) as IDictionary<string, object>);
268266

269267
/// <summary>
270268
/// Initiates a connection to the live query server asynchronously.
@@ -355,13 +353,13 @@ public async Task<IParseLiveQuerySubscription> SubscribeAsync<T>(ParseLiveQuery<
355353
bool signalReceived = completionSignal.Token.WaitHandle.WaitOne(TimeOut);
356354
SubscriptionSignals.Remove(requestId);
357355
completionSignal.Dispose();
358-
if (signalReceived)
356+
if (!signalReceived)
359357
{
360-
ParseLiveQuerySubscription<T> subscription = new ParseLiveQuerySubscription<T>(liveQuery.Services, liveQuery.ClassName, requestId);
361-
Subscriptions.Add(requestId, subscription);
362-
return subscription;
358+
throw new TimeoutException();
363359
}
364-
throw new TimeoutException();
360+
ParseLiveQuerySubscription<T> subscription = new ParseLiveQuerySubscription<T>(liveQuery.Services, liveQuery.ClassName, requestId);
361+
Subscriptions.Add(requestId, subscription);
362+
return subscription;
365363
}
366364

367365
/// <summary>

Parse/Platform/LiveQueries/ParseLiveQuerySubscription.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,15 @@ public ParseLiveQuerySubscription(IServiceHub serviceHub, string className, int
6868
/// This allows adjustments to the filter or watched keys without unsubscribing
6969
/// and re-subscribing.
7070
/// </summary>
71-
/// <typeparam name="T">The type of the ParseObject associated with the subscription.</typeparam>
71+
/// <typeparam name="T1">The type of the ParseObject associated with the subscription.</typeparam>
7272
/// <param name="liveQuery">The updated live query containing new parameters that
7373
/// will replace the existing ones for this subscription.</param>
7474
/// <param name="cancellationToken">A token to monitor for cancellation requests. If triggered,
7575
/// the update process will be halted.</param>
7676
/// <returns>A task that represents the asynchronous operation of updating
7777
/// the subscription with the new query parameters.</returns>
78-
public async Task UpdateAsync<T>(ParseLiveQuery<T> liveQuery, CancellationToken cancellationToken = default) where T : ParseObject
79-
{
78+
public async Task UpdateAsync<T1>(ParseLiveQuery<T1> liveQuery, CancellationToken cancellationToken = default) where T1 : ParseObject =>
8079
await Services.LiveQueryController.UpdateSubscriptionAsync(liveQuery, RequestId, CancellationToken.None);
81-
}
8280

8381
/// <summary>
8482
/// Cancels the current live query subscription by unsubscribing from the Parse Live Query server.
@@ -87,10 +85,8 @@ public async Task UpdateAsync<T>(ParseLiveQuery<T> liveQuery, CancellationToken
8785
/// </summary>
8886
/// <param name="cancellationToken">A token to monitor for cancellation requests. If triggered, the cancellation process will halt.</param>
8987
/// <returns>A task that represents the asynchronous operation of canceling the subscription.</returns>
90-
public async Task CancelAsync(CancellationToken cancellationToken = default)
91-
{
88+
public async Task CancelAsync(CancellationToken cancellationToken = default) =>
9289
await Services.LiveQueryController.UnsubscribeAsync(RequestId, CancellationToken.None);
93-
}
9490

9591
/// <summary>
9692
/// Handles the creation event for an object that matches the subscription's query.
@@ -99,36 +95,30 @@ public async Task CancelAsync(CancellationToken cancellationToken = default)
9995
/// <param name="objectState">
10096
/// The state of the object that triggered the creation event, containing its data and metadata.
10197
/// </param>
102-
public void OnCreate(IObjectState objectState)
103-
{
98+
public void OnCreate(IObjectState objectState) =>
10499
Create?.Invoke(this, new ParseLiveQueryEventArgs(Services.GenerateObjectFromState<T>(objectState, ClassName)));
105-
}
106100

107101
/// <summary>
108102
/// Handles the event when an object enters the result set of a live query subscription. This occurs when an
109103
/// object begins to satisfy the query conditions.
110104
/// </summary>
111105
/// <param name="objectState">The current state of the object that has entered the query result set.</param>
112106
/// <param name="originalState">The original state of the object before entering the query result set.</param>
113-
public void OnEnter(IObjectState objectState, IObjectState originalState)
114-
{
107+
public void OnEnter(IObjectState objectState, IObjectState originalState) =>
115108
Enter?.Invoke(this, new ParseLiveQueryDualEventArgs(
116109
Services.GenerateObjectFromState<T>(objectState, ClassName),
117110
Services.GenerateObjectFromState<T>(originalState, ClassName)));
118-
}
119111

120112
/// <summary>
121113
/// Handles the update event for objects subscribed to the Live Query. This method triggers the Update
122114
/// event, providing the updated object and its original state.
123115
/// </summary>
124116
/// <param name="objectState">The new state of the object after the update.</param>
125117
/// <param name="originalState">The original state of the object before the update.</param>
126-
public void OnUpdate(IObjectState objectState, IObjectState originalState)
127-
{
118+
public void OnUpdate(IObjectState objectState, IObjectState originalState) =>
128119
Update?.Invoke(this, new ParseLiveQueryDualEventArgs(
129120
Services.GenerateObjectFromState<T>(objectState, ClassName),
130121
Services.GenerateObjectFromState<T>(originalState, ClassName)));
131-
}
132122

133123
/// <summary>
134124
/// Handles the event when an object leaves the result set of the live query subscription.
@@ -137,21 +127,17 @@ public void OnUpdate(IObjectState objectState, IObjectState originalState)
137127
/// </summary>
138128
/// <param name="objectState">The state of the object that left the result set.</param>
139129
/// <param name="originalState">The original state of the object before it left the result set.</param>
140-
public void OnLeave(IObjectState objectState, IObjectState originalState)
141-
{
130+
public void OnLeave(IObjectState objectState, IObjectState originalState) =>
142131
Leave?.Invoke(this, new ParseLiveQueryDualEventArgs(
143132
Services.GenerateObjectFromState<T>(objectState, ClassName),
144133
Services.GenerateObjectFromState<T>(originalState, ClassName)));
145-
}
146134

147135
/// <summary>
148136
/// Handles the "delete" event for a live query subscription, triggered when an object is removed
149137
/// from the query's result set. This method processes the event by invoking the associated
150138
/// delete event handler, if subscribed, with the relevant object data.
151139
/// </summary>
152140
/// <param name="objectState">The state information of the object that was deleted.</param>
153-
public void OnDelete(IObjectState objectState)
154-
{
141+
public void OnDelete(IObjectState objectState) =>
155142
Delete?.Invoke(this, new ParseLiveQueryEventArgs(Services.GenerateObjectFromState<T>(objectState, ClassName)));
156-
}
157143
}

0 commit comments

Comments
 (0)