-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadCommand.lua
More file actions
62 lines (50 loc) · 1.51 KB
/
threadCommand.lua
File metadata and controls
62 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local PATH = (...):match("^(.-)[^%.]+$")
local le = love.event
local mintmousse = require(PATH .. "conf")
local codec = require(PATH .. "codec")
local loggerCommand = mintmousse._logger:extend("Command")
local threadCommand = {
commandQueue = love.thread.getChannel(mintmousse.THREAD_COMMAND_QUEUE_ID),
eventQueue = mintmousse.THREAD_RESPONSE_QUEUE_ID,
}
threadCommand.batchStart = function()
if threadCommand._batch then
loggerCommand:warning("batchStart called while a batch is already active. Ignoring nested call.")
else
threadCommand._batch = { }
end
end
threadCommand.batchEnd = function()
if not threadCommand._batch then
loggerCommand:warning("batchEnd called without a matching batchStart. No batch to close.")
return
end
local batch = threadCommand._batch
threadCommand._batch = nil
if #batch ~= 0 then
threadCommand.call("batch", batch)
end
end
threadCommand.push = function(message)
if threadCommand._batch then
table.insert(threadCommand._batch, message)
return
end
threadCommand.commandQueue:push(codec.encode(message))
end
threadCommand.call = function(func, args)
threadCommand.push({ func = func, args = args })
end
if isMintMousseThread then
threadCommand.pop = function()
local encodedMessage = threadCommand.commandQueue:pop()
if not encodedMessage then
return
end
return codec.decode(encodedMessage)
end
threadCommand.pushEvent = function(enum, ...)
le.push(threadCommand.eventQueue, enum, ...)
end
end
return threadCommand