properties: Allow to delay some operations.

This will be useful for the append/remove support.
This commit is contained in:
Emmanuel Lepage Vallee 2019-10-19 19:49:14 -04:00
parent a8dcd3b2a2
commit fbe2b34af4
1 changed files with 29 additions and 8 deletions

View File

@ -8,6 +8,7 @@
---------------------------------------------------------------------------
local gtable = require("gears.table")
local gtimer = nil --require("gears.timer")
-- local gdebug = require("gears.debug")
local object = {}
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
@ -83,6 +84,8 @@ function object.capi_index_fallback(class, args)
end
end
assert(type(class) ~= "function")
-- Attach the accessor methods
class.set_index_miss_handler(getter)
class.set_newindex_miss_handler(setter)
@ -190,7 +193,7 @@ local function copy_object(obj, to_set, name, capi_name, is_object, join_if, set
})
end
function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_empty)
function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_empty, delay)
-- Some objects have a special "object" property to add more properties, but
-- not all.
@ -248,6 +251,7 @@ function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_
--TODO v6 Use the original directly and drop this legacy copy
local function apply()
local result = is_formatted and objs
or gtable.join(unpack(objs))
@ -258,6 +262,23 @@ function object._legacy_accessors(obj, name, capi_name, is_object, join_if, set_
else
self._private[name.."_formatted"] = result
end
end
-- Some properties, like keys, are expensive to set, schedule them
-- instead.
if not delay then
apply()
else
if not self._private["_delayed_"..name] then
gtimer = gtimer or require("gears.timer")
gtimer.delayed_call(function()
self._private["_delayed_"..name]()
self._private["_delayed_"..name] = nil
end)
end
self._private["_delayed_"..name] = apply
end
self._private[name] = copy_object(
obj, objs, name, capi_name, is_object, join_if, set_empty