diff --git a/Zframework/clipboard.lua b/Zframework/clipboard.lua index 93c3c99e..8e80cd0e 100644 --- a/Zframework/clipboard.lua +++ b/Zframework/clipboard.lua @@ -17,11 +17,12 @@ local function _sanitize(content) end if SYSTEM~='Web' then - local get = love.system.getClipboardText - local set = love.system.setClipboardText + local get=love.system.getClipboardText + local set=love.system.setClipboardText return { get=function() return get() or '' end, set=function(content) set(_sanitize(content)) end, + setFreshInterval=NULL, _update=NULL, } end @@ -29,7 +30,7 @@ end if WEB_COMPAT_MODE then local _clipboardBuffer='' return { - get=function () + get=function() JS.newPromiseRequest( JS.stringFunc( [[ @@ -43,13 +44,13 @@ if WEB_COMPAT_MODE then ]] ), function(data) _clipboardBuffer=data end, - function(id, error) print(id, error) end, + function(id,error) print(id, error) end, 3, 'getClipboardText' ) return _clipboardBuffer end, - set=function (str) + set=function(str) JS.callJS(JS.stringFunc( [[ window.navigator.clipboard @@ -60,26 +61,30 @@ if WEB_COMPAT_MODE then _sanitize(str) )) end, + setFreshInterval=NULL, _update=NULL, } end -local clipboard_thread=love.thread.newThread('Zframework/clipboard_thread.lua') -local getCHN=love.thread.newChannel() -local setCHN=love.thread.newChannel() -local triggerCHN=love.thread.newChannel() +local getCHN=love.thread.getChannel('CLIP_get') +local setCHN=love.thread.getChannel('CLIP_set') +local trigCHN=love.thread.getChannel('CLIP_trig') -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, set=function(content) setCHN:push(_sanitize(content)) end, - _update=function() - triggerCHN:push(0) - local error = clipboard_thread:getError() - if error then - MES.new('error',error) - MES.traceback() + setFreshInterval=function(val) + freshInterval=val + end, + _update=function(dt) + timer=timer+dt + if timer>freshInterval then + timer=0 + trigCHN:push(0) end end, } diff --git a/Zframework/clipboard_thread.lua b/Zframework/clipboard_thread.lua index eace600d..f53ccfda 100644 --- a/Zframework/clipboard_thread.lua +++ b/Zframework/clipboard_thread.lua @@ -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 diff --git a/Zframework/http.lua b/Zframework/http.lua index 55484e64..4576c661 100644 --- a/Zframework/http.lua +++ b/Zframework/http.lua @@ -1,5 +1,5 @@ -local sendCHN=love.thread.getChannel('inputChannel') -local recvCHN=love.thread.getChannel('outputChannel') +local sendCHN=love.thread.getChannel('HTTP_inputChannel') +local recvCHN=love.thread.getChannel('HTTP_outputChannel') local threads={} local threadCount=0 @@ -9,11 +9,15 @@ local threadCode=[[ local http=require'socket.http' local ltn12=require'ltn12' - local sendCHN=love.thread.getChannel('inputChannel') - local recvCHN=love.thread.getChannel('outputChannel') + local sendCHN=love.thread.getChannel('HTTP_inputChannel') + local recvCHN=love.thread.getChannel('HTTP_outputChannel') + local sleep=require'love.timer'.sleep 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 recvCHN:push{ diff --git a/Zframework/init.lua b/Zframework/init.lua index f54ac342..5faa2e47 100644 --- a/Zframework/init.lua +++ b/Zframework/init.lua @@ -741,9 +741,9 @@ function love.run() -- UPDATE STEP() - if JS then + if JS then JS.retrieveData(dt) - CLIPBOARD._update() + CLIPBOARD._update(dt) end if mouseShow then mouse_update(dt) end if next(jsState) then gp_update(jsState[1],dt) end diff --git a/Zframework/websocket_thread.lua b/Zframework/websocket_thread.lua index 742f1d72..6588577d 100644 --- a/Zframework/websocket_thread.lua +++ b/Zframework/websocket_thread.lua @@ -1,3 +1,4 @@ +---@type love.Channel,love.Channel,love.Channel local triggerCHN,sendCHN,readCHN=... 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 JSON=require'Zframework.json' +local sleep=require'love.timer'.sleep do-- Connect - local host=CHN_demand(sendCHN) - local port=CHN_demand(sendCHN) - local path=CHN_demand(sendCHN) - local head=CHN_demand(sendCHN) - local timeout=CHN_demand(sendCHN) + -- Warning: workaround for love.js, used to use CHN_demand instead + while CHN_getCount(sendCHN)<5 do sleep(.0626) end + local host=CHN_pop(sendCHN) + local port=CHN_pop(sendCHN) + local path=CHN_pop(sendCHN) + local head=CHN_pop(sendCHN) + local timeout=CHN_pop(sendCHN) SOCK:settimeout(timeout) local res,err=SOCK:connect(host,port) @@ -186,7 +190,8 @@ end) local success,err while true do-- Running - CHN_demand(triggerCHN) + while CHN_getCount(triggerCHN)==0 do sleep(.0626) end + CHN_pop(triggerCHN) success,err=pcall(sendThread) if not success or err then break end success,err=pcall(readThread)