mirror of
https://github.com/Insality/druid.git
synced 2025-09-28 02:22:18 +02:00
Update docs
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
-- Copyright (c) 2021 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Druid lua event library
|
||||
--- Druid Event module.
|
||||
--
|
||||
-- Event is a simple class to handle callbacks. It's used in many Druid components.
|
||||
-- You can subscribe to event with `:subscribe` method and unsubscribe with `:unsubscribe`.
|
||||
-- @module DruidEvent
|
||||
-- @alias druid.event
|
||||
|
||||
@@ -9,9 +12,13 @@ local class = require("druid.system.middleclass")
|
||||
local DruidEvent = class("druid.event")
|
||||
|
||||
|
||||
--- Event constructur
|
||||
--- DruidEvent constructor
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @tparam function initial_callback Subscribe the callback on new event, if callback exist
|
||||
-- @tparam[opt] function initial_callback Subscribe the callback on new event, if callback exist
|
||||
-- @usage
|
||||
-- local Event = require("druid.event")
|
||||
-- ...
|
||||
-- local event = Event(initial_callback)
|
||||
function DruidEvent.initialize(self, initial_callback)
|
||||
self._callbacks = nil -- initialize later
|
||||
|
||||
@@ -24,7 +31,14 @@ end
|
||||
--- Subscribe callback on event
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @tparam function callback Callback itself
|
||||
-- @tparam table context Additional context as first param to callback call
|
||||
-- @tparam[opt] Any context Additional context as first param to callback call, usually it's self
|
||||
-- @usage
|
||||
-- local function on_long_callback(self)
|
||||
-- print("Long click!")
|
||||
-- end
|
||||
-- ...
|
||||
-- local button = self.druid:new_button("button", callback)
|
||||
-- button.on_long_click:subscribe(on_long_callback, self)
|
||||
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")
|
||||
@@ -42,7 +56,13 @@ end
|
||||
--- Unsubscribe callback on event
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @tparam function callback Callback itself
|
||||
-- @tparam table context Additional context as first param to callback call
|
||||
-- @tparam[opt] Any context Additional context as first param to callback call
|
||||
-- @usage
|
||||
-- local function on_long_callback(self)
|
||||
-- print("Long click!")
|
||||
-- end
|
||||
-- ...
|
||||
-- button.on_long_click:unsubscribe(on_long_callback, self)
|
||||
function DruidEvent.unsubscribe(self, callback, context)
|
||||
if not self._callbacks then
|
||||
return
|
||||
@@ -60,6 +80,8 @@ end
|
||||
--- Return true, if event have at lease one handler
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @treturn bool True if event have handlers
|
||||
-- @usage
|
||||
-- local is_long_click_handler_exists = button.on_long_click:is_exist()
|
||||
function DruidEvent.is_exist(self)
|
||||
if not self._callbacks then
|
||||
return false
|
||||
@@ -70,6 +92,8 @@ end
|
||||
|
||||
--- Clear the all event handlers
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @usage
|
||||
-- button.on_long_click:clear()
|
||||
function DruidEvent.clear(self)
|
||||
self._callbacks = nil
|
||||
end
|
||||
@@ -77,13 +101,18 @@ end
|
||||
|
||||
--- Trigger the event and call all subscribed callbacks
|
||||
-- @tparam DruidEvent self @{DruidEvent}
|
||||
-- @tparam any ... All event params
|
||||
-- @tparam Any ... All event params
|
||||
-- @usage
|
||||
-- local Event = require("druid.event")
|
||||
-- ...
|
||||
-- local event = Event()
|
||||
-- event:trigger("Param1", "Param2")
|
||||
function DruidEvent.trigger(self, ...)
|
||||
if not self._callbacks then
|
||||
return false
|
||||
end
|
||||
|
||||
for index, callback_info in ipairs(self._callbacks) do
|
||||
for _, callback_info in ipairs(self._callbacks) do
|
||||
if callback_info.context then
|
||||
callback_info.callback(callback_info.context, ...)
|
||||
else
|
||||
|
Reference in New Issue
Block a user