microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Apps/State/TurnStateContainer.cs
68lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Apps.State; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Holds the conversation-scoped and user-scoped state for the current turn. |
| 8 | /// </summary> |
| 9 | public sealed class TurnStateContainer |
| 10 | { |
| 11 | private Func<CancellationToken, Task>? _deleteDelegate; |
| 12 | |
| 13 | /// <summary> |
| 14 | /// Gets the conversation-scoped state, shared by all users in the conversation. |
| 15 | /// Keyed by <c>Conversation.Id</c>. |
| 16 | /// </summary> |
| 17 | public TurnState ConversationState { get; } |
| 18 | |
| 19 | /// <summary> |
| 20 | /// Gets the user-scoped state, private to each user in each conversation. |
| 21 | /// Keyed by <c>Conversation.Id</c> + <c>From.Id</c>. |
| 22 | /// Returns <see langword="null"/> when the activity has no <c>From</c> field. |
| 23 | /// </summary> |
| 24 | public TurnState? UserState { get; } |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Initializes a new instance of the <see cref="TurnStateContainer"/> class. |
| 28 | /// </summary> |
| 29 | public TurnStateContainer(TurnState conversationState, TurnState? userState) |
| 30 | { |
| 31 | ArgumentNullException.ThrowIfNull(conversationState); |
| 32 | ConversationState = conversationState; |
| 33 | UserState = userState; |
| 34 | } |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Sets the delegate used to delete state from the backing store. |
| 38 | /// Called by the framework after loading state. |
| 39 | /// </summary> |
| 40 | internal void SetDeleteDelegate(Func<CancellationToken, Task> deleteDelegate) |
| 41 | { |
| 42 | _deleteDelegate = deleteDelegate; |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Deletes conversation and user state from the backing store. |
| 47 | /// The in-memory state remains accessible for the rest of the turn |
| 48 | /// but will not be persisted at end-of-turn unless new values are written. |
| 49 | /// </summary> |
| 50 | /// <param name="cancellationToken">A cancellation token.</param> |
| 51 | public async Task DeleteAsync(CancellationToken cancellationToken = default) |
| 52 | { |
| 53 | if (_deleteDelegate is null) |
| 54 | { |
| 55 | throw new InvalidOperationException( |
| 56 | "State deletion is not available. Call UseState() during service registration."); |
| 57 | } |
| 58 | |
| 59 | await _deleteDelegate(cancellationToken).ConfigureAwait(false); |
| 60 | |
| 61 | // Clear dirty flags so end-of-turn save doesn't re-persist the deleted state. |
| 62 | ConversationState.IsDirty = false; |
| 63 | if (UserState is not null) |
| 64 | { |
| 65 | UserState.IsDirty = false; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |