aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-08-06 23:19:34 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-08-06 23:19:34 +0200
commitfa58e719d5fb5515e00f765f79fe920e4cb996fe (patch)
treec8b68ede8df3945da298b3347f2c6833b01e457e
parent2615778348a16ac83b7afd9e86f08feda98da1df (diff)
downloadlua_async-fa58e719d5fb5515e00f765f79fe920e4cb996fe.tar.xz
Add Event and EventTarget
-rw-r--r--README.md2
-rw-r--r--events.lua61
-rw-r--r--init.lua1
-rw-r--r--promises.lua6
4 files changed, 65 insertions, 5 deletions
diff --git a/README.md b/README.md
index b592877..6d50e5b 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# lua_async
This project aims to provide an API similar to the Node.js Event loop - for Lua, fully written in Lua itself. It is tested with Lua 5.1 and Lua 5.3.3, but should probably work with any Lua 5.x.
Note that the goal is not to clone the Node Event loop exactly.
-This is already fully usable, but some features are missing (Events, EventTargets, some Promise methods) and will be implemented in the near future.
+This is already fully usable, but some features are missing (especially some Promise methods) and will be implemented in the near future.
It also provides a few useful extra methods as well as basic scheduling.
## Current features
diff --git a/events.lua b/events.lua
new file mode 100644
index 0000000..ee723e8
--- /dev/null
+++ b/events.lua
@@ -0,0 +1,61 @@
+local EventPrototype = {}
+
+function EventPrototype:preventDefault()
+ self.defaultPrevented = true
+end
+
+function Event(type, data)
+ return setmetatable({
+ type = type,
+ data = data,
+ defaultPrevented = false,
+ timeStamp = os.clock(),
+ }, {__index = EventPrototype})
+end
+
+local EventTargetPrototype = {}
+
+function EventTargetPrototype:dispatchEvent(event)
+ event.target = self
+
+ local callback = self["on" + event.type]
+
+ if callback then
+ callback(event)
+ end
+
+ local listeners = self.__eventListeners[type]
+
+ if listeners then
+ for i, callback in ipairs(listeners) do
+ callback(event)
+ end
+ end
+
+ return not event.defaultPrevented
+end
+
+function EventTargetPrototype:addEventListener(type, callback)
+ local listeners = self.__eventListeners[type] or {}
+ table.insert(listeners, callback)
+ self.__eventListeners[type] = listeners
+end
+
+function EventTargetPrototype:removeEventListener(type, callback)
+ local listeners = self.__eventListeners[type]
+
+ if listeners then
+ for k, v in pairs(listeners) do
+ if v == callback then
+ table.remove(listeners, k)
+ break
+ end
+ end
+ end
+end
+
+function EventTarget()
+ return setmetatable({
+ __eventListeners = {},
+ }, {__index = EventTargetPrototype})
+end
diff --git a/init.lua b/init.lua
index 749bee6..07bea3e 100644
--- a/init.lua
+++ b/init.lua
@@ -26,6 +26,7 @@ return function(path)
"async_await",
"util",
"limiting",
+ "events",
} do
dofile(path .. f .. ".lua")
end
diff --git a/promises.lua b/promises.lua
index 0ab61a6..29bcdab 100644
--- a/promises.lua
+++ b/promises.lua
@@ -86,12 +86,10 @@ end
Promise = setmetatable({}, {
__call = function(_, resolver)
- local promise = {
+ local promise = setmetatable({
state = "pending",
__children = {},
- }
-
- setmetatable(promise, {__index = PromisePrototype})
+ }, {__index = PromisePrototype})
if resolver then
resolver(