aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials.ServiceStack/src/PluginRutimeEventHandler.cs
blob: f892823daa889119f3fcacc2282e995328eac4fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.ServiceStack
* File: PluginRutimeEventHandler.cs 
*
* PluginRutimeEventHandler.cs is part of VNLib.Plugins.Essentials.ServiceStack which 
* is part of the larger VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.ServiceStack is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero 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.Essentials.ServiceStack 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/


using System.Linq;

using VNLib.Utils.Extensions;
using VNLib.Plugins.Runtime;

namespace VNLib.Plugins.Essentials.ServiceStack
{
    internal sealed class PluginRutimeEventHandler(ServiceDomain Domain) : IPluginEventListener
    {
        ///<inheritdoc/>
        void IPluginEventListener.OnPluginLoaded(PluginController controller, object? state) => OnPluginLoaded((state as IManagedPlugin)!);

        ///<inheritdoc/>
        void IPluginEventListener.OnPluginUnloaded(PluginController controller, object? state) => OnPluginUnloaded((state as IManagedPlugin)!);

        /// <summary>
        /// Called when a plugin has been successfully loaded and 
        /// should be put into service
        /// </summary>
        /// <param name="plugin">The plugin that was loaded</param>
        internal void OnPluginLoaded(IManagedPlugin plugin)
        {
            //Run onload method before invoking other handlers
            plugin.OnPluginLoaded();

            //Get event listeners at event time because deps may be modified by the domain
            ServiceGroup[] deps = Domain.ServiceGroups.Select(static d => d).ToArray();

            //run onload method
            deps.TryForeach(d => d.OnPluginLoaded(plugin));
        }

        /// <summary>
        /// Called when a plugin is about to be unloaded and should 
        /// be removed from service.
        /// </summary>
        /// <param name="plugin">The plugin instance to unload</param>
        internal void OnPluginUnloaded(IManagedPlugin plugin)
        {
            try
            {
                //Get event listeners at event time because deps may be modified by the domain
                ServiceGroup[] deps = Domain.ServiceGroups.Select(static d => d).ToArray();

                //Run unloaded method
                deps.TryForeach(d => d.OnPluginUnloaded(plugin));
            }
            finally
            {
                //always unload the plugin wrapper
                plugin.OnPluginUnloaded();
            }
        }
    }
}