1 /**
2 * Copyright © Webd 2018
3 * License: MIT (https://github.com/DiamondMVC/Webd/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module webd.modules.modulepackage;
7 
8 import webd.modules.moduleaction;
9 
10 /// Wrapper for a module.
11 abstract class ModulePackage
12 {
13   private:
14   /// Collection of module action events.
15   IModuleAction[string] _actions;
16   /// Boolean determining whether the module is disabled or not.
17   bool _disabled;
18 
19   protected:
20   this() { }
21 
22   public:
23   @property
24   {
25     /// Gets a boolean determining whether the module is disabled or not.
26     bool disabled() { return _disabled; }
27 
28     /// Sets a boolean determining whether the module is disabled or not.
29     void disabled(bool disabledState)
30     {
31       _disabled = disabledState;
32     }
33   }
34 
35   /**
36   * Subscribes to an event.
37   * Params:
38   *   event = The event to subscribe to.
39   *   action = The event handler.
40   */
41   void subscribeEvent(string event, IModuleAction action)
42   {
43     _actions[event] = action;
44   }
45 
46   /**
47   * Unsubscribes from an event.
48   * Params:
49   *   event = The event to unsubscribe from.
50   */
51   void unsubscribeEvent(string event)
52   {
53     if (_actions && event in _actions)
54     {
55       _actions.remove(event);
56     }
57   }
58 
59   /**
60   * Fires an event for the module.
61   * Params:
62   *   event = The event to fire.
63   *   args =  The arguments to pass to the event handler.
64   */
65   void fireEvent(T)(string event, T args)
66   {
67     if (_disabled)
68     {
69       return;
70     }
71 
72     auto action = cast(ModuleAction!T)_actions.get(event, null);
73 
74     if (action)
75     {
76       action(args);
77     }
78   }
79 }