Skip to content

Commit 8740db2

Browse files
committed
ParseLiveQueryController initialization
1 parent 0932e35 commit 8740db2

File tree

13 files changed

+126
-51
lines changed

13 files changed

+126
-51
lines changed

Parse/Abstractions/Infrastructure/CustomServiceHub.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public abstract class CustomServiceHub : ICustomServiceHub
3232

3333
public virtual IParseCommandRunner CommandRunner => Services.CommandRunner;
3434

35+
public virtual IWebSocketClient WebSocketClient => Services.WebSocketClient;
36+
3537
public virtual IParseCloudCodeController CloudCodeController => Services.CloudCodeController;
3638

3739
public virtual IParseConfigurationController ConfigurationController => Services.ConfigurationController;
@@ -62,7 +64,7 @@ public abstract class CustomServiceHub : ICustomServiceHub
6264

6365
public virtual IServerConnectionData ServerConnectionData => Services.ServerConnectionData;
6466

65-
public virtual IServerConnectionData LiveQueryServerConnectionData => Services.ServerConnectionData;
67+
public virtual IServerConnectionData LiveQueryServerConnectionData => Services.LiveQueryServerConnectionData;
6668

6769
public virtual IParseDataDecoder Decoder => Services.Decoder;
6870

Parse/Abstractions/Infrastructure/IMutableServiceHub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Parse.Abstractions.Infrastructure;
1919
public interface IMutableServiceHub : IServiceHub
2020
{
2121
IServerConnectionData ServerConnectionData { set; }
22+
IServerConnectionData LiveQueryServerConnectionData { set; }
2223
IMetadataController MetadataController { set; }
2324

2425
IServiceHubCloner Cloner { set; }
@@ -31,6 +32,7 @@ public interface IMutableServiceHub : IServiceHub
3132

3233
IParseInstallationController InstallationController { set; }
3334
IParseCommandRunner CommandRunner { set; }
35+
IWebSocketClient WebSocketClient { set; }
3436

3537
IParseCloudCodeController CloudCodeController { set; }
3638
IParseConfigurationController ConfigurationController { set; }

Parse/Abstractions/Infrastructure/IServiceHub.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public interface IServiceHub
4040

4141
IParseInstallationController InstallationController { get; }
4242
IParseCommandRunner CommandRunner { get; }
43+
IWebSocketClient WebSocketClient { get; }
4344

4445
IParseCloudCodeController CloudCodeController { get; }
4546
IParseConfigurationController ConfigurationController { get; }

Parse/Infrastructure/Execution/TextWebSocketClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ public async Task OpenAsync(string serverUri, CancellationToken cancellationToke
5454
{
5555
_webSocket ??= new ClientWebSocket();
5656

57+
Debug.WriteLine($"Status: {_webSocket.State.ToString()}");
5758
if (_webSocket.State != WebSocketState.Open && _webSocket.State != WebSocketState.Connecting)
5859
{
60+
Debug.WriteLine($"Connecting to: {serverUri}");
5961
await _webSocket.ConnectAsync(new Uri(serverUri), cancellationToken);
62+
Debug.WriteLine($"Status: {_webSocket.State.ToString()}");
6063
StartListening(cancellationToken);
6164
}
6265
}
@@ -138,7 +141,11 @@ private void StartListening(CancellationToken cancellationToken)
138141
/// </returns>
139142
public async Task SendAsync(string message, CancellationToken cancellationToken = default)
140143
{
144+
Debug.WriteLine($"Sending: {message}");
141145
if (_webSocket is not null && _webSocket.State == WebSocketState.Open)
146+
{
142147
await _webSocket.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, cancellationToken);
148+
Console.WriteLine("Sent");
149+
}
143150
}
144151
}

Parse/Infrastructure/LateInitializedMutableServiceHub.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ public IWebClient WebClient
4848
set => LateInitializer.SetValue(value);
4949
}
5050

51-
public IWebSocketClient WebSocketClient
52-
{
53-
get => LateInitializer.GetValue<IWebSocketClient>(() => new TextWebSocketClient { });
54-
set => LateInitializer.SetValue(value);
55-
}
56-
5751
public ICacheController CacheController
5852
{
5953
get => LateInitializer.GetValue<ICacheController>(() => new CacheController { });
@@ -78,6 +72,12 @@ public IParseCommandRunner CommandRunner
7872
set => LateInitializer.SetValue(value);
7973
}
8074

