移除所有的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

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