Skip to content

feat: Add livequery support #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e0d63ab
Add ParseLiveQuery and dependencies
theSlyest Jun 19, 2025
d7c8c71
Added ParseLiveQuerySubscription and refactored accordingly
theSlyest Jun 20, 2025
0932e35
Added EventArgs
theSlyest Jun 21, 2025
8740db2
ParseLiveQueryController initialization
theSlyest Jun 22, 2025
542e3cb
Subscription bug fixes
theSlyest Jun 23, 2025
40044a2
Updated event argument types
theSlyest Jun 23, 2025
0f737a0
Added DualParseLiveQueryEventArgs
theSlyest Jun 23, 2025
98e295a
Live query server error management
theSlyest Jun 23, 2025
c0a6bec
Renamed DualParseLiveQueryEventArgs to ParseLiveQueryDualEventArgs
theSlyest Jun 24, 2025
a267f63
Code quality
theSlyest Jun 25, 2025
4b83d23
Improve code quality
theSlyest Jun 25, 2025
65c73e4
Add null safety for the "where" clause extraction
theSlyest Jun 25, 2025
340f6fb
Improve code quality
theSlyest Jun 25, 2025
7e66bb6
Improvements
theSlyest Jun 25, 2025
84f7060
Null checks
theSlyest Jun 25, 2025
834ff89
Move TimeOut and BufferSize to new LiveQueryServerConnectionData and …
theSlyest Jun 27, 2025
bd36b7d
Minor improvements
theSlyest Jun 28, 2025
e9c6bcc
Improve message parsing
theSlyest Jun 30, 2025
8938a4d
Improve the retrieval of data objects from a message
theSlyest Jun 30, 2025
b65e230
Null safety and small changes
theSlyest Jun 30, 2025
cc5168c
Improve controller disposal
theSlyest Jun 30, 2025
e70789e
Fix race conditions
theSlyest Jun 30, 2025
2cfef04
Websocket exception handling
theSlyest Jun 30, 2025
97313bf
Small clean up
theSlyest Jun 30, 2025
f3374f6
Fix test error
theSlyest Jul 9, 2025
62e81fb
Fix RelationTests
theSlyest Jul 10, 2025
a76014f
Fix UserTests
theSlyest Jul 10, 2025
fbe273a
Fix RelationTests for net9.0
theSlyest Jul 10, 2025
c59316b
Add live query and live query event arg tests
theSlyest Jul 26, 2025
d9ec311
Live query event args test corrections
theSlyest Jul 26, 2025
e3b5df9
Code quality improvement
theSlyest Jul 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Parse.Tests/EncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class ParseEncoderTestClass : ParseDataEncoder
protected override IDictionary<string, object> EncodeObject(ParseObject value) => null;
}

[TestInitialize]
public void SetUp()
{
Client.Publicize();
}

