diff --git a/frontend/httpclient.lua b/frontend/httpclient.lua index fb2668435..0ee112b1e 100644 --- a/frontend/httpclient.lua +++ b/frontend/httpclient.lua @@ -17,7 +17,7 @@ function HTTPClient:request(request, response_callback) request.connect_timeout = 10 request.request_timeout = 20 UIManager:initLooper() - UIManager:handleTask(function() + UIManager.looper:add_callback(function() -- avoid endless waiting for input UIManager.INPUT_TIMEOUT = self.INPUT_TIMEOUT self.input_timeouts = self.input_timeouts + 1 diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index 44c42845f..880491d13 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -498,24 +498,14 @@ function UIManager:handleInput() end end - -- handle next input - self:handleTask(function() self:handleInput() end) -end - --- handle task(callback function) in Turbo I/O looper --- or run task immediately if looper is not available -function UIManager:handleTask(task) if self.looper then - DEBUG("handle task in turbo I/O looper") - self.looper:add_callback(task) - else - DEBUG("run task") - task() + DEBUG("handle input in turbo I/O looper") + self.looper:add_callback(function() self:handleInput() end) end end function UIManager:initLooper() - if not self.looper then + if not self.looper and not Device.isAndroid() and ffi.os ~= "Windows" then TURBO_SSL = true local turbo = require("turbo") self.looper = turbo.ioloop.instance() @@ -527,11 +517,15 @@ end -- them to dialogs function UIManager:run() self._running = true - if ffi.os == "Windows" then - self:handleInput() + self:initLooper() + -- currently there is no Turbo support for Android and Windows + -- use our own main loop + if not self.looper then + while self._running do + self:handleInput() + end else - self:initLooper() - self:handleTask(function() self:handleInput() end) + self.looper:add_callback(function() self:handleInput() end) self.looper:start() end end diff --git a/plugins/kosync.koplugin/KOSyncClient.lua b/plugins/kosync.koplugin/KOSyncClient.lua index 98ca2a2ab..b2b645e31 100644 --- a/plugins/kosync.koplugin/KOSyncClient.lua +++ b/plugins/kosync.koplugin/KOSyncClient.lua @@ -25,13 +25,13 @@ function KOSyncClient:init() req.headers['x-auth-user'] = args.username req.headers['x-auth-key'] = args.userkey end - local HTTPClient = require("httpclient") - local async_http_client = HTTPClient:new() package.loaded['Spore.Middleware.AsyncHTTP'] = {} require('Spore.Middleware.AsyncHTTP').call = function(args, req) + -- disable async http if Turbo looper is missing + if not UIManager.looper then return end req:finalize() local result - async_http_client:request({ + require("httpclient"):new():request({ url = req.url, method = req.method, body = req.env.spore.payload, @@ -47,8 +47,6 @@ function KOSyncClient:init() -- Turbo HTTP client uses code instead of status -- change to status so that Spore can understand result.status = res.code - -- fallback to sync http request - if result.error then result = nil end coroutine.resume(args.thread) end) return coroutine.create(function() coroutine.yield(result) end) @@ -119,7 +117,7 @@ function KOSyncClient:update_progress(username, password, end) self.client:enable("AsyncHTTP", {thread = co}) coroutine.resume(co) - UIManager.INPUT_TIMEOUT = 100 + if UIManager.looper then UIManager.INPUT_TIMEOUT = 100 end end function KOSyncClient:get_progress(username, password, @@ -146,7 +144,7 @@ function KOSyncClient:get_progress(username, password, end) self.client:enable("AsyncHTTP", {thread = co}) coroutine.resume(co) - UIManager.INPUT_TIMEOUT = 100 + if UIManager.looper then UIManager.INPUT_TIMEOUT = 100 end end return KOSyncClient