Skip to content

Commit

Permalink
More types.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagolove committed Feb 10, 2021
1 parent 7c3abbe commit 4797c31
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 73 deletions.
70 changes: 70 additions & 0 deletions coroprocessor.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local resume = coroutine.resume

global type CoroProcessor = record
coros: {string:{thread}}
messages: {string:{string}}

new: function(): CoroProcessor
sendMessage: function(queuename: string, message: string)
push: function(queuename: string, func: function, ...: any)
update: function()
end

local CoroProcessor_mt: metatable<CoroProcessor> = {
__index = CoroProcessor
}

function CoroProcessor.new(): CoroProcessor
local self = setmetatable({} as CoroProcessor, CoroProcessor_mt)
self.coros = {}
self.messages = {}
return self
end

function CoroProcessor:sendMessage(queuename: string, message: string)
local tbl = self.messages[queuename]
if tbl then
table.insert(tbl, message)
end
end

function CoroProcessor:push(queuename: string, func: function, ...: any)
local q = self.coros[queuename]
if not q then
self.coros[queuename] = {}
self.messages[queuename] = {}
q = self.coros[queuename]
end
table.insert(q, coroutine.create(func))
if select("#", ...) ~= 0 then
resume(q[#q], ...)
end
end

function CoroProcessor:update()
for k, v in pairs(self.coros) do
if #v >= 1 then
local msgs = self.messages[k]
local msg: string
if #msgs >= 1 then
msg = msgs[1]
table.remove(msgs, 1)
end
local ret: any
if msg then
ret = resume(v[1], msg)
else
ret = resume(v[1])
end
if not ret then
table.remove(v, 1)
if v[1] then
resume(v[1])
end
end
end
end
end

return CoroProcessor

10 changes: 10 additions & 0 deletions globals.tl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
require "nback"
require "menu"
require "pviewer"
require "help"

global menu: Menu
global pviewer: Pviewer
global help: Help
global nback: Nback

local function initGlobals()
global menu = require "menu".new()
global pviewer = require "pviewer".new()
Expand Down
7 changes: 7 additions & 0 deletions layout.tl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ global type Layout = record
middle: Layout
left: Layout
right: Layout
leftTop: Layout
leftMiddle: Layout
leftBottom: Layout
rightTop: Layout
rightMiddle: Layout
rightBottom: Layout
center: Layout
end

local Layout_mt: metatable<Layout> = {
Expand Down
Loading

0 comments on commit 4797c31

Please sign in to comment.