cleaning up and documenting

This commit is contained in:
Ryan Ward 2020-03-12 15:38:42 -04:00
parent 2d281fae90
commit dee687f4b1
5 changed files with 83 additions and 148 deletions

View File

@ -15,6 +15,7 @@ Current Multi Version: 14.2.0
# Multi Runners
`multi:lightloop()` — A light version of the mainloop
</br>`multi:loveloop([BOOLEAN: light true])` — Run's all the love related features as well
</br>`multi:mainloop([TABLE settings])` — This runs the mainloop by having its own internal while loop running
</br>`multi:threadloop([TABLE settings])` — This runs the mainloop by having its own internal while loop running, but prioritizes threads over multi-objects
</br>`multi:uManager([TABLE settings])` — This runs the mainloop, but does not have its own while loop and thus needs to be within a loop of some kind.
@ -445,7 +446,7 @@ Helpful methods are wrapped around the builtin coroutine module which make it fe
<b>\*</b>A note about multi.NIL, this should only be used within the hold and hold like methods. thread.hold(), thread.holdFor(), and thread.holdWithin() methods. This is not needed within threaded functions! The reason hold prevents nil and false is because it is testing for a condition so the first argument needs to be non nil nor false! multi.NIL should not be used anywhere else. Sometimes you may need to pass a 'nil' value or return. While you could always return true or something you could use multi.NIL to force a nil value through a hold like method.
# CBT: newService(FUNCTION: func)
`serv = newService(FUNCTION: func(self,TABLE: data))`
`serv = newService(FUNCTION: func(self,TABLE: data))` — func is called each time the service is updated think of it like a loop multi-obj. self is the service object and data is a private table that only the service can see.
- `serv.OnError(FUNCTION: func)` — connection that fired if there is an error
- `serv.OnStopped(FUNCTION: func(serv))` — connection that is fired when a service is stopped
- `serv.OnStarted(FUNCTION: func(serv))` — connection that is fired when a service is started
@ -469,6 +470,71 @@ Helpful methods are wrapped around the builtin coroutine module which make it fe
- `2` — uses a cycle based style of yielding. thread.skip()
- `CONVERTS(serv) = serv.Destroy()` — Stops the service then Destroys the service triggering all events! The service becomes a destroyed object
Example:
```lua
-- Jobs are not natively part of the multi library. I planned on adding them, but decided against it. Below is the code that would have been used.
-- Implementing a job manager using services
package.path="?/init.lua;?.lua;"..package.path
local multi = require("multi")
multi.Jobs = multi:newService(function(self,jobs)
local job = table.remove(jobs,1)
if job and job.removed==nil then
job.func()
end
end)
multi.Jobs.OnStarted(function(self,jobs)
function self:newJob(func,name)
table.insert(jobs,{
func = func,
name = name,
removeJob = function(self) self.removed = true end
})
end
function self:getJobs(name)
local tab = {}
if not name then return jobs end
for i=1,#jobs do
if name == jobs[i].name then
table.insert(tab,jobs[i])
end
end
return tab
end
function self:removeJobs(name)
for i=1,#jobs do
if name ~= nil and name == jobs[i].name then
jobs[i]:removeJob()
elseif name == nil then
jobs[i]:removeJob()
end
end
end
end)
multi.Jobs.SetPriority(multi.Priority_Normal)
multi.Jobs.Start()
-- Testing job stuff
function pushJobs()
multi.Jobs:newJob(function()
print("job called")
end) -- No name job
multi.Jobs:newJob(function()
print("job called2")
end,"test")
multi.Jobs:newJob(function()
print("job called3")
end,"test2")
end
pushJobs()
pushJobs()
local jobs = multi.Jobs:getJobs() -- gets all jobs
local jobsn = multi.Jobs:getJobs("test") -- gets all jobs names 'test'
jobsn[1]:removeJob() -- Select a job and remove it
multi.Jobs:removeJobs("test2") -- Remove all jobs names 'test2'
multi.Jobs.SetScheme(1) -- Jobs are internally a service, so setting scheme and priority
multi.Jobs.SetPriority(multi.Priority_Core)
```
# CBT: newThread()
`th = multi:newThread([STRING name,] FUNCTION func)` — Creates a new thread with name and function.

View File

