Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ArrayEvent = {}
- local DictEvent = {}
- local LinkedListEvent = {}
- local BindEvent = Instance.new('BindableEvent')
- ArrayEvent.__index = ArrayEvent
- DictEvent.__index = DictEvent
- LinkedListEvent.__index = LinkedListEvent
- local Node = {}
- Node.__index = Node
- function Node.new(signal)
- return setmetatable({Listener = signal}, Node)
- end
- -- ArrayEvent
- function ArrayEvent.new()
- return setmetatable({}, ArrayEvent)
- end
- function ArrayEvent:Connect(signal)
- local NewNode = Node.new(signal)
- table.insert(self, NewNode)
- end
- function ArrayEvent:Disconnect(index)
- if self[index] and self[index].Listener then
- table.remove(self, index)
- --self[index] = nil (Optional)
- else
- error("That Index Does not Exist")
- end
- end
- function ArrayEvent:FireAll(...)
- for i, v in ipairs(self) do
- if v.Listener then v.Listener(...) end
- end
- end
- function ArrayEvent:Fire(index, ...)
- if self[index] and self[index].Listener then
- self[index].Listener(...)
- else
- error("That Index Does Not Exist")
- end
- end
- -- DictEvent
- function DictEvent.new()
- return setmetatable({}, DictEvent)
- end
- function DictEvent:Connect(indexname, signal)
- if not self[indexname] then
- local NewNode = Node.new(signal)
- self[indexname] = NewNode
- else
- error("That Key Already Exist Dict Does Not Allow Dupe")
- end
- end
- function DictEvent:Disconnect(indexname)
- if self[indexname] and self[indexname].Listener then
- --self[indexname] = nil (Optional)
- table.remove(self, table.find(self[indexname]))
- else
- error("That Key Does not Exist")
- end
- end
- function DictEvent:FireAll(...)
- for i, v in pairs(self) do
- if v.Listener then v.Listener(...) end
- end
- end
- function DictEvent:Fire(indexname, ...)
- if self[indexname] and self[indexname].Listener then
- self[indexname].Listener(...)
- else
- error("That Key Does not Exist")
- end
- end
- -- LinkedListEvent
- function LinkedListEvent.new()
- return setmetatable({Next = nil}, LinkedListEvent)
- end
- function LinkedListEvent:Connect(signal)
- local NewNode = Node.new(signal)
- if not self.Next then
- self.Next = NewNode
- else
- local current = self.Next
- while current.Next do
- current = current.Next
- end
- current.Next = NewNode
- end
- end
- function LinkedListEvent:Disconnect(pos)
- local c = 1
- local current = self.Next
- local prev = nil
- if not current or pos < 1 then
- return
- end
- if pos == 1 then
- self.Next = current.Next
- return
- end
- while c < pos and current do
- prev = current
- current = current.Next
- c = c + 1
- end
- if not current then
- return
- end
- prev.Next = current.Next
- end
- function LinkedListEvent:FireAll(...)
- local current = self.Next
- while current do
- if current.Listener then current.Listener(...) end
- current = current.Next
- end
- end
- function LinkedListEvent:Fire(pos, ...)
- local current = self.Next
- if pos == 1 then
- if current.Listener then
- current.Listener(...)
- else
- error("This Pos does not Contain Listener")
- end
- else
- local c = 1
- while c < pos and current do
- current = current.Next
- c = c + 1
- end
- if current.Listener then
- current.Listener(...)
- else
- error("This Pos does not contain Listener")
- end
- end
- end
- local ll1 = LinkedListEvent.new()
- -- LinkedList Test Case
- ll1:Connect(function(msg)
- print("ll1 : ", msg)
- end)
- ll1:Connect(function(msg)
- print("ll2 :", msg)
- end)
- ll1:Fire(1, "Hello World")
- ll1:Disconnect(1)
- print("After Removed ll1:")
- ll1:FireAll("Hello World")
- -- ArrayEvent TestCase
- local arr1 = ArrayEvent.new()
- arr1:Connect(function(msg)
- print("arr1 : ", msg)
- end)
- arr1:Connect(function(msg)
- print("arr2 : ", msg)
- end)
- arr1:Fire(1, "Hello World")
- arr1:Fire(2, "Hello World")
- arr1:Disconnect(1)
- print("After Removed arr1")
- arr1:Fire(1, "Hello World") -- Test if error Print are working
- -- Now arr2 Print instead of arr1
- local dict1 = DictEvent.new()
- dict1:Connect("Sigma", function(msg)
- print("Dict1 : ", msg)
- end)
- --dict1:Connect("Sigma", function(msg)print()end) -- Test if Error Raise are working
- dict1:Connect("Skibid", function(msg)
- print("Dict2: ", msg)
- end)
- dict1:Fire("Sigma", "Hello Directly From Sigma")
- dict1:FireAll("Hello World Dict")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement