diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2016-11-04 20:20:21 +0100 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2016-11-04 20:21:46 +0100 |
commit | cb9f187f80250ed522031ef3efba866a9171e9ce (patch) | |
tree | 8c82d19391a563f6a49b1a0fbd9ad23f9b597054 | |
parent | 0247c4667a8c63c8f657f4c782fdd4a591c1e1d0 (diff) | |
download | xK-cb9f187f80250ed522031ef3efba866a9171e9ce.tar.gz xK-cb9f187f80250ed522031ef3efba866a9171e9ce.tar.xz xK-cb9f187f80250ed522031ef3efba866a9171e9ce.zip |
degesch: get rid of Lua timer hooks
Since they were the exception and have been replaced with the async API.
-rw-r--r-- | degesch.c | 48 | ||||
-rw-r--r-- | plugins/degesch/auto-rejoin.lua | 6 |
2 files changed, 4 insertions, 50 deletions
@@ -9073,7 +9073,6 @@ enum lua_hook_type XLUA_HOOK_IRC, ///< IRC hook XLUA_HOOK_PROMPT, ///< Prompt hook XLUA_HOOK_COMPLETION, ///< Autocomplete - XLUA_HOOK_TIMER, ///< One-shot timer }; struct lua_hook @@ -9088,8 +9087,6 @@ struct lua_hook struct irc_hook irc_hook; ///< IRC hook struct prompt_hook prompt_hook; ///< IRC hook struct completion_hook c_hook; ///< Autocomplete hook - - struct poller_timer timer; ///< Timer } data; ///< Hook data }; @@ -9113,9 +9110,6 @@ lua_hook_unhook (lua_State *L) case XLUA_HOOK_COMPLETION: LIST_UNLINK (hook->plugin->ctx->completion_hooks, &hook->data.hook); break; - case XLUA_HOOK_TIMER: - poller_timer_reset (&hook->data.timer); - break; default: hard_assert (!"invalid hook type"); case XLUA_HOOK_DEFUNCT: @@ -9302,26 +9296,6 @@ lua_completion_hook_complete (struct completion_hook *self, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -static void -lua_timer_hook_dispatch (void *user_data) -{ - struct lua_hook *hook = user_data; - struct lua_plugin *plugin = hook->plugin; - lua_State *L = plugin->L; - - lua_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback); - lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook - - struct error *e = NULL; - if (!lua_plugin_call (plugin, 1, 0, &e)) - lua_plugin_log_error (plugin, "timer hook", e); - - // There's no need to keep the hook around once the timer is dispatched - lua_cache_invalidate (L, hook); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static struct lua_hook * lua_plugin_push_hook (lua_State *L, struct lua_plugin *plugin, int callback_index, enum lua_hook_type type, int priority) @@ -9392,27 +9366,6 @@ lua_plugin_hook_completion (lua_State *L) return 1; } -static int -lua_plugin_hook_timer (lua_State *L) -{ - struct lua_plugin *plugin = lua_touserdata (L, lua_upvalueindex (1)); - lua_Integer timeout = luaL_checkinteger (L, 2); - - if (timeout < 0) - luaL_argerror (L, 2, "timeout mustn't be negative"); - - // This doesn't really hook anything but we can reuse the code - struct lua_hook *hook = lua_plugin_push_hook - (L, plugin, 1, XLUA_HOOK_TIMER, 0 /* priority doesn't apply */); - - struct poller_timer *timer = &hook->data.timer; - poller_timer_init (timer, &plugin->ctx->poller); - timer->dispatcher = lua_timer_hook_dispatch; - timer->user_data = hook; - poller_timer_set (timer, timeout); - return 1; -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #define XLUA_SCHEMA_METATABLE "schema" ///< Identifier for the Lua metatable @@ -10096,7 +10049,6 @@ static luaL_Reg lua_plugin_library[] = { "hook_irc", lua_plugin_hook_irc }, { "hook_prompt", lua_plugin_hook_prompt }, { "hook_completion", lua_plugin_hook_completion }, - { "hook_timer", lua_plugin_hook_timer }, { "setup_config", lua_plugin_setup_config }, { "connect", lua_plugin_connect }, diff --git a/plugins/degesch/auto-rejoin.lua b/plugins/degesch/auto-rejoin.lua index 3caa6ea..8abdb66 100644 --- a/plugins/degesch/auto-rejoin.lua +++ b/plugins/degesch/auto-rejoin.lua @@ -32,6 +32,7 @@ degesch.setup_config { }, } +async, await = degesch.async, coroutine.yield degesch.hook_irc (function (hook, server, line) local msg = degesch.parse (line) if msg.command ~= "KICK" then return line end @@ -39,9 +40,10 @@ degesch.hook_irc (function (hook, server, line) local who = msg.prefix:match ("^[^!]*") local channel, whom = table.unpack (msg.params) if who ~= whom and whom == server.user.nickname then - degesch.hook_timer (function (hook) + async.go (function () + await (async.timer_ms (timeout * 1000)) server:send ("JOIN " .. channel) - end, timeout * 1000) + end) end return line end) |