From 776868b430bc3a3cff9b200ed2442205bf4d9cf4 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sat, 16 Oct 2021 16:10:23 +0200 Subject: [PATCH] add: API changes - move `registered_slots` to `_private` - change `status` to `connected: boolean` - expose `get_slot` function - make the API declarative only - simplify static functions name - add a `connect` parameter to the create function --- init.lua | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/init.lua b/init.lua index 7397a64..7a145a4 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,7 @@ ----- -- AwesomeWM - Slot -- --- An OO/Declarative API to connect signals for the AwesomeWM. +-- A declarative API to connect signals for the AwesomeWM. -- It completes the native `gears.signal` module to make signal connection -- easier to manage. ----- @@ -25,10 +25,8 @@ local awesome_slot = { ruled_client = require "ruled.client", ruled_notification = require "ruled.notification", }, - registered_slots = {}, - slot_status = { - CREATED = 1, - CONNECTED = 2, + _private = { + registered_slots = {}, }, } @@ -44,20 +42,21 @@ local function generate_id(base_id) return id end -local function get_slot(slot) +function awesome_slot.get_slot(slot) assert(slot) local id = type(slot) == "string" and slot or slot.id assert(id, "Slot not found!") - return awesome_slot.registered_slots[id] + return awesome_slot._private.registered_slots[id] end -function awesome_slot.create_slot(params) +function awesome_slot.create(params) local slot = {} slot.id = generate_id(params.id or "UNNAMED_SLOT") slot.target = params.target slot.signal = params.signal + slot.connected = false if params.slot_params then slot.params = params.slot_params @@ -68,30 +67,30 @@ function awesome_slot.create_slot(params) slot.callback = params.slot end - slot.status = awesome_slot.slot_status.CREATED - -- Insert the new slot into the slots list - awesome_slot.registered_slots[slot.id] = slot + awesome_slot._private.registered_slots[slot.id] = slot + + if params.connect then + awesome_slot.connect(slot) + end return slot end -function awesome_slot.delete_slot(params) - local slot = get_slot(params) +function awesome_slot.remove(params) + local slot = awesome_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" - return false + if slot.connected then + awesome_slot.disconnect_slot(slot) end - awesome_slot.registered_slots[slot.id] = nil + awesome_slot._private.registered_slots[slot.id] = nil return true end -function awesome_slot.connect_slot(params) - local slot = get_slot(params) +function awesome_slot.connect(params) + local slot = awesome_slot.get_slot(params) -- Some modules expose a static connect_signals function -- at the module level, while other tables/objects inheriting from @@ -102,13 +101,13 @@ function awesome_slot.connect_slot(params) slot.target:connect_signal(slot.signal, slot.callback) end - slot.status = awesome_slot.slot_status.CONNECTED + slot.connected = true return slot end -function awesome_slot.disconnect_slot(params) - local slot = get_slot(params) +function awesome_slot.disconnect(params) + local slot = awesome_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 @@ -117,13 +116,13 @@ function awesome_slot.disconnect_slot(params) slot.target:disconnect_slot(slot.signal, slot.callback) end - slot.status = awesome_slot.slot_status.CREATED + slot.connected = false return slot end function awesome_slot.mt:__call(...) -- luacheck: ignore unused argument self - return awesome_slot.connect_slot(awesome_slot.create_slot(...)) + return awesome_slot.create(...) end return setmetatable(awesome_slot, awesome_slot.mt)