73 lines
1.7 KiB
C#
73 lines
1.7 KiB
C#
using Orleans.Concurrency;
|
|
|
|
namespace Pushy.Grains;
|
|
|
|
public sealed class TextItem
|
|
{
|
|
public string? Text { get; set; }
|
|
}
|
|
|
|
public interface ITextItem : IGrainWithStringKey
|
|
{
|
|
[Alias("IsAvailable")]
|
|
public ValueTask<bool> IsAvailable();
|
|
|
|
[Alias("SetText")]
|
|
public ValueTask SetText(string text);
|
|
|
|
[ReadOnly]
|
|
[return: Immutable]
|
|
[AlwaysInterleave]
|
|
[Alias("GetText")]
|
|
ValueTask<string> GetText();
|
|
}
|
|
|
|
public sealed class TextItemGrain : Grain, ITextItem, IRemindable
|
|
{
|
|
private readonly IPersistentState<TextItem> _state;
|
|
private IGrainReminder _reminder;
|
|
|
|
public TextItemGrain(
|
|
[PersistentState("text")] IPersistentState<TextItem> state)
|
|
{
|
|
_state = state;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async Task OnActivateAsync(CancellationToken cancellationToken)
|
|
{
|
|
_reminder = await this.RegisterOrUpdateReminder("clear", TimeSpan.FromDays(10), TimeSpan.FromDays(10));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask<bool> IsAvailable()
|
|
{
|
|
return ValueTask.FromResult(_state.State.Text is null);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async ValueTask SetText(string text)
|
|
{
|
|
if(_state.State.Text is not null)
|
|
throw new InvalidOperationException("Text is already set");
|
|
|
|
_state.State.Text = text;
|
|
await _state.WriteStateAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask<string> GetText()
|
|
{
|
|
return ValueTask.FromResult(_state.State.Text!);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task ReceiveReminder(string reminderName, TickStatus status)
|
|
{
|
|
if (reminderName == "clear")
|
|
{
|
|
_state.State.Text = null;
|
|
await _state.WriteStateAsync();
|
|
}
|
|
}
|
|
} |