Optimize component and event creation, add cache for interests, optimize scroll

This commit is contained in:
Insality
2022-03-11 20:37:18 +02:00
parent cc35fe208c
commit c5c335d17c
5 changed files with 41 additions and 39 deletions

View File

@@ -13,7 +13,7 @@ local DruidEvent = class("druid.event")
-- @tparam DruidEvent self @{DruidEvent}
-- @tparam function initial_callback Subscribe the callback on new event, if callback exist
function DruidEvent.initialize(self, initial_callback)
self._callbacks = {}
self._callbacks = nil -- initialize later
if initial_callback then
self:subscribe(initial_callback)
@@ -29,6 +29,7 @@ function DruidEvent.subscribe(self, callback, context)
assert(type(self) == "table", "You should subscribe to event with : syntax")
assert(type(callback) == "function", "Callback should be function")
self._callbacks = self._callbacks or {}
table.insert(self._callbacks, {
callback = callback,
context = context
@@ -43,6 +44,10 @@ end
-- @tparam function callback Callback itself
-- @tparam table context Additional context as first param to callback call
function DruidEvent.unsubscribe(self, callback, context)
if not self._callbacks then
return
end
for index, callback_info in ipairs(self._callbacks) do
if callback_info.callback == callback and callback_info.context == context then
table.remove(self._callbacks, index)
@@ -56,6 +61,9 @@ end
-- @tparam DruidEvent self @{DruidEvent}
-- @treturn bool True if event have handlers
function DruidEvent.is_exist(self)
if not self._callbacks then
return false
end
return #self._callbacks > 0
end
@@ -63,7 +71,7 @@ end
--- Clear the all event handlers
-- @tparam DruidEvent self @{DruidEvent}
function DruidEvent.clear(self)
self._callbacks = {}
self._callbacks = nil
end
@@ -71,6 +79,10 @@ end
-- @tparam DruidEvent self @{DruidEvent}
-- @tparam any ... All event params
function DruidEvent.trigger(self, ...)
if not self._callbacks then
return false
end
for index, callback_info in ipairs(self._callbacks) do
if callback_info.context then
callback_info.callback(callback_info.context, ...)