75+
public IWebSocketClient WebSocketClient
76+
{
77+
get => LateInitializer.GetValue<IWebSocketClient>(() => new TextWebSocketClient { });
78+
set => LateInitializer.SetValue(value);
79+
}
80+
8181
public IParseCloudCodeController CloudCodeController
8282
{
8383
get => LateInitializer.GetValue<IParseCloudCodeController>(() => new ParseCloudCodeController(CommandRunner, Decoder));

Parse/Infrastructure/MutableServiceHub.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Parse.Platform.Configuration;
2121
using Parse.Platform.Files;
2222
using Parse.Platform.Installations;
23+
using Parse.Platform.LiveQueries;
2324
using Parse.Platform.Objects;
2425
using Parse.Platform.Push;
2526
using Parse.Platform.Queries;
@@ -48,6 +49,7 @@ public class MutableServiceHub : IMutableServiceHub
4849

4950
public IParseInstallationController InstallationController { get; set; }
5051
public IParseCommandRunner CommandRunner { get; set; }
52+
public IWebSocketClient WebSocketClient { get; set; }
5153

5254
public IParseCloudCodeController CloudCodeController { get; set; }
5355
public IParseConfigurationController ConfigurationController { get; set; }
@@ -68,9 +70,10 @@ public class MutableServiceHub : IMutableServiceHub
6870
public IParseCurrentInstallationController CurrentInstallationController { get; set; }
6971
public IParseInstallationDataFinalizer InstallationDataFinalizer { get; set; }
7072

71-
public MutableServiceHub SetDefaults(IServerConnectionData connectionData = default)
73+
public MutableServiceHub SetDefaults(IServerConnectionData connectionData = default, IServerConnectionData liveQueryConnectionData = default)
7274
{
7375
ServerConnectionData ??= connectionData;
76+
LiveQueryServerConnectionData ??= liveQueryConnectionData;
7477
MetadataController ??= new MetadataController
7578
{
7679
EnvironmentData = EnvironmentData.Inferred,
@@ -87,12 +90,14 @@ public MutableServiceHub SetDefaults(IServerConnectionData connectionData = defa
8790

8891
InstallationController ??= new ParseInstallationController(CacheController);
8992
CommandRunner ??= new ParseCommandRunner(WebClient, InstallationController, MetadataController, ServerConnectionData, new Lazy<IParseUserController>(() => UserController));
93+
WebSocketClient ??= new TextWebSocketClient { };
9094

9195
CloudCodeController ??= new ParseCloudCodeController(CommandRunner, Decoder);
9296
ConfigurationController ??= new ParseConfigurationController(CommandRunner, CacheController, Decoder);
9397
FileController ??= new ParseFileController(CommandRunner);
9498
ObjectController ??= new ParseObjectController(CommandRunner, Decoder, ServerConnectionData);
9599
QueryController ??= new ParseQueryController(CommandRunner, Decoder);
100+
LiveQueryController ??= new ParseLiveQueryController(WebSocketClient);
96101
SessionController ??= new ParseSessionController(CommandRunner, Decoder);
97102
UserController ??= new ParseUserController(CommandRunner, Decoder);
98103
CurrentUserController ??= new ParseCurrentUserController(CacheController, ClassController, Decoder);

Parse/Infrastructure/OrchestrationServiceHub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class OrchestrationServiceHub : IServiceHub
3434
public IParseInstallationController InstallationController => Custom.InstallationController ?? Default.InstallationController;
3535

3636
public IParseCommandRunner CommandRunner => Custom.CommandRunner ?? Default.CommandRunner;
37+
public IWebSocketClient WebSocketClient => Custom.WebSocketClient ?? Default.WebSocketClient;
3738

3839
public IParseCloudCodeController CloudCodeController => Custom.CloudCodeController ?? Default.CloudCodeController;
3940

@@ -64,6 +65,7 @@ public class OrchestrationServiceHub : IServiceHub
6465
public IParseCurrentInstallationController CurrentInstallationController => Custom.CurrentInstallationController ?? Default.CurrentInstallationController;
6566

6667
public IServerConnectionData ServerConnectionData => Custom.ServerConnectionData ?? Default.ServerConnectionData;
68+
6769
public IServerConnectionData LiveQueryServerConnectionData => Custom.LiveQueryServerConnectionData ?? Default.LiveQueryServerConnectionData;
6870

6971
public IParseDataDecoder Decoder => Custom.Decoder ?? Default.Decoder;

Parse/Infrastructure/ServiceHub.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class ServiceHub : IServiceHub
5252

5353
public IParseInstallationController InstallationController => LateInitializer.GetValue(() => new ParseInstallationController(CacheController));
5454
public IParseCommandRunner CommandRunner => LateInitializer.GetValue(() => new ParseCommandRunner(WebClient, InstallationController, MetadataController, ServerConnectionData, new Lazy<IParseUserController>(() => UserController)));
55-
5655
public IWebSocketClient WebSocketClient => LateInitializer.GetValue(() => new TextWebSocketClient { });
5756

5857
public IParseCloudCodeController CloudCodeController => LateInitializer.GetValue(() => new ParseCloudCodeController(CommandRunner, Decoder));

Parse/Platform/LiveQueries/ParseLiveQueryController.cs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,17 @@ public enum ParseLiveQueryState
8888

8989
private IDictionary<int, ParseLiveQuerySubscription> Subscriptions { get; set; } = new Dictionary<int, ParseLiveQuerySubscription> { };
9090

91+
/// <summary>
92+
/// Initializes a new instance of the <see cref="ParseLiveQueryController"/> class.
93+
/// </summary>
94+
/// <param name="webSocketClient">
95+
/// The <see cref="IWebSocketClient"/> implementation to use for the live query connection.
96+
/// </param>
97+
/// <remarks>
98+
/// This constructor is used to initialize a new instance of the <see cref="ParseLiveQueryController"/> class
9199
public ParseLiveQueryController(IWebSocketClient webSocketClient)
92100
{
101+
Debug.WriteLine("ParseLiveQueryController initialized.");
93102
WebSocketClient = webSocketClient;
94103
State = ParseLiveQueryState.Closed;
95104
}
@@ -99,7 +108,7 @@ private void ProcessMessage(IDictionary<string, object> message)
99108
int requestId;
100109
string clientId;
101110
ParseLiveQuerySubscription subscription;
102-
switch (message["op"])
111+
switch (message["op"] as string)
103112
{
104113
case "connected":
105114
State = ParseLiveQueryState.Connected;
@@ -134,17 +143,10 @@ private void ProcessMessage(IDictionary<string, object> message)
134143
break;
135144

136145
case "error":
137-
if ((bool)message["reconnect"])
138-
{
139-
ConnectAsync();
140-
}
141-
142-
ParseLiveQueryErrorEventArgs errorArgs = new ParseLiveQueryErrorEventArgs
143-
{
144-
Error = message["error"] as string,
145-
Code = Convert.ToInt32(message["code"]),
146-
Reconnected = (bool)message["reconnect"]
147-
};
146+
ParseLiveQueryErrorEventArgs errorArgs = new ParseLiveQueryErrorEventArgs(
147+
Convert.ToInt32(message["code"]),
148+
message["error"] as string,
149+
(bool) message["reconnect"]);
148150
Error?.Invoke(this, errorArgs);
149151
break;
150152

@@ -155,10 +157,7 @@ private void ProcessMessage(IDictionary<string, object> message)
155157
requestId = Convert.ToInt32(message["requestId"]);
156158
if (Subscriptions.TryGetValue(requestId, out subscription))
157159
{
158-
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs()
159-
{
160-
Object = message["object"]
161-
};
160+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(message["object"]);
162161
subscription.OnCreate(args);
163162
}
164163
}
@@ -171,11 +170,9 @@ private void ProcessMessage(IDictionary<string, object> message)
171170
requestId = Convert.ToInt32(message["requestId"]);
172171
if (Subscriptions.TryGetValue(requestId, out subscription))
173172
{
174-
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs()
175-
{
176-
Object = message["object"],
177-
Original = message["original"]
178-
};
173+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(
174+
message["object"],
175+
message["original"]);
179176
subscription.OnEnter(args);
180177
}
181178
}
@@ -188,11 +185,9 @@ private void ProcessMessage(IDictionary<string, object> message)
188185
requestId = Convert.ToInt32(message["requestId"]);
189186
if (Subscriptions.TryGetValue(requestId, out subscription))
190187
{
191-
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs()
192-
{
193-
Object = message["object"],
194-
Original = message["original"]
195-
};
188+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(
189+
message["object"],
190+
message["original"]);
196191
subscription.OnUpdate(args);
197192
}
198193
}
@@ -205,11 +200,9 @@ private void ProcessMessage(IDictionary<string, object> message)
205200
requestId = Convert.ToInt32(message["requestId"]);
206201
if (Subscriptions.TryGetValue(requestId, out subscription))
207202
{
208-
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs()
209-
{
210-
Object = message["object"],
211-
Original = message["original"]
212-
};
203+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(
204+
message["object"],
205+
message["original"]);
213206
subscription.OnLeave(args);
214207
}
215208
}
@@ -222,10 +215,7 @@ private void ProcessMessage(IDictionary<string, object> message)
222215
requestId = Convert.ToInt32(message["requestId"]);
223216
if (Subscriptions.TryGetValue(requestId, out subscription))
224217
{
225-
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs()
226-
{
227-
Object = message["object"],
228-
};
218+
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(message["object"]);
229219
subscription.OnDelete(args);
230220
}
231221
}
@@ -291,7 +281,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
291281
{
292282
{ "op", "connect" },
293283
{ "applicationId", ParseClient.Instance.Services.LiveQueryServerConnectionData.ApplicationID },
294-
{ "clientKey", ParseClient.Instance.Services.LiveQueryServerConnectionData.Key }
284+
{ "windowsKey", ParseClient.Instance.Services.LiveQueryServerConnectionData.Key }
295285
};
296286
await SendMessage(message, cancellationToken);
297287
ConnectionSignal = new CancellationTokenSource();

