Skip to content

Commit

Permalink
1.0.7.373 修复event潜藏的时序问题
Browse files Browse the repository at this point in the history
  • Loading branch information
topameng committed Sep 1, 2017
1 parent 9d1958f commit a752ce2
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions Assets/ToLua/Lua/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ function _event:Add(func, obj)
end

if self.lock then
local node = {value = func, _prev = 0, _next = 0}
table.insert(self.opList, node)
node.op = list.pushnode
local node = {value = func, _prev = 0, _next = 0, removed = true}
table.insert(self.opList, function() self.list:pushnode(node) end)
return node
else
return self.list:push(func)
Expand All @@ -90,8 +89,7 @@ function _event:Remove(func, obj)
for i, v in ilist(self.list) do
if v.func == func and v.obj == obj then
if self.lock then
table.insert(self.opList, i)
i.op = list.remove
table.insert(self.opList, function() self.list:remove(i) end)
else
self.list:remove(i)
end
Expand All @@ -111,21 +109,19 @@ function _event:CreateListener(func, obj)
end

function _event:AddListener(handle)
if self.lock then
table.insert(self.opList, handle)
handle.op = list.pushnode
if self.lock then
table.insert(self.opList, function() self.list:pushnode(handle) end)
else
self.list:pushnode(handle)
end
end

function _event:RemoveListener(handle)
if self.lock then
table.insert(self.opList, handle)
handle.op = list.remove
table.insert(self.opList, function() self.list:remove(handle) end)
else
self.list:remove(handle)
end
end
end

function _event:Count()
Expand Down Expand Up @@ -174,12 +170,13 @@ _event.__call = function(self, ...)
end
end

for _, i in ipairs(self.opList) do
i.op(_list, i)
end

self.lock = false
local opList = self.opList
self.opList = {}
self.lock = false

for _, op in ipairs(opList) do
op()
end
end

function event(name, safe)
Expand Down

0 comments on commit a752ce2

Please sign in to comment.