Skip to content
Open
7 changes: 7 additions & 0 deletions Assets/Plugins/StreamChat/Core/Configs/IStreamClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ public interface IStreamClientConfig
/// Does not change server history. See <see cref="StatefulModels.IStreamChannel.MessageCacheWindow"/>.
/// </summary>
MessageCacheWindow DefaultMessageCacheWindow { get; set; }

/// <summary>
/// How the client restores local state after the websocket reconnects.
/// Default is <see cref="Configs.StateRecoveryStrategy.ReplayEvents"/>.
/// See <see cref="Configs.StateRecoveryStrategy"/> for the other options.
/// </summary>
StateRecoveryStrategy StateRecoveryStrategy { get; set; }
}
}
58 changes: 58 additions & 0 deletions Assets/Plugins/StreamChat/Core/Configs/StateRecoveryStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace StreamChat.Core.Configs
{
/// <summary>
/// How <see cref="IStreamChatClient"/> restores local state after the websocket reconnects.
/// Set this on <see cref="IStreamClientConfig.StateRecoveryStrategy"/>.
/// </summary>
/// <remarks>
/// After a reconnect, the server always drops the old watches.
/// <see cref="ReplayEvents"/> and <see cref="BatchStateUpdate"/> start watching those channels again.
/// <see cref="Disabled"/> does not - you must do that yourself.
/// </remarks>
public enum StateRecoveryStrategy
{
/// <summary>
/// Default.
///
/// Missed events are replayed as normal events
/// (<see cref="StatefulModels.IStreamChannel.MessageReceived"/>,
/// <see cref="StatefulModels.IStreamChannel.ReactionAdded"/>, and so on).
/// The client also refreshes those channels and starts watching them again.
///
/// Use this if your UI updates from per-event callbacks.
/// After a long disconnect on a busy channel this can replay many events at once
/// and cause a hitch.
/// </summary>
ReplayEvents = 0,

/// <summary>
/// Same recovery as <see cref="ReplayEvents"/>, but missed events update local state
/// without raising per-event callbacks. Listen to
/// <see cref="IStreamChatClient.StateRecovered"/> and rebuild your UI from channel state
/// (<see cref="StatefulModels.IStreamChannel.Messages"/>, and similar).
///
/// Some events are still raised because they are not stored in channel state:
/// <see cref="StatefulModels.IStreamChannel.CustomEventReceived"/>,
/// <see cref="IStreamChatClient.ChannelDeleted"/>, and membership or invite notifications.
///
/// Use this if a long disconnect causes a hitch when the app resumes.
/// </summary>
BatchStateUpdate = 1,

/// <summary>
/// The SDK does not restore state after a reconnect.
/// It does not refresh channels, does not start watching them again,
/// and does not raise <see cref="IStreamChatClient.StateRecovered"/>.
/// Local state and <see cref="IStreamChatClient.WatchedChannels"/> stay as they were
/// before the disconnect.
///
/// Use this only if you handle recovery yourself. When the connection is
/// <see cref="LowLevelClient.ConnectionState.Connected"/> again, query the channels you need.
/// Example: <c>QueryChannelsAsync(new[] { ChannelFilter.Cid.In(cids) }, limit: 30)</c>.
/// That call refreshes state and starts watching.
/// Do not use <see cref="StatefulModels.IStreamChannel.WatchAsync"/> here:
/// <see cref="StatefulModels.IStreamChannel.IsWatched"/> is still true, so WatchAsync does nothing.
/// </summary>
Disabled = 2,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Plugins/StreamChat/Core/Configs/StreamClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class StreamClientConfig : IStreamClientConfig
public bool OptimisticMessageInsert { get; set; } = true;

public MessageCacheWindow DefaultMessageCacheWindow { get; set; } = null;

public StateRecoveryStrategy StateRecoveryStrategy { get; set; } = Configs.StateRecoveryStrategy.ReplayEvents;
}
}
24 changes: 24 additions & 0 deletions Assets/Plugins/StreamChat/Core/IStreamChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public interface IStreamChatClient : IDisposable, IStreamChatClientEventsListene
/// </summary>
event Action Disconnected;

/// <summary>
/// Raised once when reconnect recovery is done.
/// This includes full success, partial success, and cases with nothing to recover.
/// Not raised on the first login.
/// Not raised when <see cref="Configs.IStreamClientConfig.StateRecoveryStrategy"/> is
/// <see cref="Configs.StateRecoveryStrategy.Disabled"/>.
///
/// Channels in <see cref="StreamStateRecoveredEventArgs.Channels"/> have fresh state and are watched again.
/// Cids in <see cref="StreamStateRecoveredEventArgs.UnrecoveredChannelCids"/> are still stale and not watched.
///
/// Use this to rebuild your UI from channel state after a disconnect.
/// You need this for <see cref="Configs.StateRecoveryStrategy.BatchStateUpdate"/>,
/// because per-event callbacks do not fire during recovery.
/// It is also useful for <see cref="Configs.StateRecoveryStrategy.ReplayEvents"/>,
/// because some state updates after reconnect do not raise per-message callbacks.
/// </summary>
event StateRecoveredHandler StateRecovered;

/// <summary>
/// Event fired when connection state with Stream Chat server has changed
/// </summary>
Expand Down Expand Up @@ -129,6 +147,12 @@ public interface IStreamChatClient : IDisposable, IStreamChatClientEventsListene
/// methods may not be watched - check <see cref="IStreamChannel.IsWatched"/> on a specific
/// channel to know its state.
/// </para>
/// <para>
/// This list is cleared when the connection drops, because the server drops every watch.
/// Recovery fills it again.
/// If you read this list to restore watches yourself, do it before the disconnect,
/// or use <see cref="Configs.StateRecoveryStrategy.Disabled"/> so the list is not cleared.
/// </para>
/// </summary>
IReadOnlyList<IStreamChannel> WatchedChannels { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace StreamChat.Core.LowLevelClient
{
/// <summary>
/// Result of applying one <c>/sync</c> history batch.
/// </summary>
internal sealed class HistorySyncApplyResult
{
/// <summary>
/// <c>created_at</c> of the newest event that applied, or <c>null</c>.
/// The <c>/sync</c> <c>last_sync_at</c> cursor advances to this.
/// </summary>
public DateTimeOffset? MaxAppliedCreatedAt { get; set; }

/// <summary>
/// Events that threw while applying. Skipped, not retried in this batch.
/// </summary>
public int FailedEventCount { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading