移除所有的Channel:demand,因为love.js不支持

整理代码
This commit is contained in:
MrZ_26
2024-11-02 05:52:36 +08:00
parent f2e064e900
commit 785935d26f
5 changed files with 78 additions and 84 deletions

View File

@@ -17,11 +17,12 @@ local function _sanitize(content)
end end
if SYSTEM~='Web' then if SYSTEM~='Web' then
local get = love.system.getClipboardText local get=love.system.getClipboardText
local set = love.system.setClipboardText local set=love.system.setClipboardText
return { return {
get=function() return get() or '' end, get=function() return get() or '' end,
set=function(content) set(_sanitize(content)) end, set=function(content) set(_sanitize(content)) end,
setFreshInterval=NULL,
_update=NULL, _update=NULL,
} }
end end
@@ -29,7 +30,7 @@ end
if WEB_COMPAT_MODE then if WEB_COMPAT_MODE then
local _clipboardBuffer='' local _clipboardBuffer=''
return { return {
get=function () get=function()
JS.newPromiseRequest( JS.newPromiseRequest(
JS.stringFunc( JS.stringFunc(
[[ [[
@@ -43,13 +44,13 @@ if WEB_COMPAT_MODE then
]] ]]
), ),
function(data) _clipboardBuffer=data end, function(data) _clipboardBuffer=data end,
function(id, error) print(id, error) end, function(id,error) print(id, error) end,
3, 3,
'getClipboardText' 'getClipboardText'
) )
return _clipboardBuffer return _clipboardBuffer
end, end,
set=function (str) set=function(str)
JS.callJS(JS.stringFunc( JS.callJS(JS.stringFunc(
[[ [[
window.navigator.clipboard window.navigator.clipboard
@@ -60,26 +61,30 @@ if WEB_COMPAT_MODE then
_sanitize(str) _sanitize(str)
)) ))
end, end,
setFreshInterval=NULL,
_update=NULL, _update=NULL,
} }
end end
local clipboard_thread=love.thread.newThread('Zframework/clipboard_thread.lua') local getCHN=love.thread.getChannel('CLIP_get')
local getCHN=love.thread.newChannel() local setCHN=love.thread.getChannel('CLIP_set')
local setCHN=love.thread.newChannel() local trigCHN=love.thread.getChannel('CLIP_trig')
local triggerCHN=love.thread.newChannel()
clipboard_thread:start(getCHN,setCHN,triggerCHN) love.thread.newThread('Zframework/clipboard_thread.lua'):start()
return{ local freshInterval=1
local timer=0
return {
get=function() return getCHN:peek() or '' end, get=function() return getCHN:peek() or '' end,
set=function(content) setCHN:push(_sanitize(content)) end, set=function(content) setCHN:push(_sanitize(content)) end,
_update=function() setFreshInterval=function(val)
triggerCHN:push(0) freshInterval=val
local error = clipboard_thread:getError() end,
if error then _update=function(dt)
MES.new('error',error) timer=timer+dt
MES.traceback() if timer>freshInterval then
timer=0
trigCHN:push(0)
end end
end, end,
} }

View File

@@ -1,65 +1,45 @@
local getCHN,setCHN,triggerCHN=... local getCHN=love.thread.getChannel('CLIP_get')
local setCHN=love.thread.getChannel('CLIP_set')
local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount local trigCHN=love.thread.getChannel('CLIP_trig')
local CHN_push,CHN_pop=triggerCHN.push,triggerCHN.pop
JS=require'Zframework.js' JS=require'Zframework.js'
love.timer=require'love.timer'
local yield=coroutine.yield while true do
local setThread=coroutine.wrap(function() if trigCHN:getCount()>0 then
while true do trigCHN:pop()
JS.callJS(JS.stringFunc( if setCHN:getCount()>0 then
[[ repeat setCHN:pop() until setCHN:getCount()==1
window.navigator.clipboard -- Set Clipboard
.writeText('%s') JS.callJS(JS.stringFunc(
.then(() => console.log('Copied to clipboard'))
.catch((e) => console.warn(e));
]],
CHN_pop(setCHN)
))
yield()
end
end)
local getThread=coroutine.wrap(function()
while true do
JS.newPromiseRequest(
JS.stringFunc(
[[ [[
window.navigator.clipboard window.navigator.clipboard
.readText() .writeText('%s')
.then((text) => _$_(text)) .then(() => console.log('Copied to clipboard'))
.catch((e) => { .catch((e) => console.warn(e));
console.warn(e); ]],
_$_(''); setCHN:pop()
}); ))
]] end
), -- Get Clipboard
function(data) JS.newPromiseRequest(
while getCHN:getCount()>0 do JS.stringFunc[[
CHN_pop(getCHN) window.navigator.clipboard
end .readText()
CHN_push(getCHN, data) .then((text) => _$_(text))
.catch((e) => {
console.warn(e);
_$_('');
});
]],
function(data)
while getCHN:getCount()>0 do getCHN:pop() end
getCHN:push(data)
end, end,
function(id, error) print(id, error) end, function(id,error) print(id, error) end,
2, 2,
'getClipboardText' 'getClipboardText'
) )
yield()
end end
end) love.timer.sleep(.0626)
local success,err
while true do-- Running
CHN_demand(triggerCHN)
if CHN_getCount(setCHN)>0 then
while CHN_getCount(setCHN)>1 do
CHN_pop(setCHN)
end
print('Running setThread')
setThread()
end
print('Running getThread')
getThread()
end end

View File

@@ -1,5 +1,5 @@
local sendCHN=love.thread.getChannel('inputChannel') local sendCHN=love.thread.getChannel('HTTP_inputChannel')
local recvCHN=love.thread.getChannel('outputChannel') local recvCHN=love.thread.getChannel('HTTP_outputChannel')
local threads={} local threads={}
local threadCount=0 local threadCount=0
@@ -9,11 +9,15 @@ local threadCode=[[
local http=require'socket.http' local http=require'socket.http'
local ltn12=require'ltn12' local ltn12=require'ltn12'
local sendCHN=love.thread.getChannel('inputChannel') local sendCHN=love.thread.getChannel('HTTP_inputChannel')
local recvCHN=love.thread.getChannel('outputChannel') local recvCHN=love.thread.getChannel('HTTP_outputChannel')
local sleep=require'love.timer'.sleep
while true do while true do
local arg=sendCHN:demand() -- local arg=sendCHN:demand()
-- Warning: workaround for love.js
while sendCHN:getCount()==0 do sleep(.0626) end
local arg=sendCHN:pop()
if arg._destroy then if arg._destroy then
recvCHN:push{ recvCHN:push{

View File

@@ -741,9 +741,9 @@ function love.run()
-- UPDATE -- UPDATE
STEP() STEP()
if JS then if JS then
JS.retrieveData(dt) JS.retrieveData(dt)
CLIPBOARD._update() CLIPBOARD._update(dt)
end end
if mouseShow then mouse_update(dt) end if mouseShow then mouse_update(dt) end
if next(jsState) then gp_update(jsState[1],dt) end if next(jsState) then gp_update(jsState[1],dt) end

View File

@@ -1,3 +1,4 @@
---@type love.Channel,love.Channel,love.Channel
local triggerCHN,sendCHN,readCHN=... local triggerCHN,sendCHN,readCHN=...
local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount
@@ -5,13 +6,16 @@ local CHN_push,CHN_pop=triggerCHN.push,triggerCHN.pop
local SOCK=require'socket'.tcp() local SOCK=require'socket'.tcp()
local JSON=require'Zframework.json' local JSON=require'Zframework.json'
local sleep=require'love.timer'.sleep
do-- Connect do-- Connect
local host=CHN_demand(sendCHN) -- Warning: workaround for love.js, used to use CHN_demand instead
local port=CHN_demand(sendCHN) while CHN_getCount(sendCHN)<5 do sleep(.0626) end
local path=CHN_demand(sendCHN) local host=CHN_pop(sendCHN)
local head=CHN_demand(sendCHN) local port=CHN_pop(sendCHN)
local timeout=CHN_demand(sendCHN) local path=CHN_pop(sendCHN)
local head=CHN_pop(sendCHN)
local timeout=CHN_pop(sendCHN)
SOCK:settimeout(timeout) SOCK:settimeout(timeout)
local res,err=SOCK:connect(host,port) local res,err=SOCK:connect(host,port)
@@ -186,7 +190,8 @@ end)
local success,err local success,err
while true do-- Running while true do-- Running
CHN_demand(triggerCHN) while CHN_getCount(triggerCHN)==0 do sleep(.0626) end
CHN_pop(triggerCHN)
success,err=pcall(sendThread) success,err=pcall(sendThread)
if not success or err then break end if not success or err then break end
success,err=pcall(readThread) success,err=pcall(readThread)