degesch: get rid of Lua timer hooks

Since they were the exception and have been replaced with the async API.
This commit is contained in:
Přemysl Eric Janouch 2016-11-04 20:20:21 +01:00
parent 0247c4667a
commit cb9f187f80
Signed by: p
GPG Key ID: B715679E3A361BE6
2 changed files with 4 additions and 50 deletions

View File

@ -9073,7 +9073,6 @@ enum lua_hook_type
XLUA_HOOK_IRC, ///< IRC hook XLUA_HOOK_IRC, ///< IRC hook
XLUA_HOOK_PROMPT, ///< Prompt hook XLUA_HOOK_PROMPT, ///< Prompt hook
XLUA_HOOK_COMPLETION, ///< Autocomplete XLUA_HOOK_COMPLETION, ///< Autocomplete
XLUA_HOOK_TIMER, ///< One-shot timer
}; };
struct lua_hook struct lua_hook
@ -9088,8 +9087,6 @@ struct lua_hook
struct irc_hook irc_hook; ///< IRC hook struct irc_hook irc_hook; ///< IRC hook
struct prompt_hook prompt_hook; ///< IRC hook struct prompt_hook prompt_hook; ///< IRC hook
struct completion_hook c_hook; ///< Autocomplete hook struct completion_hook c_hook; ///< Autocomplete hook
struct poller_timer timer; ///< Timer
} }
data; ///< Hook data data; ///< Hook data
}; };
@ -9113,9 +9110,6 @@ lua_hook_unhook (lua_State *L)
case XLUA_HOOK_COMPLETION: case XLUA_HOOK_COMPLETION:
LIST_UNLINK (hook->plugin->ctx->completion_hooks, &hook->data.hook); LIST_UNLINK (hook->plugin->ctx->completion_hooks, &hook->data.hook);
break; break;
case XLUA_HOOK_TIMER:
poller_timer_reset (&hook->data.timer);
break;
default: default:
hard_assert (!"invalid hook type"); hard_assert (!"invalid hook type");
case XLUA_HOOK_DEFUNCT: 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 * static struct lua_hook *
lua_plugin_push_hook (lua_State *L, struct lua_plugin *plugin, lua_plugin_push_hook (lua_State *L, struct lua_plugin *plugin,
int callback_index, enum lua_hook_type type, int priority) int callback_index, enum lua_hook_type type, int priority)
@ -9392,27 +9366,6 @@ lua_plugin_hook_completion (lua_State *L)
return 1; 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 #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_irc", lua_plugin_hook_irc },
{ "hook_prompt", lua_plugin_hook_prompt }, { "hook_prompt", lua_plugin_hook_prompt },
{ "hook_completion", lua_plugin_hook_completion }, { "hook_completion", lua_plugin_hook_completion },
{ "hook_timer", lua_plugin_hook_timer },
{ "setup_config", lua_plugin_setup_config }, { "setup_config", lua_plugin_setup_config },
{ "connect", lua_plugin_connect }, { "connect", lua_plugin_connect },

View File

@ -32,6 +32,7 @@ degesch.setup_config {
}, },
} }
async, await = degesch.async, coroutine.yield
degesch.hook_irc (function (hook, server, line) degesch.hook_irc (function (hook, server, line)
local msg = degesch.parse (line) local msg = degesch.parse (line)
if msg.command ~= "KICK" then return line end 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 who = msg.prefix:match ("^[^!]*")
local channel, whom = table.unpack (msg.params) local channel, whom = table.unpack (msg.params)
if who ~= whom and whom == server.user.nickname then 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) server:send ("JOIN " .. channel)
end, timeout * 1000) end)
end end
return line return line
end) end)