]> git.lizzy.rs Git - lua_async.git/commitdiff
Add Event and EventTarget
authorElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 6 Aug 2021 21:19:34 +0000 (23:19 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 6 Aug 2021 21:19:34 +0000 (23:19 +0200)
README.md
events.lua [new file with mode: 0644]
init.lua
promises.lua

index b5928779ab5100ab6ec0c9e79c4b807152ceacc7..6d50e5bc2c5e94a946aa74481efeb6a961b92b33 100644 (file)
--- 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 (file)
index 0000000..ee723e8
--- /dev/null
@@ -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
index 749bee6c7dca94355c47ef0d1861dd5ff18211e6..07bea3e9fa7ed97792cacb0a047c1a080a192c7f 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -26,6 +26,7 @@ return function(path)
                "async_await",
                "util",
                "limiting",
+               "events",
        } do
                dofile(path .. f .. ".lua")
        end
index 0ab61a6b20a459ca60850487d566b28f27e0a7a3..29bcdabcf374d8c38637d55144194fc68d9c4c7e 100644 (file)
@@ -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(