FileManager/ReaderUI: Clarify the current instance accessor (#7658)

* FileManager/ReaderUI: Clarify the current instance accessor

Make it clearer that we actually store it in a *module/class* member, not an *instance* member.

Also, warn if there's a close/open mismatch.
This commit is contained in:
NiLuJe
2021-05-11 00:49:35 +02:00
committed by GitHub
parent 939fcf7f0e
commit de6f2e84a3
5 changed files with 65 additions and 40 deletions

View File

@@ -567,12 +567,12 @@ function ReaderUI:showReaderCoroutine(file, provider)
end)
end
local _running_instance = nil
function ReaderUI:doShowReader(file, provider)
logger.info("opening file", file)
-- Keep only one instance running
if _running_instance then
_running_instance:onClose()
-- Only keep a single instance running
if ReaderUI.instance then
logger.warn("ReaderUI instance mismatch! Tried to spin up a new instance, while we still have an existing one:", tostring(ReaderUI.instance))
ReaderUI.instance:onClose()
end
local document = DocumentRegistry:openDocument(file, provider)
if not document then
@@ -621,11 +621,21 @@ function ReaderUI:doShowReader(file, provider)
end
UIManager:show(reader, "full")
_running_instance = reader
if ReaderUI.instance == nil then
logger.dbg("Spinning up new ReaderUI instance", tostring(reader))
else
-- Should never happen, given what we did above...
logger.warn("ReaderUI instance mismatch! Opened", tostring(reader), "while we still have an existing instance:", tostring(ReaderUI.instance))
end
ReaderUI.instance = reader
end
-- NOTE: The instance reference used to be stored in a private module variable, hence the getter method.
-- We've since aligned behavior with FileManager, which uses a class member instead,
-- but kept the function to avoid changing existing code.
function ReaderUI:_getRunningInstance()
return _running_instance
return ReaderUI.instance
end
function ReaderUI:unlockDocumentWithPassword(document, try_again)
@@ -741,9 +751,16 @@ function ReaderUI:onClose(full_refresh)
self:notifyCloseDocument()
end
UIManager:close(self.dialog, full_refresh and "full")
if _running_instance == self then
_running_instance = nil
end
function ReaderUI:onCloseWidget()
if ReaderUI.instance == self then
logger.dbg("Tearing down ReaderUI", tostring(self))
else
logger.warn("ReaderUI instance mismatch! Closed", tostring(self), "while the active one is supposed to be", tostring(ReaderUI.instance))
end
ReaderUI.instance = nil
self._coroutine = nil
end
function ReaderUI:dealWithLoadDocumentFailure()