There doesn't seem to be any hook that would notify you when thathappens. One way to deal with it would be to lean on the garbagecollector somewhat and use weak references to the relevant objects.Then, in their finalizers, clear out the files in question. Additionally,you would want to make sure to clear out existing files during startupand probably shutdown.Downside of this is it's not predictable when an object is finalized, butthat may be enough for your use case.Another option is to use the session's cleanup hook to take care ofany associated files, but that's obviously less granular.
An update here: PR 1709 implements an `onFunctionOwnersRemoved` hook onLiftSession that can be used to watch for function owners being evicted due to functionbinding GC. We'll be merging it in the next day or two if we don't see any objections/remarks from other folks.Thanks,Antonio
object pageIdWatchers extends java.util.concurrent.ConcurrentHashMap[String, List[() => Unit]] with Loggable {
def removeCleanupFunc(functionOwner: String): Unit = {
debug(s"Removing cleanupFunc for $functionOwner")
remove(functionOwner)
}
def addCleanupFunc(func: () => Unit): Unit = {
S.currentCometActor.map(_.uniqueId -> "Comet").orElse(S.request.map(_ => S.renderVersion -> "Request")).foreach {
case (functionOwner, typeStr) =>
debug(s"Adding cleanupFunc for $typeStr $functionOwner")
put(functionOwner, List(func) ::: pageIdWatchers.getOrDefault(functionOwner, Nil))
}
}
}
LiftSession.onFunctionOwnersRemoved ::= { removedOwners =>
for {
owner <- removedOwners
watcher <- Option(pageIdWatchers.get(owner))} {
watcher.foreach(wf => tryo(wf()))
pageIdWatchers.removeCleanupFunc(owner)
}
}
def cleanupPage(): Unit = {
debug(s"Running cleanupPage, releasing allocated resources")
}
pageIdWatchers.addCleanupFunc(cleanupPage)