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