local sys = require "sys" local socket = require "socket" local event = {} local eventList = {} local function guid() local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'} local tb={} math.randomseed(math.floor(os.time() * math.random())) for i = 1, 32 do table.insert(tb, seed[math.random(1,16)]) end local sid=table.concat(tb) return string.format('%s-%s-%s-%s-%s', string.sub(sid, 1, 8), string.sub(sid, 9, 12), string.sub(sid, 13, 16), string.sub(sid, 17, 20), string.sub(sid, 21, 32) ) end function event.publish(id, msg) if eventList[id] ~= nil then for _, v in pairs(eventList[id]) do if(type(v) == "table") then v.callback(msg) end end end end function event.wait(id, timeout) if eventList[id] == nil then eventList[id] = {} end local result = false local message local obj = {} obj.timeout = timeout obj.callback = function(msg) result = true message = msg end local guid = guid() eventList[id][guid] = obj local tickstart = math.floor(socket.gettime() * 1000) while math.floor(socket.gettime() * 1000) - tickstart < timeout do if result == true then eventList[id][guid] = nil return result, message end sys.wait(1) end eventList[id][guid] = nil return result end return event