@ -4,33 +4,13 @@ Table of contents
---
# Update 14.2.0 - Documentation [====100%====] Done
# Update 14.2.0 - Removing Bloat
Full Update Showcase
---
```lua
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
local multi,thread = require("multi"):init()
-- Testing job stuff
function pushJobs()
multi.Jobs:newJob(function()
print("job called")
end) -- No name job
multi.Jobs:newJob(function()
print("job called2")
end,"test")
multi.Jobs:newJob(function()
print("job called3")
end,"test2")
end
pushJobs()
pushJobs()
local jobs = multi.Jobs:getJobs() -- gets all jobs
local jobsn = multi.Jobs:getJobs("test") -- gets all jobs names 'test'
jobsn[1]:removeJob() -- Select a job and remove it
multi.Jobs:removeJobs("test2") -- Remove all jobs names 'test2'
multi.Jobs.SetScheme(1) -- Jobs are internally a service, so setting scheme and priority
multi.Jobs.SetPriority(multi.Priority_Core)
-- Testing destroying and fixed connections
c = multi:newConnection()
c1 = c(function()
@ -115,15 +95,10 @@ Changed:
conn = OnExample(...)
print(conn.Type) -- connector_link
```
- Revamped the job system — See Full Update Showcase
- multi.Jobs:newJob(func,name) — You do not need to set a name, but it might be useful
- multi.Jobs:getJobs(name) — Get all jobs, or all jobs with a certain name
- multi.Jobs:removeJobs(name) — Remove all jobs, or all jobs with a certain name
- multi.Jobs.SetPriority(Priority) — Set the priority of the Job service
- multi.Jobs.SetScheme(scheme) — Set the scheme of the Job service
Removed:
Removed: (Cleaning up a lot of old features)
---
- Removed multi:newProcessor(STRING: file) — Old feature that is not really needed anymore. Create your multi-objs on the multi object or use a thread
- bin dependency from the rockspec
- Example folder and .html variants of the .md files
- multi:newTrigger() — Connections do everything this thing could do and more.

View File

@ -70,7 +70,7 @@ multi.OnPreLoad(function()
end)
end)
function multi:loveloop()
function multi:loveloop(light)
local link
link = multi:newThread(function()
local mainloop = love.run()
@ -81,8 +81,13 @@ function multi:loveloop()
end).OnError(function(...)
print(...)
end)
multi:mainloop()
if light==false then
multi:mainloop()
else
multi:lightloop()
end
end
multi.OnQuit(function()
multi.Stop()
love.event.quit()

View File

@ -510,75 +510,6 @@ multi.OnObjectCreated=multi:newConnection()
multi.OnObjectDestroyed=multi:newConnection()
multi.OnLoad = multi:newConnection(nil,nil,true)
ignoreconn = false
function multi:newProcessor(file)
if not(self.Type=='mainprocess') then error('Can only create an interface on the multi obj') return false end
local c = {}
setmetatable(c, {__index = multi})
c.Parent=self
c.Active=true
c.func={}
c.Type='process'
c.Mainloop={}
c.Garbage={}
c.Children={}
c.Active=false
c.Rest=0
c.queue={}
c.l=self:newLoop(function(self,dt)
if self.link.Active then
c:uManager()
end
end)
c.l.link = c
c.l.Type = "processor"
function c:getController()
return c.l
end
function c:Start()
self.Active = true
return self
end
function c:Resume()
self.Active = false
return self
end
function c:setName(name)
c.l.Name = name
return self
end
function c:Pause()
if self.l then
self.l:Pause()
end
return self
end
function c:Remove()
if self.Type == "process" then
self:__Destroy()
self.l:Destroy()
else
self:__Destroy()
end
end
function c:Destroy()
if self == c then
self.l:Destroy()
else
for i = #c.Mainloop,1,-1 do
if c.Mainloop[i] == self then
table.remove(c.Mainloop,i)
break
end
end
end
end
if file then
self.Cself=c
loadstring('local process=multi.Cself '..io.open(file,'rb'):read('*all'))()
end
self:create(c)
return c
end
function multi:newTimer()
local c={}
c.Type='timer'
@ -1399,9 +1330,6 @@ function multi.initThreads(justThreads)
end
end
end
function multi:threadloop()
multi.initThreads(true)
end
function multi:newService(func) -- Priority managed threads
local c = {}
c.Type = "service"
@ -1493,43 +1421,10 @@ function multi:newService(func) -- Priority managed threads
multi.create(multi,c)
return c
end
multi.Jobs = multi:newService(function(self,jobs)
local job = table.remove(jobs,1)
if job and job.removed==nil then
job.func()
end
end)
multi.Jobs.OnStarted(function(self,jobs)
function self:newJob(func,name)
table.insert(jobs,{
func = func,
name = name,
removeJob = function(self) self.removed = true end
})
end
function self:getJobs(name)
local tab = {}
if not name then return jobs end
for i=1,#jobs do
if name == jobs[i].name then
table.insert(tab,jobs[i])
end
end
return tab
end
function self:removeJobs(name)
for i=1,#jobs do
if name ~= nil and name == jobs[i].name then
jobs[i]:removeJob()
elseif name == nil then
jobs[i]:removeJob()
end
end
end
end)
multi.Jobs.SetPriority(multi.Priority_Normal)
multi.Jobs.Start()
-- Multi runners
function multi:threadloop()
multi.initThreads(true)
end
function multi:lightloop()
if not isRunning then
local Loop=self.Mainloop

View File

@ -1,11 +1,5 @@
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
multi, thread = require("multi"):init()
func = thread:newFunction(function(a)
return thread.holdFor(3,function()
return a==5 and "This is returned" -- Condition being tested!
end)
end,true)
print(func(5))
print(func(0))
-- You actually do not need the light/mainloop or any runner for threaded functions to work
--multi:lightloop()
multi:lightloop()