2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/spec/unit/commonrequire.lua
poire-z 962fd02c98
Tame BackgroundRunner: stop running when no more job (#6605)
A BackgroundRunner plugin instance will stop running
(rescheduling a check every 2 seconds) when there is no
(or no more) job to run.
Clients of this service now have to emit an event after
adding a job into PluginShare.backgroundJobs, so an
already loaded but stopped BackgroundRunner can notice
it and start running again.
2020-08-31 16:48:29 +02:00

112 lines
3.2 KiB
Lua

-- don't try to overwrite metatables so we can use --auto-insulate-tests
-- shamelessly copied from https://github.com/Olivine-Labs/busted/commit/db6d8b4be8fd099ab387efeb8232cfd905912abb
local ffi = require "ffi"
local old_metatype = ffi.metatype
local exists = {}
ffi.metatype = function(def, mttable)
if exists[def] then return exists[def] end
exists[def] = old_metatype(def, mttable)
return exists[def]
end
require "defaults"
package.path = "?.lua;common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path
package.cpath = "?.so;common/?.so;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" .. package.cpath
-- turn off debug by default and set log level to warning
require("dbg"):turnOff()
local logger = require("logger")
logger:setLevel(logger.levels.warn)
-- global reader settings
local DataStorage = require("datastorage")
os.remove(DataStorage:getDataDir().."/settings.reader.lua")
G_reader_settings = require("luasettings"):open(".reader")
-- global einkfb for Screen (do not show SDL window)
einkfb = require("ffi/framebuffer") --luacheck: ignore
einkfb.dummy = true --luacheck: ignore
local Device = require("device")
-- init output device
local Screen = Device.screen
Screen:init()
local CanvasContext = require("document/canvascontext")
CanvasContext:init(Device)
-- init input device (do not show SDL window)
local Input = Device.input
Input.dummy = true
package.unload = function(module)
if type(module) ~= "string" then return false end
package.loaded[module] = nil
_G[module] = nil
return true
end
package.replace = function(name, module)
if type(name) ~= "string" then return false end
assert(package.unload(name))
package.loaded[name] = module
return true
end
package.reload = function(name)
if type(name) ~= "string" then return false end
assert(package.unload(name))
return require(name)
end
package.unloadAll = function()
local candidates = {
"spec/",
"frontend/",
"plugins/",
"datastorage.lua",
"defaults.lua",
}
local pending = {}
for name, _ in pairs(package.loaded) do
local path = package.searchpath(name, package.path)
if path ~= nil then
for _, candidate in ipairs(candidates) do
if path:find(candidate) == 1 then
table.insert(pending, name)
end
end
end
end
for _, name in ipairs(pending) do
if name ~= "commonrequire" then
assert(package.unload(name))
end
end
return #pending
end
local background_runner
requireBackgroundRunner = function()
require("pluginshare").stopBackgroundRunner = nil
if background_runner == nil then
local package_path = package.path
package.path = "plugins/backgroundrunner.koplugin/?.lua;" .. package.path
background_runner = dofile("plugins/backgroundrunner.koplugin/main.lua")
package.path = package_path
end
return background_runner
end
stopBackgroundRunner = function()
background_runner = nil
require("pluginshare").stopBackgroundRunner = true
end
notifyBackgroundJobsUpdated = function()
if background_runner then
background_runner:onBackgroundJobsUpdated()
end
end