From 756c39207eff6fdcfdb2cc1ec70cf4561f44cdb3 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sat, 16 Oct 2021 15:53:00 +0200 Subject: [PATCH] add: remove singleton pattern The module pattern induced by Lua is enough for this module use case. --- init.lua | 132 ++++++++++++++----------------------------------------- 1 file changed, 34 insertions(+), 98 deletions(-) diff --git a/init.lua b/init.lua index 321cc77..7397a64 100644 --- a/init.lua +++ b/init.lua @@ -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)