mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 10:27:47 +02:00
Solve #182 add table pool for events
This commit is contained in:
parent
2779f9cf7a
commit
dff522fbaa
@ -30,6 +30,12 @@ function BackHandler.init(self, callback, params)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function BackHandler.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_back:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Input handler for component
|
--- Input handler for component
|
||||||
-- @tparam BackHandler self @{BackHandler}
|
-- @tparam BackHandler self @{BackHandler}
|
||||||
-- @tparam string action_id on_input action id
|
-- @tparam string action_id on_input action id
|
||||||
|
@ -239,6 +239,17 @@ function Button.on_late_init(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Button.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_click:clear()
|
||||||
|
self.on_repeated_click:clear()
|
||||||
|
self.on_long_click:clear()
|
||||||
|
self.on_double_click:clear()
|
||||||
|
self.on_hold_callback:clear()
|
||||||
|
self.on_click_outside:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Button.on_input(self, action_id, action)
|
function Button.on_input(self, action_id, action)
|
||||||
if not is_input_match(self, action_id) then
|
if not is_input_match(self, action_id) then
|
||||||
return false
|
return false
|
||||||
|
@ -189,6 +189,16 @@ function Drag.init(self, node, on_drag_callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Drag.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_touch_start:clear()
|
||||||
|
self.on_touch_end:clear()
|
||||||
|
self.on_drag_start:clear()
|
||||||
|
self.on_drag:clear()
|
||||||
|
self.on_drag_end:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Drag.on_late_init(self)
|
function Drag.on_late_init(self)
|
||||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||||
|
@ -38,6 +38,13 @@ function Hover.init(self, node, on_hover_callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Hover.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_hover:clear()
|
||||||
|
self.on_mouse_hover:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Hover.on_late_init(self)
|
function Hover.on_late_init(self)
|
||||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||||
|
@ -174,6 +174,14 @@ function Scroll.init(self, view_node, content_node)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Scroll.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_scroll:clear()
|
||||||
|
self.on_scroll_to:clear()
|
||||||
|
self.on_point_scroll:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Scroll.on_late_init(self)
|
function Scroll.on_late_init(self)
|
||||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||||
|
@ -115,6 +115,16 @@ function StaticGrid.init(self, parent, element, in_row)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function StaticGrid.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_add_item:clear()
|
||||||
|
self.on_remove_item:clear()
|
||||||
|
self.on_change_items:clear()
|
||||||
|
self.on_clear:clear()
|
||||||
|
self.on_update_positions:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local _temp_pos = vmath.vector3(0)
|
local _temp_pos = vmath.vector3(0)
|
||||||
--- Return pos for grid node index
|
--- Return pos for grid node index
|
||||||
-- @tparam StaticGrid self @{StaticGrid}
|
-- @tparam StaticGrid self @{StaticGrid}
|
||||||
|
@ -99,6 +99,12 @@ function Swipe.init(self, node, on_swipe_callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Swipe.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_swipe:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Swipe.on_late_init(self)
|
function Swipe.on_late_init(self)
|
||||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||||
|
@ -217,6 +217,14 @@ function Text.init(self, node, value, adjust_type)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Text.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_set_text:clear()
|
||||||
|
self.on_set_pivot:clear()
|
||||||
|
self.on_update_text_scale:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Text.on_layout_change(self)
|
function Text.on_layout_change(self)
|
||||||
self:set_to(self.last_value)
|
self:set_to(self.last_value)
|
||||||
end
|
end
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
local const = require("druid.const")
|
local const = require("druid.const")
|
||||||
local class = require("druid.system.middleclass")
|
local class = require("druid.system.middleclass")
|
||||||
local helper = require("druid.helper")
|
local helper = require("druid.helper")
|
||||||
|
local tablepool = require("druid.system.tablepool")
|
||||||
|
|
||||||
|
|
||||||
local BaseComponent = class("druid.component")
|
local BaseComponent = class("druid.component")
|
||||||
@ -348,6 +349,11 @@ function BaseComponent.set_debug(self, is_debug)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- On remove method on druid:remove or druid:final (protected)
|
||||||
|
function BaseComponent:on_internal_remove()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Return true, if input priority was changed
|
--- Return true, if input priority was changed
|
||||||
-- @tparam BaseComponent self @{BaseComponent}
|
-- @tparam BaseComponent self @{BaseComponent}
|
||||||
-- @local
|
-- @local
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
M.POOL_ID = "druid_table_pool"
|
||||||
|
|
||||||
M.ACTION_TEXT = hash(sys.get_config("druid.input_text", "text"))
|
M.ACTION_TEXT = hash(sys.get_config("druid.input_text", "text"))
|
||||||
M.ACTION_TOUCH = hash(sys.get_config("druid.input_touch", "touch"))
|
M.ACTION_TOUCH = hash(sys.get_config("druid.input_touch", "touch"))
|
||||||
M.ACTION_MARKED_TEXT = hash(sys.get_config("druid.input_marked_text", "marked_text"))
|
M.ACTION_MARKED_TEXT = hash(sys.get_config("druid.input_marked_text", "marked_text"))
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
-- @alias druid.event
|
-- @alias druid.event
|
||||||
|
|
||||||
local class = require("druid.system.middleclass")
|
local class = require("druid.system.middleclass")
|
||||||
|
local const = require("druid.const")
|
||||||
|
local tablepool = require("druid.system.tablepool")
|
||||||
|
|
||||||
local DruidEvent = class("druid.event")
|
local DruidEvent = class("druid.event")
|
||||||
|
|
||||||
@ -29,11 +31,12 @@ function DruidEvent.subscribe(self, callback, context)
|
|||||||
assert(type(self) == "table", "You should subscribe to event with : syntax")
|
assert(type(self) == "table", "You should subscribe to event with : syntax")
|
||||||
assert(type(callback) == "function", "Callback should be function")
|
assert(type(callback) == "function", "Callback should be function")
|
||||||
|
|
||||||
self._callbacks = self._callbacks or {}
|
self._callbacks = self._callbacks or tablepool.fetch(const.POOL_ID)
|
||||||
table.insert(self._callbacks, {
|
local callback_table = tablepool.fetch(const.POOL_ID, 0, 2)
|
||||||
callback = callback,
|
callback_table.callback = callback
|
||||||
context = context
|
callback_table.context = context
|
||||||
})
|
|
||||||
|
table.insert(self._callbacks, callback_table)
|
||||||
|
|
||||||
return callback
|
return callback
|
||||||
end
|
end
|
||||||
@ -50,6 +53,7 @@ function DruidEvent.unsubscribe(self, callback, context)
|
|||||||
|
|
||||||
for index, callback_info in ipairs(self._callbacks) do
|
for index, callback_info in ipairs(self._callbacks) do
|
||||||
if callback_info.callback == callback and callback_info.context == context then
|
if callback_info.callback == callback and callback_info.context == context then
|
||||||
|
tablepool.release(self._callbacks[index])
|
||||||
table.remove(self._callbacks, index)
|
table.remove(self._callbacks, index)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -71,6 +75,12 @@ end
|
|||||||
--- Clear the all event handlers
|
--- Clear the all event handlers
|
||||||
-- @tparam DruidEvent self @{DruidEvent}
|
-- @tparam DruidEvent self @{DruidEvent}
|
||||||
function DruidEvent.clear(self)
|
function DruidEvent.clear(self)
|
||||||
|
if self._callbacks then
|
||||||
|
for index = #self._callbacks, 1, -1 do
|
||||||
|
tablepool.release(self._callbacks[index])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
tablepool.release(self._callbacks)
|
||||||
self._callbacks = nil
|
self._callbacks = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -83,7 +93,7 @@ function DruidEvent.trigger(self, ...)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
for index, callback_info in ipairs(self._callbacks) do
|
for _, callback_info in ipairs(self._callbacks) do
|
||||||
if callback_info.context then
|
if callback_info.context then
|
||||||
callback_info.callback(callback_info.context, ...)
|
callback_info.callback(callback_info.context, ...)
|
||||||
else
|
else
|
||||||
|
@ -67,6 +67,12 @@ function Checkbox.on_layout_change(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Checkbox.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_change_state:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Set checkbox state
|
--- Set checkbox state
|
||||||
-- @tparam Checkbox self @{Checkbox}
|
-- @tparam Checkbox self @{Checkbox}
|
||||||
-- @tparam bool state Checkbox state
|
-- @tparam bool state Checkbox state
|
||||||
|
@ -41,6 +41,13 @@ function CheckboxGroup.init(self, nodes, callback, click_nodes)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function CheckboxGroup.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_checkbox_click:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--- Set checkbox group state
|
--- Set checkbox group state
|
||||||
-- @tparam CheckboxGroup self @{CheckboxGroup}
|
-- @tparam CheckboxGroup self @{CheckboxGroup}
|
||||||
-- @tparam bool[] indexes Array of checkbox state
|
-- @tparam bool[] indexes Array of checkbox state
|
||||||
|
@ -76,6 +76,12 @@ function DataList.on_remove(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function DataList.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_scroll_progress_change:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Set new data set for DataList component
|
--- Set new data set for DataList component
|
||||||
-- @tparam DataList self @{DataList}
|
-- @tparam DataList self @{DataList}
|
||||||
-- @tparam table data The new data array
|
-- @tparam table data The new data array
|
||||||
|
@ -89,6 +89,16 @@ function DynamicGrid.init(self, parent)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function DynamicGrid.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_add_item:clear()
|
||||||
|
self.on_remove_item:clear()
|
||||||
|
self.on_change_items:clear()
|
||||||
|
self.on_clear:clear()
|
||||||
|
self.on_update_positions:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function DynamicGrid.on_layout_change(self)
|
function DynamicGrid.on_layout_change(self)
|
||||||
self:_update(true)
|
self:_update(true)
|
||||||
end
|
end
|
||||||
|
@ -154,6 +154,17 @@ function Input.init(self, click_node, text_node, keyboard_type)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Input.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_input_select:clear()
|
||||||
|
self.on_input_unselect:clear()
|
||||||
|
self.on_input_text:clear()
|
||||||
|
self.on_input_empty:clear()
|
||||||
|
self.on_input_full:clear()
|
||||||
|
self.on_input_wrong:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Input.on_input(self, action_id, action)
|
function Input.on_input(self, action_id, action)
|
||||||
if self.is_selected then
|
if self.is_selected then
|
||||||
local input_text = nil
|
local input_text = nil
|
||||||
|
@ -41,6 +41,12 @@ function LangText.init(self, node, locale_id, no_adjust)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function LangText.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_change:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function LangText.on_language_change(self)
|
function LangText.on_language_change(self)
|
||||||
if self.last_locale then
|
if self.last_locale then
|
||||||
self:translate(self.last_locale, unpack(self.last_locale_args))
|
self:translate(self.last_locale, unpack(self.last_locale_args))
|
||||||
|
@ -39,6 +39,12 @@ function Layout:init(node, mode, on_size_changed_callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Layout.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_size_changed:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Layout:on_window_resized()
|
function Layout:on_window_resized()
|
||||||
local window_x, window_y = window.get_size()
|
local window_x, window_y = window.get_size()
|
||||||
local stretch_x = window_x / self.window_size.x
|
local stretch_x = window_x / self.window_size.x
|
||||||
|
@ -121,6 +121,12 @@ function Progress.init(self, node, key, init_value)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Progress.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_change:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- @tparam Progress self @{Progress}
|
-- @tparam Progress self @{Progress}
|
||||||
function Progress.on_late_init(self)
|
function Progress.on_late_init(self)
|
||||||
self:set_to(self._init_value)
|
self:set_to(self._init_value)
|
||||||
|
@ -50,6 +50,12 @@ function RadioGroup.init(self, nodes, callback, click_nodes)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function RadioGroup.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_radio_click:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Set radio group state
|
--- Set radio group state
|
||||||
-- @tparam RadioGroup self @{RadioGroup}
|
-- @tparam RadioGroup self @{RadioGroup}
|
||||||
-- @tparam number index Index in radio group
|
-- @tparam number index Index in radio group
|
||||||
|
@ -77,6 +77,12 @@ function Slider.init(self, node, end_pos, callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Slider.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_change_value:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Slider.on_layout_change(self)
|
function Slider.on_layout_change(self)
|
||||||
self:set(self.value)
|
self:set(self.value)
|
||||||
end
|
end
|
||||||
|
@ -65,6 +65,14 @@ function Timer.init(self, node, seconds_from, seconds_to, callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Timer.on_internal_remove(self)
|
||||||
|
component.on_internal_remove(self)
|
||||||
|
self.on_tick:clear()
|
||||||
|
self.on_set_enabled:clear()
|
||||||
|
self.on_timer_end:clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Timer.update(self, dt)
|
function Timer.update(self, dt)
|
||||||
if not self.is_on then
|
if not self.is_on then
|
||||||
return
|
return
|
||||||
|
@ -289,6 +289,7 @@ function DruidInstance.remove(self, component)
|
|||||||
local all_components = self.components_all
|
local all_components = self.components_all
|
||||||
for i = #all_components, 1, -1 do
|
for i = #all_components, 1, -1 do
|
||||||
if all_components[i] == component then
|
if all_components[i] == component then
|
||||||
|
component:on_internal_remove()
|
||||||
if component.on_remove then
|
if component.on_remove then
|
||||||
component:on_remove()
|
component:on_remove()
|
||||||
end
|
end
|
||||||
|
75
druid/system/tablepool.lua
Normal file
75
druid/system/tablepool.lua
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
-- Source: https://github.com/openresty/lua-tablepool/blob/master/lib/tablepool.lua
|
||||||
|
|
||||||
|
local setmetatable = setmetatable
|
||||||
|
|
||||||
|
local _M = {}
|
||||||
|
local max_pool_size = 500
|
||||||
|
local pools = {}
|
||||||
|
|
||||||
|
|
||||||
|
function _M.fetch(tag)
|
||||||
|
local pool = pools[tag]
|
||||||
|
if not pool then
|
||||||
|
pool = {}
|
||||||
|
pools[tag] = pool
|
||||||
|
pool.c = 0
|
||||||
|
pool[0] = 0
|
||||||
|
|
||||||
|
else
|
||||||
|
local len = pool[0]
|
||||||
|
if len > 0 then
|
||||||
|
local obj = pool[len]
|
||||||
|
pool[len] = nil
|
||||||
|
pool[0] = len - 1
|
||||||
|
return obj
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function _M.release(tag, obj, noclear)
|
||||||
|
if not obj then
|
||||||
|
error("object empty", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
local pool = pools[tag]
|
||||||
|
if not pool then
|
||||||
|
pool = {}
|
||||||
|
pools[tag] = pool
|
||||||
|
pool.c = 0
|
||||||
|
pool[0] = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if not noclear then
|
||||||
|
setmetatable(obj, nil)
|
||||||
|
for k in pairs(obj) do
|
||||||
|
obj[k] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local cnt = pool.c + 1
|
||||||
|
if cnt >= 20000 then
|
||||||
|
pool = {}
|
||||||
|
pools[tag] = pool
|
||||||
|
pool.c = 0
|
||||||
|
pool[0] = 0
|
||||||
|
return
|
||||||
|
end
|
||||||
|
pool.c = cnt
|
||||||
|
end
|
||||||
|
|
||||||
|
local len = pool[0] + 1
|
||||||
|
if len > max_pool_size then
|
||||||
|
-- discard it simply
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
pool[len] = obj
|
||||||
|
pool[0] = len
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return _M
|
Loading…
x
Reference in New Issue
Block a user