public async Task <bool> TryAddStateAsync <T>(string stateName, T value, CancellationToken cancellationToken) { ArgumentVerifier.ThrowIfNull(stateName, nameof(stateName)); EnsureStateProviderInitialized(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; // Check if the property was marked as remove in the cache if (stateMetadata.ChangeKind == StateChangeKind.Remove) { this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Update); return(true); } return(false); } if (await this.actor.Host.StateProvider.ContainsStateAsync(this.actorTypeName, this.actor.Id.ToString(), stateName, cancellationToken)) { return(false); } this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Add); return(true); }
public async Task <ConditionalValue <T> > TryGetStateAsync <T>(string stateName, CancellationToken cancellationToken) { ArgumentVerifier.ThrowIfNull(stateName, nameof(stateName)); EnsureStateProviderInitialized(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; // Check if the property was marked as remove in the cache if (stateMetadata.ChangeKind == StateChangeKind.Remove) { return(new ConditionalValue <T>(false, default)); } return(new ConditionalValue <T>(true, (T)stateMetadata.Value)); } var conditionalResult = await this.TryGetStateFromStateProviderAsync <T>(stateName, cancellationToken); if (conditionalResult.HasValue) { this.stateChangeTracker.Add(stateName, StateMetadata.Create(conditionalResult.Value, StateChangeKind.None)); } return(conditionalResult); }
public async Task SetStateAsync <T>(string stateName, T value, CancellationToken cancellationToken) { ArgumentVerifier.ThrowIfNull(stateName, nameof(stateName)); EnsureStateProviderInitialized(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; stateMetadata.Value = value; if (stateMetadata.ChangeKind == StateChangeKind.None || stateMetadata.ChangeKind == StateChangeKind.Remove) { stateMetadata.ChangeKind = StateChangeKind.Update; } } else if (await this.actor.Host.StateProvider.ContainsStateAsync(this.actorTypeName, this.actor.Id.ToString(), stateName, cancellationToken)) { this.stateChangeTracker.Add(stateName, StateMetadata.Create(value, StateChangeKind.Update)); } else { this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Add); } }
public async Task <bool> TryAddStateAsync <T>(string stateName, T value, CancellationToken cancellationToken) { Requires.Argument("stateName", stateName).NotNull(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; // Check if the property was marked as remove in the cache if (stateMetadata.ChangeKind == StateChangeKind.Remove) { this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Update); return(true); } return(false); } if (await this.stateProvider.ContainsStateAsync(this.actor.Id, stateName, cancellationToken)) { return(false); } this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Add); return(true); }
public async Task SetStateAsync <T>(string stateName, T value, CancellationToken cancellationToken) { this.ThrowIfClosed(); Requires.Argument("stateName", stateName).NotNull(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; stateMetadata.Value = value; if (stateMetadata.ChangeKind == StateChangeKind.None || stateMetadata.ChangeKind == StateChangeKind.Remove) { stateMetadata.ChangeKind = StateChangeKind.Update; } } else if (await this.stateProvider.ContainsStateAsync(this.actor.Id, stateName, cancellationToken)) { this.stateChangeTracker.Add(stateName, StateMetadata.Create(value, StateChangeKind.Update)); } else { this.stateChangeTracker[stateName] = StateMetadata.Create(value, StateChangeKind.Add); } }
public async Task <T> GetOrAddStateAsync <T>(string stateName, T value, CancellationToken cancellationToken) { var condRes = await this.TryGetStateAsync <T>(stateName, cancellationToken); if (condRes.HasValue) { return(condRes.Value); } var changeKind = this.IsStateMarkedForRemove(stateName) ? StateChangeKind.Update : StateChangeKind.Add; this.stateChangeTracker[stateName] = StateMetadata.Create(value, changeKind); return(value); }
public async Task <T> AddOrUpdateStateAsync <T>( string stateName, T addValue, Func <string, T, T> updateValueFactory, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNull(stateName, nameof(stateName)); EnsureStateProviderInitialized(); if (this.stateChangeTracker.ContainsKey(stateName)) { var stateMetadata = this.stateChangeTracker[stateName]; // Check if the property was marked as remove in the cache if (stateMetadata.ChangeKind == StateChangeKind.Remove) { this.stateChangeTracker[stateName] = StateMetadata.Create(addValue, StateChangeKind.Update); return(addValue); } var newValue = updateValueFactory.Invoke(stateName, (T)stateMetadata.Value); stateMetadata.Value = newValue; if (stateMetadata.ChangeKind == StateChangeKind.None) { stateMetadata.ChangeKind = StateChangeKind.Update; } return(newValue); } var conditionalResult = await this.TryGetStateFromStateProviderAsync <T>(stateName, cancellationToken); if (conditionalResult.HasValue) { var newValue = updateValueFactory.Invoke(stateName, conditionalResult.Value); this.stateChangeTracker.Add(stateName, StateMetadata.Create(newValue, StateChangeKind.Update)); return(newValue); } this.stateChangeTracker[stateName] = StateMetadata.Create(addValue, StateChangeKind.Add); return(addValue); }