Skip to content

Commit

Permalink
Add shutdown handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftwerk28 committed Feb 15, 2023
1 parent 1f491f0 commit 4c983f3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
3 changes: 0 additions & 3 deletions examples/run.sh

This file was deleted.

13 changes: 7 additions & 6 deletions examples/simple.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
-- Switch to prev/next tab in topmost tabbed layout
local i3 = require("i3ipc")
local ipc = i3.Connection:new()
ipc.main(function(ipc)
local ret = ipc:get_inputs()
print(require("inspect")(ret))
ipc:on("window", function(_, event)
print(require("inspect")(event))
i3.main(function(ipc)
ipc:on("shutdown", function(ipc)
print("Shutdown!")
end)
ipc:on("window", function(ipc, event)
print("window event", event.container.id)
ipc:command(("exec notify-send Window '%s'"):format(event.container.name))
end)
end)
81 changes: 47 additions & 34 deletions i3ipc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ function Connection:new()
return conn
end

--
-- Private methods:
--

function Connection:_consume_events()
while true do
coroutine.yield()
Expand Down Expand Up @@ -127,21 +131,58 @@ function Connection:_on_message(msg)
end
end

function Connection:connect_socket(sockpath)
function Connection:_has_subscriptions()
for _, h in pairs(self.handlers) do
if #h > 0 then
return true
end
end
return false
end

function Connection:_stop()
self.pipe:read_stop()
self.pipe:close(function()
uv.stop()
end)
end

function Connection:_is_subscribed_to(event)
local evd = resolve_event(event)
for _, e in ipairs(evd) do
if not self.subscribed_to[e.name] then
return false
end
end
return true
end

function Connection:_connect_socket(sockpath)
sockpath = sockpath or get_sockpath()
local co = coroutine.running()
self.pipe:connect(sockpath, function()
assert(coroutine.resume(co))
end)
coroutine.yield()
self.pipe:read_start(function(err, chunk)
if err ~= nil or chunk == nil then
return
if err then
self:_stop()
elseif chunk then
self.parser:parse(chunk)
end
self.parser:parse(chunk)
end)
end

function Connection:_subscribe_shutdown()
self:once(p.EVENT.SHUTDOWN, function(ipc)
ipc:_stop()
end)
end

--
-- Public methods:
--

function Connection:send(type, payload)
local event_id = type
local msg = Parser.serialize(event_id, payload)
Expand Down Expand Up @@ -232,7 +273,8 @@ function Connection:main(callback)
s:start(signal, handle_signal)
end
coroutine.wrap(function()
self:connect_socket()
self:_connect_socket()
self:_subscribe_shutdown()
callback(self)
if self:_has_subscriptions() then
self.main_finished = true
Expand All @@ -243,35 +285,6 @@ function Connection:main(callback)
uv.run()
end

function Connection:_has_subscriptions()
for _, h in pairs(self.handlers) do
if #h > 0 then
return true
end
end
return false
end

function Connection:_stop()
self.pipe:read_stop()
local co = coroutine.running()
self.pipe:close(function()
assert(coroutine.resume(co))
end)
coroutine.yield()
uv.stop()
end

function Connection:_is_subscribed_to(event)
local evd = resolve_event(event)
for _, e in ipairs(evd) do
if not self.subscribed_to[e.name] then
return false
end
end
return true
end

local function main(fn)
local conn = Connection:new()
conn:main(fn)
Expand Down
4 changes: 0 additions & 4 deletions run-example.sh

This file was deleted.

0 comments on commit 4c983f3

Please sign in to comment.