Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions packages/Realtime/Realtime.Tests/Presence/PresenceStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class PresenceStateTests
"\"payload\":{\"joins\":{\"user-2\":{\"metas\":[{\"phx_ref\":\"r2\",\"time\":\"2023-09-01T11:00:00Z\"}]}}," +
"\"leaves\":{\"user-1\":{\"metas\":[{\"phx_ref\":\"r1\"}]}}}}";

private const string SecondConnectionJoinFrame =
"{\"topic\":\"realtime:online-users\",\"event\":\"presence_diff\",\"ref\":\"s3\"," +
"\"payload\":{\"joins\":{\"user-1\":{\"metas\":[{\"phx_ref\":\"r2\"}]}},\"leaves\":{}}}";

private const string FirstConnectionLeaveFrame =
"{\"topic\":\"realtime:online-users\",\"event\":\"presence_diff\",\"ref\":\"s4\"," +
"\"payload\":{\"joins\":{},\"leaves\":{\"user-1\":{\"metas\":[{\"phx_ref\":\"r1\"}]}}}}";

private static (RealtimeChannel channel, RealtimePresence<PresenceExample> presence) Presence()
{
var channel = Wire.Channel("realtime:online-users");
Expand Down Expand Up @@ -67,6 +75,19 @@ public void HandleSocketMessage_ShouldApplyJoinsAndLeaves_GivenPresenceDiff()
}
}

[TestMethod]
public void HandleSocketMessage_ShouldTrackEachMeta_GivenTwoConnectionsOnOneKey()
{
var (channel, presence) = Presence();
channel.HandleSocketMessage(Wire.Decode(StateFrame));
channel.HandleSocketMessage(Wire.Decode(SecondConnectionJoinFrame));
presence.CurrentState["user-1"].Should().HaveCount(2, "a second connection joined the same key");

channel.HandleSocketMessage(Wire.Decode(FirstConnectionLeaveFrame));
presence.CurrentState.Should().ContainKey("user-1", "the user is still online on the other connection")
.WhoseValue.Should().ContainSingle().Which.PhoenixRef.Should().Be("r2");
}

[TestMethod]
public void TriggerDiff_ShouldThrow_GivenUnparsableResponse()
{
Expand Down
27 changes: 21 additions & 6 deletions packages/Realtime/Realtime/RealtimePresence.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Supabase.Core.Attributes;
Expand Down Expand Up @@ -225,13 +226,27 @@ private void SetState()

if (state?.Payload == null) return;

// Remove any result that has "left"
foreach (var item in state.Payload.Leaves!)
this.CurrentState.Remove(item.Key);

// Add any results that have come in.
foreach (var item in state.Payload.Joins!)
this.CurrentState[item.Key] = item.Value.Metas!;
{
var joined = item.Value.Metas!;
var joinedRefs = joined.Select(m => m.PhoenixRef).ToHashSet();
this.CurrentState[item.Key] = this.CurrentState.GetValueOrDefault(item.Key, [])
.Where(m => !joinedRefs.Contains(m.PhoenixRef))
.Concat(joined)
.ToList();
}

foreach (var item in state.Payload.Leaves!)
{
var leftRefs = item.Value.Metas!.Select(m => m.PhoenixRef).ToHashSet();
var metas = this.CurrentState.GetValueOrDefault(item.Key, [])
.Where(m => !leftRefs.Contains(m.PhoenixRef))
.ToList();
if (metas.Count > 0)
this.CurrentState[item.Key] = metas;
else
this.CurrentState.Remove(item.Key);
}
}
else
{
Expand Down
Loading