-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsessions.lua
62 lines (50 loc) · 1.4 KB
/
sessions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- luacheck: globals vim
local connections = require("acid.connections")
local ops = require("acid.ops")
local utils = require("acid.utils")
local core = require("acid.core")
local pwd_to_key = function(pwd)
if not utils.ends_with(pwd, "/") then
return pwd .. "/"
end
return pwd
end
local sessions = {}
sessions.store = {}
sessions.register_session = function(connection_ix, session)
if sessions.store[connection_ix] == nil then
sessions.store[connection_ix] = {
list = {},
selected = nil
}
end
table.insert(sessions.store[connection_ix].list, session)
sessions.store[connection_ix].selected = session
end
sessions.filter = function(data, connection_ix)
connection_ix = connection_ix or connections.peek()
local session = sessions.store[connection_ix]
if session ~= nil then
data.session = session.selected
end
return data
end
sessions.new_session = function(connection_ix)
connection_ix = connection_ix or connections.peek()
local handler = function(data)
sessions.register_session(connection_ix, data['new-session'])
end
local conn = connections.store[connection_ix]
local clone = ops.clone{}
core.send(conn, clone.payload(), handler)
end
sessions.reverse_lookup = function(session_id)
for k, v in pairs(sessions.store) do
for _, v2 in ipairs(v.list) do
if v2 == session_id then
return k
end
end
end
end
return sessions