Software /
code /
prosody
Comparison
core/modulemanager.lua @ 4534:7a0a31c4f6c5
modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 Jan 2012 18:49:11 +0000 |
parent | 4533:c6480d17be1e |
child | 4535:d46e9ad4fe8a |
comparison
equal
deleted
inserted
replaced
4533:c6480d17be1e | 4534:7a0a31c4f6c5 |
---|---|
46 | 46 |
47 local api = _G.require "core.moduleapi"; -- Module API container | 47 local api = _G.require "core.moduleapi"; -- Module API container |
48 | 48 |
49 local modulemap = { ["*"] = {} }; | 49 local modulemap = { ["*"] = {} }; |
50 | 50 |
51 local modulehelpers = setmetatable({}, { __index = _G }); | |
52 | |
53 local hooks = multitable_new(); | |
54 | |
55 local NULL = {}; | 51 local NULL = {}; |
56 | 52 |
57 -- Load modules when a host is activated | 53 -- Load modules when a host is activated |
58 function load_modules_for_host(host) | 54 function load_modules_for_host(host) |
59 local component = config.get(host, "core", "component_module"); | 55 local component = config.get(host, "core", "component_module"); |
98 local ok, err = call_module_method(mod, "unload"); | 94 local ok, err = call_module_method(mod, "unload"); |
99 if (not ok) and err then | 95 if (not ok) and err then |
100 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); | 96 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); |
101 end | 97 end |
102 end | 98 end |
103 -- unhook event handlers hooked by module:hook | 99 |
104 for event, handlers in pairs(hooks:get(host, name) or NULL) do | 100 for handler, event in pairs(mod.module.event_handlers) do |
105 for handler in pairs(handlers or NULL) do | 101 event.object.remove_handler(event.name, handler); |
106 (hosts[host] or prosody).events.remove_handler(event, handler); | 102 end |
107 end | 103 |
108 end | |
109 -- unhook event handlers hooked by module:hook_global | |
110 for event, handlers in pairs(hooks:get("*", name) or NULL) do | |
111 for handler in pairs(handlers or NULL) do | |
112 prosody.events.remove_handler(event, handler); | |
113 end | |
114 end | |
115 hooks:remove(host, name); | |
116 if mod.module.items then -- remove items | 104 if mod.module.items then -- remove items |
117 for key,t in pairs(mod.module.items) do | 105 for key,t in pairs(mod.module.items) do |
118 for i = #t,1,-1 do | 106 for i = #t,1,-1 do |
119 local value = t[i]; | 107 local value = t[i]; |
120 t[i] = nil; | 108 t[i] = nil; |
151 return nil, err; | 139 return nil, err; |
152 end | 140 end |
153 | 141 |
154 local _log = logger.init(host..":"..module_name); | 142 local _log = logger.init(host..":"..module_name); |
155 local api_instance = setmetatable({ name = module_name, host = host, path = err, | 143 local api_instance = setmetatable({ name = module_name, host = host, path = err, |
156 _log = _log, log = function (self, ...) return _log(...); end } | 144 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = {} } |
157 , { __index = api }); | 145 , { __index = api }); |
158 | 146 |
159 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); | 147 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
160 api_instance.environment = pluginenv; | 148 api_instance.environment = pluginenv; |
161 | 149 |