add: remove singleton pattern

The module pattern induced by Lua is enough for this module  use case.
This commit is contained in:
Aire-One 2021-10-16 15:53:00 +02:00
parent 61cbed0c52
commit 756c39207e
1 changed files with 34 additions and 98 deletions

132
init.lua
View File

@ -6,26 +6,30 @@
-- easier to manage. -- easier to manage.
----- -----
local gtable = require 'gears.table' local gtable = require "gears.table"
-- Load global awesome components from the C API -- Load global awesome components from the C API
local capi = { local capi = {
client = _G.client, client = _G.client,
screen = _G.screen, screen = _G.screen,
tag = _G.tag tag = _G.tag,
} }
local awesome_slot = { local awesome_slot = {
mt = {}, mt = {},
_instance = nil, slots = require "awesome-slot.slots",
slots = require 'awesome-slot.slots',
static_connect = { static_connect = {
client = capi.client, client = capi.client,
screen = capi.screen, screen = capi.screen,
tag = capi.tag, tag = capi.tag,
ruled_client = require 'ruled.client', ruled_client = require "ruled.client",
ruled_notification = require 'ruled.notification' ruled_notification = require "ruled.notification",
} },
registered_slots = {},
slot_status = {
CREATED = 1,
CONNECTED = 2,
},
} }
local function generate_id(base_id) local function generate_id(base_id)
@ -40,54 +44,24 @@ local function generate_id(base_id)
return id return id
end end
-- Singleton local function get_slot(slot)
function awesome_slot.instance() assert(slot)
if not awesome_slot._instance then local id = type(slot) == "string" and slot or slot.id
awesome_slot._instance = { assert(id, "Slot not found!")
slots = {}, -- List of created slot
-- Methods
do_action = awesome_slot.do_action,
create_slot = awesome_slot.create_slot,
delete_slot = awesome_slot.delete_slot,
connect_slot = awesome_slot.connect_slot,
disconnect_slot = awesome_slot.disconnect_slot,
get_slot = awesome_slot.get_slot
}
end
return awesome_slot._instance return awesome_slot.registered_slots[id]
end end
function awesome_slot:get_slot(id) function awesome_slot.create_slot(params)
assert(id)
return self.slots[id]
end
awesome_slot.actions = {
NONE = 0,
CREATE = 1,
DELETE = 1 << 1,
CONNECT = 1 << 2,
DISCONNECT = 1 << 3
}
awesome_slot.slot_status = {
CREATED = 1,
CONNECTED = 2
}
function awesome_slot:create_slot(params)
local id = generate_id(params.id or "UNNAMED_SLOT")
local slot = {} local slot = {}
slot.id = generate_id(params.id or "UNNAMED_SLOT")
slot.target = params.target slot.target = params.target
slot.signal = params.signal slot.signal = params.signal
slot.params = params.slot_params if params.slot_params then
slot.params = params.slot_params
if slot.params then slot.callback = function()
slot.callback = function ()
params.slot(slot.params) params.slot(slot.params)
end end
else else
@ -96,35 +70,28 @@ function awesome_slot:create_slot(params)
slot.status = awesome_slot.slot_status.CREATED slot.status = awesome_slot.slot_status.CREATED
-- Insert the ne slot into the slots list -- Insert the new slot into the slots list
self.slots[id] = slot awesome_slot.registered_slots[slot.id] = slot
return slot return slot
end end
function awesome_slot:delete_slot(params) function awesome_slot.delete_slot(params)
local slot = self:get_slot(params.id) local slot = get_slot(params)
if not slot then
return false
end
-- We shouldn't delete slot if its still connected
if slot.status == awesome_slot.slot_status.CONNECTED then if slot.status == awesome_slot.slot_status.CONNECTED then
print 'please disconnnect the slot before deleting it' print "please disconnnect the slot before deleting it"
return false return false
end end
self.slots[params.id] = nil awesome_slot.registered_slots[slot.id] = nil
return true return true
end end
function awesome_slot:connect_slot(params) function awesome_slot.connect_slot(params)
local slot = self:get_slot(params.id) local slot = get_slot(params)
if not slot then
return false
end
-- Some modules expose a static connect_signals function -- Some modules expose a static connect_signals function
-- at the module level, while other tables/objects inheriting from -- at the module level, while other tables/objects inheriting from
@ -140,12 +107,8 @@ function awesome_slot:connect_slot(params)
return slot return slot
end end
function awesome_slot:disconnect_slot(params) function awesome_slot.disconnect_slot(params)
local slot = self:get_slot(params.id) local slot = get_slot(params)
if not slot then
return false
end
-- Please check the `:connect_slot` method to understand why we do this. -- Please check the `:connect_slot` method to understand why we do this.
if gtable.hasitem(awesome_slot.static_connect, slot.target) then if gtable.hasitem(awesome_slot.static_connect, slot.target) then
@ -159,35 +122,8 @@ function awesome_slot:disconnect_slot(params)
return slot return slot
end end
function awesome_slot:do_action (params) function awesome_slot.mt:__call(...) -- luacheck: ignore unused argument self
local params = params or {} -- luacheck: ignore shadowing params return awesome_slot.connect_slot(awesome_slot.create_slot(...))
local out = self
if not params.action then
return out
end
if awesome_slot.actions.DISCONNECT & params.action ~= 0 then
out = self:disconnect_slot(params)
end
if awesome_slot.actions.CREATE & params.action ~= 0 then
out = self:create_slot(params)
end
if awesome_slot.actions.DELETE & params.action ~= 0 then
out = self:delete_slot(params)
end
if awesome_slot.actions.CONNECT & params.action ~= 0 then
out = self:connect_slot(params)
end
return out
end
function awesome_slot.mt:__call(params) -- luacheck: ignore unused argument self
return awesome_slot.instance():do_action(params)
end end
return setmetatable(awesome_slot, awesome_slot.mt) return setmetatable(awesome_slot, awesome_slot.mt)