Skip to content

Commit

Permalink
Fix build and use const
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Dec 11, 2024
1 parent d9ac40b commit 71f8315
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: dev build gen check test
.PHONY: dev build gen check test compile

VERSION=0.1.0

Expand Down
5 changes: 2 additions & 3 deletions src/emitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,13 @@ end


local DEFAULT_CONFIG = { id = "", position = "last" }

local emitter_mt = { __index = Emitter }
local EMITTER_MT = { __index = Emitter }

function Emitter.new()
return setmetatable({
_listeners = {},
_forwarding = {},
}, emitter_mt)
}, EMITTER_MT)
end

function Emitter:reset()
Expand Down
15 changes: 7 additions & 8 deletions src/emitter.tl
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,14 @@ end

-----------------------------------------------------------------------------

local DEFAULT_CONFIG: Emitter.ListenerConfig = { id = "", position = "last" }

local emitter_mt: metatable<Emitter> = { __index = Emitter }
local DEFAULT_CONFIG <const>: Emitter.ListenerConfig = { id = "", position = "last" }
local EMITTER_MT <const>: metatable<Emitter> = { __index = Emitter }

function Emitter.new(): Emitter
return setmetatable({
_listeners = {},
_forwarding = {}
}, emitter_mt)
}, EMITTER_MT)
end

function Emitter:reset()
Expand All @@ -160,7 +159,7 @@ function Emitter:on<E is Emitter.Event>(event: E, listener: Emitter.Listener<E>,
self._listeners[event] = list
end
config = config or DEFAULT_CONFIG
local node: ListenerNode = { id = config.id or "", listener = listener as Emitter.Listener<any> }
local node <const>: ListenerNode = { id = config.id or "", listener = listener as Emitter.Listener<any> }
if config.position == "last" then
LinkedList.append(list, node as ListNode<any>)
else
Expand All @@ -169,7 +168,7 @@ function Emitter:on<E is Emitter.Event>(event: E, listener: Emitter.Listener<E>,
end

function Emitter:off<E is Emitter.Event>(event: E, listener: Emitter.Listener<E> | string)
local listeners = self._listeners[event]
local listeners <const> = self._listeners[event]
if not listeners then
return
elseif listener is string then
Expand Down Expand Up @@ -201,7 +200,7 @@ function Emitter:once<E is Emitter.Event>(event: E, listener: Emitter.Listener<E
end

function Emitter:emit(event: Emitter.Event)
local listeners = self._listeners[event.type]
local listeners <const> = self._listeners[event.type]
if listeners then
local node = listeners.head
while node do
Expand All @@ -222,7 +221,7 @@ function Emitter:removeAllListeners(event: Emitter.Event)
end

function Emitter:startForwarding(emitter: Emitter)
local node: EmitterNode = { emitter = emitter }
local node <const>: EmitterNode = { emitter = emitter }
LinkedList.append(self._forwarding, node as ListNode<any>)
end

Expand Down
1 change: 0 additions & 1 deletion tlconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ return {
source_dir = "src",
gen_target = "5.1",
gen_compat = "off",
include = { "src/emitter.tl" },
}

0 comments on commit 71f8315

Please sign in to comment.