2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/frontend/ui/plugin/background_task_plugin.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

39 lines
1010 B
Lua

--[[--
BackgroundTaskPlugin creates a plugin with a switch to enable or disable it and executes a
background task.
See spec/unit/background_task_plugin_spec.lua for the usage.
]]
local PluginShare = require("pluginshare")
local SwitchPlugin = require("ui/plugin/switch_plugin")
local BackgroundTaskPlugin = SwitchPlugin:extend()
function BackgroundTaskPlugin:_schedule(settings_id)
local enabled = function()
if not self.enabled then
return false
end
if settings_id ~= self.settings_id then
return false
end
return true
end
table.insert(PluginShare.backgroundJobs, {
when = self.when,
repeated = enabled,
executable = self.executable,
})
local Event = require("ui/event")
local UIManager = require("ui/uimanager")
UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated"))
end
function BackgroundTaskPlugin:_start()
self:_schedule(self.settings_id)
end
return BackgroundTaskPlugin