signage: bring in from external repo
This is b28e6f07aa48f1e2f01eb37bffa180f97a7b03bd from
https://code.hackerspace.pl/q3k/love2d-signage/. We only keep code
commited by inf and q3k, and we're both now licensing this code under
the ISC license, as per COPYING in the root of hscloud.
Change-Id: Ibeee2e6923605e4b1a17a1d295867c056863ef59
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1335
Reviewed-by: informatic <informatic@hackerspace.pl>
Reviewed-by: q3k <q3k@hackerspace.pl>
diff --git a/hswaw/signage/core/thread-node.lua b/hswaw/signage/core/thread-node.lua
new file mode 100644
index 0000000..e8498dd
--- /dev/null
+++ b/hswaw/signage/core/thread-node.lua
@@ -0,0 +1,40 @@
+local ThreadNode = Node:extend('ThreadNode', {
+ threadFile = nil,
+ threadChannel = nil,
+ updateInterval = 5,
+ lastUpdate = 0,
+ state = nil,
+})
+
+function ThreadNode:update(dt)
+ if not self.state then self:checkUpdate() end
+end
+
+function ThreadNode:checkUpdate()
+ if self.threadFile and self.threadChannel then
+ if self.lastUpdate < love.timer.getTime() - self.updateInterval or
+ (not self.state and self.lastUpdate < love.timer.getTime() - 5) then
+ self.lastUpdate = love.timer.getTime()
+ print(self.threadChannel, "Updating...")
+
+ local updateThread = love.thread.newThread(self.threadFile)
+ updateThread:start()
+ end
+
+ local v = love.thread.getChannel(self.threadChannel):pop()
+ if v then
+ self:onUpdate(v)
+ end
+ end
+end
+
+function ThreadNode:afterExit()
+ print('exit')
+ self:checkUpdate()
+end
+
+function ThreadNode:onUpdate(v)
+ self.state = v
+end
+
+return ThreadNode