Parse/Platform/LiveQueries/ParseLiveQueryErrorEventArgs.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,50 @@
22

33
namespace Parse.Platform.LiveQueries;
44

5+
/// <summary>
6+
/// Represents the arguments for an error event that occurs during a live query in the Parse platform.
7+
/// </summary>
58
public class ParseLiveQueryErrorEventArgs : EventArgs
69
{
7-
public string Error { get; set; }
8-
public int Code { get; set; }
9-
public bool Reconnected { get; set; }
10+
/// <summary>
11+
/// Represents the arguments for an error event that occurs during a live query in the Parse platform.
12+
/// </summary>
13+
internal ParseLiveQueryErrorEventArgs(int code, string error, bool reconnect)
14+
{
15+
Error = error;
16+
Code = code;
17+
Reconnect = reconnect;
18+
}
19+
20+
/// <summary>
21+
/// Gets or sets the error message associated with a live query operation.
22+
/// </summary>
23+
/// <remarks>
24+
/// The <see cref="Error"/> property contains a description of the error that occurred during
25+
/// a live query operation. It can provide detailed information about the nature of the issue,
26+
/// which can be helpful for debugging or logging purposes.
27+
/// </remarks>
28+
public string Error { get; private set; }
29+
30+
/// <summary>
31+
/// Gets or sets the error code associated with a live query operation.
32+
/// </summary>
33+
/// <remarks>
34+
/// The <see cref="Code"/> property contains a numerical identifier that represents
35+
/// the type or category of the error that occurred during a live query operation.
36+
/// This is used alongside the error message to provide detailed diagnostics or logging.
37+
/// </remarks>
38+
public int Code { get; private set; }
39+
40+
/// <summary>
41+
/// Gets or sets a value indicating whether the client should attempt to reconnect
42+
/// after an error occurs during a live query operation.
43+
/// </summary>
44+
/// <remarks>
45+
/// The <see cref="Reconnect"/> property specifies whether a reconnection to the
46+
/// live query server is recommended or required following certain error events.
47+
/// This can be used to determine the client's behavior in maintaining a continuous
48+
/// connection with the server.
49+
/// </remarks>
50+
public bool Reconnect { get; private set; }
1051
}

0 commit comments

Comments
 (0)