aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Runtime/src/PluginEventRegistration.cs
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-03-09 01:48:28 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2023-03-09 01:48:28 -0500
commit5ddef0fcb742e77b99a0e17015d2eea0a1d4131a (patch)
treec1c88284b11b70d9f373215d8d54e8a168cc5700 /lib/Plugins.Runtime/src/PluginEventRegistration.cs
parentdab71d5597fdfbe71f6ac310a240835716e952a5 (diff)
Omega cache, session, and account provider complete overhaul
Diffstat (limited to 'lib/Plugins.Runtime/src/PluginEventRegistration.cs')
-rw-r--r--lib/Plugins.Runtime/src/PluginEventRegistration.cs69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/Plugins.Runtime/src/PluginEventRegistration.cs b/lib/Plugins.Runtime/src/PluginEventRegistration.cs
new file mode 100644
index 0000000..37d6162
--- /dev/null
+++ b/lib/Plugins.Runtime/src/PluginEventRegistration.cs
@@ -0,0 +1,69 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Runtime
+* File: PluginEventRegistration.cs
+*
+* PluginEventRegistration.cs is part of VNLib.Plugins.Runtime which is
+* part of the larger VNLib collection of libraries and utilities.
+*
+* VNLib.Plugins.Runtime 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.Plugins.Runtime 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.Plugins.Runtime. If not, see http://www.gnu.org/licenses/.
+*/
+
+using System;
+
+namespace VNLib.Plugins.Runtime
+{
+ /// <summary>
+ /// Holds a registration for events to a given <see cref="IPluginEventRegistrar"/>.
+ /// The event listener is unregistered from events when this registration is disposed.
+ /// </summary>
+ public readonly record struct PluginEventRegistration : IDisposable
+ {
+ private readonly IPluginEventRegistrar _registrar;
+ private readonly IPluginEventListener _listener;
+
+ internal PluginEventRegistration(IPluginEventRegistrar container, IPluginEventListener listener)
+ {
+ _listener = listener;
+ _registrar = container;
+ }
+
+ /// <summary>
+ /// Unreigsers the listner and releases held resources
+ /// </summary>
+ public readonly void Dispose()
+ {
+ _ = _registrar?.Unregister(_listener);
+ }
+
+ /// <summary>
+ /// Unregisters a previously registered <see cref="IPluginEventListener"/>
+ /// from the <see cref="PluginController"/> it was registered to
+ /// </summary>
+ /// <exception cref="InvalidOperationException"></exception>
+ public readonly void Unregister()
+ {
+ if (_registrar == null)
+ {
+ return;
+ }
+ if (!_registrar.Unregister(_listener))
+ {
+ throw new InvalidOperationException("The listner has already been unregistered");
+ }
+ }
+ }
+}