forked from lord-server/lord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: add
base_classes.Event
class. Relates to lord-server#1558
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
|
||
minetest.mod(function(mod) | ||
require("base_classes").init(mod) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = base_classes | ||
title = Base Classes | ||
description = Very handly common base classes to make life easier | ||
depends = builtin | ||
author = alek13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local Event = require("base_classes.Event") | ||
|
||
|
||
base_classes = {} -- luacheck: ignore unused global variable base_classes | ||
|
||
local function register_api() | ||
_G.base_classes = { | ||
Event = Event | ||
} | ||
end | ||
|
||
|
||
return { | ||
init = function() | ||
register_api() | ||
end, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
local pairs | ||
= pairs | ||
|
||
|
||
--- @static | ||
--- @class base_classes.Event | ||
local Event = { | ||
Type = {}, | ||
subscribers = {}, | ||
} | ||
|
||
--- @static | ||
--- @return base_classes.Event | ||
function Event:extended(object_or_class) | ||
local class = self | ||
self = object_or_class or {} | ||
return setmetatable(self, { __index = class} ) | ||
end | ||
|
||
|
||
--- @alias base_classes.Event.callback function | ||
|
||
--- @param event string name of event (One of `Event.Type::<const>`) | ||
--- | ||
--- @return fun(callback:controls.callback) | ||
function Event:on(event) | ||
return function(callback) | ||
self:subscribe(event, callback) | ||
end | ||
end | ||
|
||
--- @param event string name of event (One of `Event.Type::<const>`) | ||
--- @param callback controls.callback | ||
function Event:subscribe(event, callback) | ||
assert(self.Type[event], "Unknown controls.Event.Type: " .. event) | ||
assert(type(callback) == "function") | ||
|
||
table.insert(self.subscribers[event], callback) | ||
end | ||
|
||
--- @private | ||
--- @param event string name of event (One of `Event.Type::<const>`) | ||
--- @vararg any pass args that will be passed to subscribers callbacks. See `controls.callbacks.<func-types>` | ||
function Event:notify(event, ...) | ||
assert(self.Type[event], "Unknown controls.Event.Type: " .. event) | ||
|
||
for _, func in pairs(self.subscribers[event]) do | ||
func(...) | ||
end | ||
end | ||
|
||
--- @param event string name of event (One of `Event.Type::<const>`) | ||
--- @vararg any pass args that will be passed to subscribers callbacks. See `controls.callbacks.<func-types>` | ||
function Event:trigger(event, ...) | ||
self:notify(event, ...) | ||
end | ||
|
||
|
||
return Event |