New event mechanism, whois fixes

master
q3k 2013-11-28 11:55:44 +01:00
parent 74fca86d34
commit a4762aa45f
4 changed files with 54 additions and 33 deletions

View File

@ -43,17 +43,11 @@ function API.Sleep(plugin_id, Delay)
plugin.ResumeQuota() plugin.ResumeQuota()
end end
function API.NewEvent(plugin_id, Event)
function API.WaitForEvent(plugin_id, Event)
plugin.PauseQuota() plugin.PauseQuota()
reactor:WaitForEvent(Event) local Event = reactor:NewEvent(Event)
plugin.ResumeQuota()
end
function API.Event(plugin_id, Event)
plugin.PauseQuota()
reactor:Event(Event)
plugin.ResumeQuota() plugin.ResumeQuota()
return Event
end end

View File

@ -14,6 +14,7 @@ function reactor:Initialize(quantum)
self._coroutines = {} self._coroutines = {}
self._timers = {} self._timers = {}
self._events = {}
end end
function reactor:Quit() function reactor:Quit()
@ -30,24 +31,54 @@ function reactor:Sleep(time)
coroutine.yield() coroutine.yield()
end end
function reactor:WaitForEvent(event) reactor.Event = {}
local co = coroutine.running() function reactor.Event:Wait()
if co == nil then if coroutine.running() == nil then
error("Main thread waiting for event... wtf?") error("Main thread waiting for event... wtf?")
end end
self._co_event[co] = event self.Waiting[#self.Waiting + 1] = coroutine.running()
coroutine.yield() local Status, Data = coroutine.yield()
if Status == false then
-- exception!
error(Data)
end
return unpack(Data)
end end
function reactor.Event:Fire(...)
function reactor:Event(event) local Data = {...}
for Coroutine, Event in pairs(self._co_event) do -- make a local copy of the old waiters, and clear the new ones
if Event == event then local Waiters = {}
coroutine.resume(Coroutine) for i, Coroutine in pairs(self.Waiting) do
end Waiters[#Waiters + 1] = Coroutine
end
self.Waiting = {}
for i, Coroutine in pairs(Waiters) do
coroutine.resume(Coroutine, true, Data)
end end
end end
function reactor.Event:Destroy()
-- destroy all waiters
for i, Coroutine in pairs(self.Waiting) do
coroutine.resume(Coroutine, false, "Coroutine destroyed.")
end
self.Reactor._events[self.Identifier] = nil
end
function reactor:NewEvent()
local Table = {}
Table.Identifier = #self._events + 1
Table.Reactor = self
Table.Waiting = {}
setmetatable(Table, reactor.Event)
reactor.Event.__index = reactor.Event
self._events[Table.Identifier] = Identifier
return Table
end
function reactor:Spawn(f, ...) function reactor:Spawn(f, ...)
local Args = {...} local Args = {...}
local co = coroutine.create(function() f(unpack(Args)) end) local co = coroutine.create(function() f(unpack(Args)) end)

View File

@ -11,11 +11,7 @@ local GetRates = function()
end end
local Rates = {} local Rates = {}
<<<<<<< HEAD
for _, Market in pairs(Rates) do
=======
for _, Market in pairs(Data) do for _, Market in pairs(Data) do
>>>>>>> c6688a87ae38c2a483731ec9f4da072f2132c725
local Rate = {} local Rate = {}
Rate.high = Market.high Rate.high = Market.high
Rate.low = Market.low Rate.low = Market.low
@ -27,11 +23,7 @@ local GetRates = function()
end end
local FormatData = function(Rate, Key) local FormatData = function(Rate, Key)
<<<<<<< HEAD
local Value = Rate[key]
=======
local Value = Rate[Key] local Value = Rate[Key]
>>>>>>> c6688a87ae38c2a483731ec9f4da072f2132c725
local Currency = Rate.currency local Currency = Rate.currency
return string.format("%.2f %s", Value, Currency) return string.format("%.2f %s", Value, Currency)

View File

@ -4,24 +4,28 @@ local Pending = {}
plugin.AddHook('auth.GetAccount', 'GetAccount', function(IRC, Username) plugin.AddHook('auth.GetAccount', 'GetAccount', function(IRC, Username)
if Map[Username] == nil then if Map[Username] == nil then
if not Pending[Username] then if not Pending[Username] then
-- create and event and wait for it
local Event = plugin.NewEvent()
Pending[Username] = Event
IRC:Whois(Username) IRC:Whois(Username)
Pending[Username] = true
end end
plugin.WaitForEvent("whois-"..Username) return Pending[Username]:Wait()
end end
return Map[Username] return Map[Username]
end) end)
plugin.AddHook('irc.GetResponse330', 'WHOISResponse', function(_, Username, Account, Message) plugin.AddHook('irc.GetResponse330', 'WHOISResponse', function(_, Username, Account, Message)
Map[Username] = Account Map[Username] = Account
plugin.Event("whois-" .. Username) if Pending[Username] ~= nil then
Pending[Username]:Fire(Account)
end
end) end)
plugin.AddHook('irc.ChannelNames', 'ScanChannelUsers', function(Channel) --[[ plugin.AddHook('irc.ChannelNames', 'ScanChannelUsers', function(Channel)
for Nick, Member in pairs(Channel.Members) do for Nick, Member in pairs(Channel.Members) do
if not Pending[Nick] then if not Pending[Nick] then
Channel:Whois(Nick) Channel:Whois(Nick)
Pending[Nick] = true Pending[Nick] = true
end end
end end
end) end) ]]--