public async Task <RevalidationState> MaybeUpdateStateAsync(Func <RevalidationState, bool> updateAction) { var internalState = await GetInternalStateAsync(); var fileReference = internalState.FileReference; var state = internalState.State; // Only update the state if the update action returns true. var originalState = new RevalidationState { IsInitialized = state.IsInitialized, IsKillswitchActive = state.IsKillswitchActive, DesiredPackageEventRate = state.DesiredPackageEventRate, }; if (!updateAction(state)) { return(originalState); } try { var accessCondition = AccessConditionWrapper.GenerateIfMatchCondition(fileReference.ContentId); using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var jsonWriter = new JsonTextWriter(writer)) { _serializer.Serialize(jsonWriter, state); jsonWriter.Flush(); stream.Position = 0; await _storage.SaveFileAsync(CoreConstants.Folders.RevalidationFolderName, StateFileName, stream, accessCondition); } return(state); } catch (StorageException e) when(e.IsPreconditionFailedException()) { throw new InvalidOperationException("Failed to update the state blob since the access condition failed", e); } }
public InternalState(IFileReference fileReference, RevalidationState state) { FileReference = fileReference ?? throw new ArgumentNullException(nameof(fileReference)); State = state ?? throw new ArgumentNullException(nameof(state)); }