From be6dc557a3b819248b014992eb96c1cb21f8112b Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 8 Jan 2023 14:44:01 -0500 Subject: Initial commit --- Utils/src/Async/AsyncUpdatableResource.cs | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Utils/src/Async/AsyncUpdatableResource.cs (limited to 'Utils/src/Async/AsyncUpdatableResource.cs') diff --git a/Utils/src/Async/AsyncUpdatableResource.cs b/Utils/src/Async/AsyncUpdatableResource.cs new file mode 100644 index 0000000..b4ce519 --- /dev/null +++ b/Utils/src/Async/AsyncUpdatableResource.cs @@ -0,0 +1,111 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Utils +* File: AsyncUpdatableResource.cs +* +* AsyncUpdatableResource.cs is part of VNLib.Utils which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Utils is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Utils is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Utils. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.IO; +using System.Text.Json; +using System.Threading.Tasks; + +using VNLib.Utils.IO; +using VNLib.Utils.Resources; + +namespace VNLib.Utils.Async +{ + /// + /// A callback delegate used for updating a + /// + /// The to be updated + /// The serialized data to be stored/updated + /// + public delegate Task AsyncUpdateCallback(object source, Stream data); + /// + /// A callback delegate invoked when a delete is requested + /// + /// The to be deleted + /// + public delegate Task AsyncDeleteCallback(object source); + + /// + /// Implemented by a resource that is backed by an external data store, that when modified or deleted will + /// be reflected to the backing store. + /// + public abstract class AsyncUpdatableResource : BackedResourceBase, IAsyncExclusiveResource + { + protected abstract AsyncUpdateCallback UpdateCb { get; } + protected abstract AsyncDeleteCallback DeleteCb { get; } + + /// + /// Releases the resource and flushes pending changes to its backing store. + /// + /// A task that represents the async operation + /// + /// + /// + public virtual async ValueTask ReleaseAsync() + { + //If resource has already been realeased, return + if (IsReleased) + { + return; + } + //If deleted flag is set, invoke the delete callback + if (Deleted) + { + await DeleteCb(this).ConfigureAwait(true); + } + //If the state has been modifed, flush changes to the store + else if (Modified) + { + await FlushPendingChangesAsync().ConfigureAwait(true); + } + //Set the released value + IsReleased = true; + } + + /// + /// + /// Writes the current state of the the resource to the backing store + /// immediatly by invoking the specified callback. + /// + /// + /// Only call this method if your store supports multiple state updates + /// + /// + protected virtual async Task FlushPendingChangesAsync() + { + //Get the resource + object resource = GetResource(); + //Open a memory stream to store data in + using VnMemoryStream data = new(); + //Serialize and write to stream + VnEncoding.JSONSerializeToBinary(resource, data, resource.GetType(), base.JSO); + //Reset stream to begining + _ = data.Seek(0, SeekOrigin.Begin); + //Invoke update callback + await UpdateCb(this, data).ConfigureAwait(true); + //Clear modified flag + Modified = false; + } + } +} -- cgit