[TestMethod]
public void TestIsValidType()
{
Expand Down
51 changes: 51 additions & 0 deletions Parse.Tests/LiveQueryDualEventArgsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Parse.Abstractions.Platform.Objects;
using Parse.Infrastructure;
using Parse.Platform.LiveQueries;
using Parse.Platform.Objects;

namespace Parse.Tests;

[TestClass]
public class LiveQueryDualEventArgsTests
{
private ParseClient Client { get; set; }

[TestInitialize]
public void SetUp()
{
// Initialize the client and ensure the instance is set
Client = new ParseClient(new ServerConnectionData { Test = true });
Client.Publicize();
}

[TestCleanup]
public void TearDown() => (Client.Services as ServiceHub).Reset();

[TestMethod]
public void TestParseLiveQueryDualEventArgsConstructor()
{
IObjectState state = new MutableObjectState
{
ObjectId = "waGiManPutr4Pet1r",
ClassName = "Pagi",
CreatedAt = new DateTime { },
ServerData = new Dictionary<string, object>
{
["username"] = "kevin",
["sessionToken"] = "se551onT0k3n"
}
};

ParseObject obj = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
obj.Set("test", "after");
ParseObject objOrig = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
objOrig.Set("test", "before");
ParseLiveQueryDualEventArgs args = new ParseLiveQueryDualEventArgs(obj, objOrig);

Assert.AreEqual(obj, args.Object);
Assert.AreEqual(objOrig, args.Original);
}
}
34 changes: 34 additions & 0 deletions Parse.Tests/LiveQueryErrorEventArgsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Parse.Platform.LiveQueries;

namespace Parse.Tests;

[TestClass]
public class LiveQueryErrorEventArgsTests
{
[TestMethod]
public void TestParseLiveQueryErrorEventArgsConstructor()
{
InvalidOperationException exception = new InvalidOperationException("Test exception");
ParseLiveQueryErrorEventArgs args = new ParseLiveQueryErrorEventArgs(42, "Test error", false, exception);

// Assert
Assert.AreEqual(42, args.Code);
Assert.AreEqual("Test error", args.Error);
Assert.AreEqual(false, args.Reconnect);
Assert.AreEqual(exception, args.LocalException);
}

[TestMethod]
public void TestParseLiveQueryErrorEventArgsConstructorWithoutException()
{
ParseLiveQueryErrorEventArgs args = new ParseLiveQueryErrorEventArgs(42, "Test error", true);

// Assert
Assert.AreEqual(42, args.Code);
Assert.AreEqual("Test error", args.Error);
Assert.AreEqual(true, args.Reconnect);
Assert.AreEqual(null, args.LocalException);
}
}
48 changes: 48 additions & 0 deletions Parse.Tests/LiveQueryEventArgsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Parse.Abstractions.Platform.Objects;
using Parse.Infrastructure;
using Parse.Platform.LiveQueries;
using Parse.Platform.Objects;

namespace Parse.Tests;

[TestClass]
public class LiveQueryEventArgsTests
{
private ParseClient Client { get; set; }

[TestInitialize]
public void SetUp()
{
// Initialize the client and ensure the instance is set
Client = new ParseClient(new ServerConnectionData { Test = true });
Client.Publicize();
}

[TestCleanup]
public void TearDown() => (Client.Services as ServiceHub).Reset();

[TestMethod]
public void TestParseLiveQueryEventArgsConstructor()
{
IObjectState state = new MutableObjectState
{
ObjectId = "waGiManPutr4Pet1r",
ClassName = "Pagi",
CreatedAt = new DateTime { },
ServerData = new Dictionary<string, object>
{
["username"] = "kevin",
["sessionToken"] = "se551onT0k3n"
}
};

ParseObject obj = Client.GenerateObjectFromState<ParseObject>(state, "Corgi");
ParseLiveQueryEventArgs args = new ParseLiveQueryEventArgs(obj);

// Assert
Assert.AreEqual(obj, args.Object);
}
}
73 changes: 73 additions & 0 deletions Parse.Tests/LiveQueryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Parse.Infrastructure;

namespace Parse.Tests;

[TestClass]
public class LiveQueryTests
{
public class DummyParseObject : ParseObject { }

private ParseClient Client { get; set; }

[TestInitialize]
public void SetUp()
{
// Initialize the client and ensure the instance is set
Client = new ParseClient(new ServerConnectionData { Test = true }, new LiveQueryServerConnectionData { Test = true });
Client.Publicize();
}

[TestCleanup]
public void TearDown() => (Client.Services as ServiceHub).Reset();

[TestMethod]
public void TestConstructor()
{
ParseLiveQuery<ParseObject> liveQuery = new ParseLiveQuery<ParseObject>(
Client.Services,
"DummyClass",
new Dictionary<string, object> { { "foo", "bar" } },
["foo"]);

// Assert
Assert.AreEqual("DummyClass", liveQuery.ClassName, "The ClassName property of liveQuery should be 'DummyClass'.");
IDictionary<string, object> buildParameters = liveQuery.BuildParameters();
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
Assert.IsInstanceOfType<Dictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
Assert.AreEqual("bar", ((Dictionary<string, object>)buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
Assert.AreEqual("foo", ((string[])buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
}

[TestMethod]
public void TestGetLive()
{
// Arrange
ParseQuery<ParseObject> query = Client.GetQuery("DummyClass")
.WhereEqualTo("foo", "bar")
.Select("foo");

// Act
ParseLiveQuery<ParseObject> liveQuery = query.GetLive()
.Watch("foo");

// Assert
Assert.AreEqual("DummyClass", liveQuery.ClassName, "The ClassName property of liveQuery should be 'DummyClass'.");
IDictionary<string, object> buildParameters = liveQuery.BuildParameters();
Assert.AreEqual("DummyClass", buildParameters["className"], "The ClassName property of liveQuery should be 'DummyClass'.");
Assert.IsTrue(buildParameters.ContainsKey("where"), "The 'where' key should be present in the build parameters.");
Assert.IsTrue(buildParameters.ContainsKey("keys"), "The 'keys' key should be present in the build parameters.");
Assert.IsTrue(buildParameters.ContainsKey("watch"), "The 'watch' key should be present in the build parameters.");
Assert.IsInstanceOfType<Dictionary<string, object>>(buildParameters["where"], "The 'where' parameter should be a Dictionary<string, object>.");
Assert.IsInstanceOfType<string[]>(buildParameters["keys"], "The 'keys' parameter should be a string array.");
Assert.IsInstanceOfType<string[]>(buildParameters["watch"], "The 'watch' parameter should be a string array.");
Assert.AreEqual("bar", ((Dictionary<string, object>)buildParameters["where"])["foo"], "The 'where' clause should match the query condition.");
Assert.AreEqual("foo", ((string[])buildParameters["keys"]).First(), "The 'keys' parameter should contain 'foo'.");
Assert.AreEqual("foo", ((string[])buildParameters["watch"]).First(), "The 'watch' parameter should contain 'foo'.");
}
}
8 changes: 2 additions & 6 deletions Parse.Tests/RelationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task AddRelationToUserAsync_ThrowsException_WhenUserIsNull()
public async Task AddRelationToUserAsync_ThrowsException_WhenRelationFieldIsNull()
{
var user = new ParseUser() { Username = "TestUser", Password = "TestPass", Services = Client.Services };
await user.SignUpAsync();

var relatedObjects = new List<ParseObject>
{
new ParseObject("Friend", Client.Services) { ["name"] = "Friend1" }
Expand Down Expand Up @@ -143,7 +143,6 @@ public async Task UpdateUserRelationAsync_ThrowsException_WhenUserIsNull()
public async Task UpdateUserRelationAsync_ThrowsException_WhenRelationFieldIsNull()
{
var user = new ParseUser() { Username = "TestUser", Password = "TestPass", Services = Client.Services };
await user.SignUpAsync();

var relatedObjectsToAdd = new List<ParseObject>
{
Expand All @@ -168,8 +167,6 @@ public async Task DeleteUserRelationAsync_ThrowsException_WhenUserIsNull()
public async Task DeleteUserRelationAsync_ThrowsException_WhenRelationFieldIsNull()
{
var user = new ParseUser() { Username = "TestUser", Password = "TestPass", Services = Client.Services };
await user.SignUpAsync();

await Assert.ThrowsExceptionAsync<ArgumentException>(() => UserManagement.DeleteUserRelationAsync(user, null));
}
[TestMethod]
Expand All @@ -183,7 +180,6 @@ public async Task GetUserRelationsAsync_ThrowsException_WhenUserIsNull()
public async Task GetUserRelationsAsync_ThrowsException_WhenRelationFieldIsNull()
{
var user = new ParseUser() { Username = "TestUser", Password = "TestPass", Services = Client.Services };
await user.SignUpAsync();

await Assert.ThrowsExceptionAsync<ArgumentException>(() => UserManagement.GetUserRelationsAsync(user, null));
}
Expand All @@ -196,7 +192,6 @@ public async Task AddRelationToUserAsync_ThrowsException_WhenRelatedObjectIsUnsa
{
// Arrange: Create and sign up a test user.
var user = new ParseUser() { Username = "TestUser", Password = "TestPass", Services = Client.Services };
await user.SignUpAsync();

// Create an unsaved Friend object (do NOT call SaveAsync).
var unsavedFriend = new ParseObject("Friend", Client.Services) { ["name"] = "UnsavedFriend" };
Expand All @@ -205,6 +200,7 @@ public async Task AddRelationToUserAsync_ThrowsException_WhenRelatedObjectIsUnsa
// Act & Assert: Expect an exception when trying to add an unsaved object.
await Assert.ThrowsExceptionAsync<ArgumentException>(() =>
UserManagement.AddRelationToUserAsync(user, "friends", relatedObjects));

}


Expand Down
3 changes: 2 additions & 1 deletion Parse.Tests/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public async Task TestLogOut()
// Mock LogOutAsync to ensure it can execute its logic
mockCurrentUserController
.Setup(obj => obj.LogOutAsync(It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
.CallBase(); // Use the actual LogOutAsync implementation
.Returns(Task.CompletedTask);

// Mock SessionController for session revocation
var mockSessionController = new Mock<IParseSessionController>();
Expand All @@ -182,6 +182,7 @@ public async Task TestLogOut()

// Inject mocks into ParseClient
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
user.Bind(client);

// Act: Perform logout
await client.LogOutAsync(CancellationToken.None);
Expand Down
7 changes: 7 additions & 0 deletions Parse/Abstractions/Infrastructure/CustomServiceHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Parse.Abstractions.Platform.Configuration;
using Parse.Abstractions.Platform.Files;
using Parse.Abstractions.Platform.Installations;
using Parse.Abstractions.Platform.LiveQueries;
using Parse.Abstractions.Platform.Objects;
using Parse.Abstractions.Platform.Push;
using Parse.Abstractions.Platform.Queries;
Expand All @@ -31,6 +32,8 @@ public abstract class CustomServiceHub : ICustomServiceHub

public virtual IParseCommandRunner CommandRunner => Services.CommandRunner;

public virtual IWebSocketClient WebSocketClient => Services.WebSocketClient;

public virtual IParseCloudCodeController CloudCodeController => Services.CloudCodeController;

public virtual IParseConfigurationController ConfigurationController => Services.ConfigurationController;
Expand All @@ -41,6 +44,8 @@ public abstract class CustomServiceHub : ICustomServiceHub

public virtual IParseQueryController QueryController => Services.QueryController;

public virtual IParseLiveQueryController LiveQueryController => Services.LiveQueryController;

public virtual IParseSessionController SessionController => Services.SessionController;

public virtual IParseUserController UserController => Services.UserController;
Expand All @@ -59,6 +64,8 @@ public abstract class CustomServiceHub : ICustomServiceHub

public virtual IServerConnectionData ServerConnectionData => Services.ServerConnectionData;

public virtual ILiveQueryServerConnectionData LiveQueryServerConnectionData => Services.LiveQueryServerConnectionData;

public virtual IParseDataDecoder Decoder => Services.Decoder;

public virtual IParseInstallationDataFinalizer InstallationDataFinalizer => Services.InstallationDataFinalizer;
Expand Down
71 changes: 71 additions & 0 deletions Parse/Abstractions/Infrastructure/Execution/IWebSocketClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Parse.Infrastructure.Execution;

namespace Parse.Abstractions.Infrastructure.Execution;

/// <summary>
/// Represents an interface for a WebSocket client to handle WebSocket connections and communications.
/// </summary>
public interface IWebSocketClient
{
/// <summary>
/// An event that is triggered when a message is received via the WebSocket connection.
/// </summary>
/// <remarks>
/// The event handler receives the message as a string parameter. This can be used to process incoming
/// WebSocket messages, such as notifications, commands, or data updates.
/// </remarks>
public event EventHandler<MessageReceivedEventArgs> MessageReceived;

/// <summary>
/// An event that is triggered when an error occurs during the WebSocket operation.
/// </summary>
/// <remarks>
/// This event communicates WebSocket-specific errors along with additional details encapsulated in
/// the <see cref="ErrorEventArgs"/> object. It can be used to handle and log errors during WebSocket
/// communication or connection lifecycle.
/// </remarks>
public event EventHandler<ErrorEventArgs> WebsocketError;

/// <summary>
/// An event that is triggered when an unknown or unexpected error occurs during WebSocket communication.
/// </summary>
/// <remarks>
/// This event can be used to handle errors that do not fall under typical WebSocket error events. The event
/// handler receives an <see cref="ErrorEventArgs"/> parameter containing details about the error.
/// </remarks>
public event EventHandler<ErrorEventArgs> UnknownError;

/// <summary>
/// Establishes a WebSocket connection to the specified server URI.
/// </summary>
/// <param name="serverUri">The URI of the WebSocket server to connect to.</param>
/// <param name="cancellationToken">
/// A token to observe cancellation requests. The operation will stop if the token is canceled.
/// </param>
/// <returns>A task that represents the asynchronous operation of opening the WebSocket connection.</returns>
public Task OpenAsync(string serverUri, CancellationToken cancellationToken = default);

/// <summary>
/// Closes the active WebSocket connection asynchronously.
/// </summary>
/// <param name="cancellationToken">
/// A token to observe cancellation requests. The operation will stop if the token is canceled.
/// </param>
/// <returns>A task that represents the asynchronous operation of closing the WebSocket connection.</returns>
public Task CloseAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Sends a message over the established WebSocket connection asynchronously.
/// </summary>
/// <param name="message">The message to send through the WebSocket connection.</param>
/// <param name="cancellationToken">
/// A token to observe cancellation requests. The operation will stop if the token is canceled.
/// </param>
/// <returns>A task that represents the asynchronous operation of sending the message.</returns>
/// <exception cref="InvalidOperationException">Thrown when trying to send a message on a WebSocket connection that is not in the Open state.</exception>
public Task SendAsync(string message, CancellationToken cancellationToken = default);
}
Loading
Loading