Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Nodell = {}
- Nodell.__index = Nodell
- function Nodell.new(signal)
- return setmetatable({Listener = signal}, Nodell)
- end
- local LinkedList = {}
- local Array = {}
- local Dict = {}
- LinkedList.__index = LinkedList
- Array.__index = Array
- Dict.__index = Dict
- -- Linked List Code
- function LinkedList.new()
- return setmetatable({Next = nil}, LinkedList)
- end
- function LinkedList:Connect(signal)
- local NewNode = Nodell.new(signal)
- if not self.Next then
- self.Next = NewNode
- else
- local current = self.Next
- while current and current.Next do
- current = current.Next
- end
- current.Next = NewNode
- end
- end
- function LinkedList:Fire(...)
- local current = self.Next
- while current do
- current.Listener(...)
- current = current.Next
- end
- end
- -- Array Main Code
- function Array.new()
- return setmetatable({}, Array)
- end
- function Array:Connect(signal)
- local NewNode = Nodell.new(signal)
- self[#self+1] = NewNode
- end
- function Array:Fire(index, ...)
- for i, v in pairs(self) do
- if (i == index) then
- v.Listener(...)
- end
- end
- end
- -- Dict Code
- function Dict.new()
- return setmetatable({}, Dict)
- end
- function Dict:Connect(HandlerName, signal)
- local NewNode = Nodell.new(signal)
- Dict[HandlerName] = NewNode
- end
- function Dict:Fire(HandlerName, ...)
- Dict[HandlerName].Listener(...)
- end
- -- Test Result
- local ll1 = LinkedList.new()
- ll1:Connect(function(msg)
- print("LinkedList Event Handler 1 : " .. msg)
- end)
- ll1:Connect(function(msg)
- print("LinkedList Event Handler 2 : " .. msg)
- end)
- ll1:Fire("Hello World")
- local arr1 = Array.new()
- arr1:Connect(function(msg)
- print("Array Event Handler 1 : " .. msg)
- end)
- arr1:Fire(1, "Hello World")
- local dict1 = Dict.new()
- dict1:Connect("Sigma", function(msg)
- print("Dict 1 Event Handler : " .. msg)
- end)
- dict1:Fire("Sigma", "Hello World")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement