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()
end
function API.WaitForEvent(plugin_id, Event)
function API.NewEvent(plugin_id, Event)
plugin.PauseQuota()
reactor:WaitForEvent(Event)
plugin.ResumeQuota()
end
function API.Event(plugin_id, Event)
plugin.PauseQuota()
reactor:Event(Event)
local Event = reactor:NewEvent(Event)
plugin.ResumeQuota()
return Event
end

View File

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

View File

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

View File

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