Working on services

This commit is contained in:
Ryan Ward 2020-02-17 15:35:46 -05:00
parent 8f5973343d
commit 4384dbbaee
3 changed files with 93 additions and 55 deletions

View File

@ -96,6 +96,7 @@ multi:mainloop()
``` ```
Changed: Changed:
--- ---
- threaded functions no longer auto detect the presence of arguments when within a threaded function. However, you can use the holup method to produce the same effect. If you plan on using a function in different ways then you can use .wait() and .connect() without setting the holup argument
- thread:newFunction(func,holup) -- Added an argument holup to always force the threaded funcion to wait. Meaning you don't need to tell it to func().wait() or func().connect() - thread:newFunction(func,holup) -- Added an argument holup to always force the threaded funcion to wait. Meaning you don't need to tell it to func().wait() or func().connect()
- multi:newConnection(protect,callback,kill) -- Added the kill argument. Makes connections work sort of like a stack. Pop off the connections as they get called. So a one time connection handler. - multi:newConnection(protect,callback,kill) -- Added the kill argument. Makes connections work sort of like a stack. Pop off the connections as they get called. So a one time connection handler.
- I'm not sure callback has been documented in any form. callback gets called each and everytime conn:Fire() gets called! As well as being triggered for each connfunc that is part of the connection. - I'm not sure callback has been documented in any form. callback gets called each and everytime conn:Fire() gets called! As well as being triggered for each connfunc that is part of the connection.
@ -195,6 +196,7 @@ Removed:
Fixed: Fixed:
--- ---
- Issue where connections object:conn() was firing based on the existance of a Type field. Now this only fires if the table contains a reference to itself. Otherwise it will connect instead of firing
- Issue where async functions connect wasn't properly triggering when a function returned - Issue where async functions connect wasn't properly triggering when a function returned
- Issue where async functions were not passing arguments properly. - Issue where async functions were not passing arguments properly.
- Issue where async functions were not handling errors properly - Issue where async functions were not handling errors properly

View File

@ -783,8 +783,13 @@ function multi:newConnection(protect,func,kill)
c.lock = false c.lock = false
setmetatable(c,{__call=function(self,...) setmetatable(c,{__call=function(self,...)
local t = ... local t = ...
if type(t)=="table" and t.Type ~= nil then if type(t)=="table" then
return self:Fire(args,select(2,...)) for i,v in pairs(t) do
if v==self then
return self:Fire(...)
end
end
return self:connect(...)
else else
return self:connect(...) return self:connect(...)
end end
@ -1559,41 +1564,18 @@ function multi:newThread(name,func,...)
if type(name) == "function" then if type(name) == "function" then
name = "Thread#"..threadCount name = "Thread#"..threadCount
end end
-- local env = {} local env = {}
-- setmetatable(env,{ setmetatable(env,{
-- __index = Gref, __index = Gref,
-- __newindex = function(t,k,v) __newindex = function(t,k,v)
-- if type(v)=="function" then if type(v)=="function" then
-- rawset(t,k,thread:newFunction(v)) rawset(t,k,thread:newFunction(v))
-- else else
-- if type(v)=="table" then Gref[k]=v
-- if v.isTFunc then end
-- if not _G["_stack_"] or #_G["_stack_"]==0 then end
-- _G["_stack_"] = {} })
-- local s = _G["_stack_"] setfenv(func,env)
-- local a,b,c,d,e,f,g = v.wait(true)
-- table.insert(s,a)
-- table.insert(s,b)
-- table.insert(s,c)
-- table.insert(s,d)
-- table.insert(s,e)
-- table.insert(s,f)
-- local x = table.remove(_G["_stack_"])
-- rawset(t,k,x)
-- else
-- local x = table.remove(_G["_stack_"])
-- rawset(t,k,x)
-- end
-- else
-- Gref[k]=v
-- end
-- else
-- Gref[k]=v
-- end
-- end
-- end
-- })
-- setfenv(func,env)
local c={} local c={}
c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil} c.TempRets = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil}
c.startArgs = {...} c.startArgs = {...}

View File

@ -1,24 +1,78 @@
package.path="?.lua;?/init.lua;?.lua;"..package.path package.path="?.lua;?/init.lua;?.lua;"..package.path
local multi, thread = require("multi"):init() local multi, thread = require("multi"):init()
func = thread:newFunction(function() function multi:newService(func) -- Priority managed threads
thread.sleep(math.random(1,3)) local c = {}
local t = math.random(1,10) c.Type = "Service"
return t c.OnError = multi:newConnection()
c.OnStopped = multi:newConnection()
c.OnStarted = multi:newConnection()
local data = {}
local active = false
local time = multi:newTimer()
local p = multi.Priority_Normal
local scheme = 1
local function process()
thread.hold(function()
return active
end) end)
func().connect(function(a) func(c,data)
print(a) if scheme == 1 then
if (p^(1/3))/10 == .1 then
thread.yield()
else
thread.sleep((p^(1/3))/10)
end
elseif scheme == 2 then
thread.skip(math.abs(p-1)*32+1)
end
end
multi:newThread(function()
while true do
process()
end
end).OnError = c.OnError -- use the threads onerror as our own
function c.SetScheme(n)
scheme = n
end
function c.Stop()
c:OnStopped(c)
time:Reset()
time:Pause()
data = {}
time = {}
active = false
end
function c.Pause()
time:Pause()
active = false
end
function c.Resume()
time:Resume()
active = true
end
function c.Start()
c:OnStarted(c)
time:Start()
active = true
end
function c.getUpTime()
return time:Get()
end
function c.setPriority(pri)
p = pri
end
return c
end
serv = multi:newService(function(self,data)
thread.sleep(1)
error("sorry i crashed :'(")
end) end)
func().connect(function(a) serv.OnError(function(...)
print(a) print(...)
end) end)
func().connect(function(a) serv.OnStarted(function(t)
print(a) print("Started!",t.Type)
end) end)
func().connect(function(a) serv:Start()
print(a) serv:setPriority(multi.Priority_Idle)
end)
func().connect(function(a)
print(a)
end)
--os.exit()
multi:mainloop() multi:mainloop()