Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SuspendingCloseable #532

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class PdfiumDocument(
core.getTableOfContents(document).map { it.toOutlineNode() }
}

override suspend fun close() {
override fun close() {
tryOrLog {
core.closeDocument(document)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class PsPdfKitDocument(
document.outline.toOutlineNodes()
}

override suspend fun close() {}
override fun close() {}
}

private fun List<OutlineElement>.toOutlineNodes(): List<PdfDocument.OutlineNode> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public class Publication(
/**
* Closes any opened resource associated with the [Publication], including services.
*/
override suspend fun close() {
override fun close() {
container.close()
services.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ package org.readium.r2.shared.publication
import kotlin.reflect.KClass
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.extensions.tryOrLog
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Closeable

/**
* Holds [Publication.Service] instances for a [Publication].
*/
public interface PublicationServicesHolder : SuspendingCloseable {
public interface PublicationServicesHolder : Closeable {
/**
* Returns the first publication service that is an instance of [serviceType].
*/
Expand All @@ -37,7 +37,7 @@ internal class ListPublicationServicesHolder(
override fun <T : Publication.Service> findServices(serviceType: KClass<T>): List<T> =
services.filterIsInstance(serviceType.java)

override suspend fun close() {
override fun close() {
for (service in services) {
tryOrLog { service.close() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.readium.r2.shared.ExperimentalReadiumApi
import org.readium.r2.shared.publication.LocatorCollection
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.ServiceFactory
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.Error
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.data.ReadError

Expand Down Expand Up @@ -134,7 +134,7 @@ public var Publication.ServicesBuilder.searchServiceFactory: ServiceFactory?
* Iterates through search results.
*/
@ExperimentalReadiumApi
public interface SearchIterator : SuspendingCloseable {
public interface SearchIterator : Closeable {

/**
* Number of matches for this search, if known.
Expand All @@ -157,7 +157,7 @@ public interface SearchIterator : SuspendingCloseable {
* Closes any resources allocated for the search query, such as a cursor.
* To be called when the user dismisses the search.
*/
override suspend fun close() {}
override fun close() {}

/**
* Performs the given operation on each result page of this [SearchIterator].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,11 @@ public interface Closeable {
public fun close()
}

/**
* A [SuspendingCloseable] is an object holding closeable resources, such as open files or streams.
*/
public interface SuspendingCloseable {

/**
* Closes this object and releases any resources associated with it.
* If the object is already closed then invoking this method has no effect.
*/
public suspend fun close()
}

/**
* Executes the given block function on this resource and then closes it down correctly whether
* an exception is thrown or not.
*/
public suspend inline fun <T : SuspendingCloseable?, R> T.use(block: (T) -> R): R {
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
var exception: Throwable? = null
try {
return block(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

package org.readium.r2.shared.util.asset

import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.data.Container
import org.readium.r2.shared.util.format.Format
import org.readium.r2.shared.util.resource.Resource

/**
* An asset which is either a single resource or a container that holds multiple resources.
*/
public sealed class Asset : SuspendingCloseable {
public sealed class Asset : Closeable {

/**
* Format of the asset.
Expand All @@ -33,7 +33,7 @@ public class ContainerAsset(
public val container: Container<Resource>
) : Asset() {

override suspend fun close() {
override fun close() {
container.close()
}
}
Expand All @@ -49,7 +49,7 @@ public class ResourceAsset(
public val resource: Resource
) : Asset() {

override suspend fun close() {
override fun close() {
resource.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import kotlinx.coroutines.sync.withLock
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.MemoryObserver
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Try

/**
Expand All @@ -25,7 +24,7 @@ import org.readium.r2.shared.util.Try
* It implements [MemoryObserver] to flush unused in-memory objects when necessary.
*/
@InternalReadiumApi
public interface Cache<V> : SuspendingCloseable, MemoryObserver {
public interface Cache<V> : Closeable, MemoryObserver {
/**
* Performs an atomic [block] transaction on this cache.
*/
Expand Down Expand Up @@ -111,11 +110,13 @@ public class InMemoryCache<V> : Cache<V> {
}
}

override suspend fun close() {
transaction {
for ((_, value) in values) {
(value as? Closeable)?.close()
(value as? SuspendingCloseable)?.close()
@OptIn(DelicateCoroutinesApi::class)
override fun close() {
GlobalScope.launch {
transaction {
for ((_, value) in values) {
(value as? Closeable)?.close()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ContentResource(

override val sourceUrl: AbsoluteUrl? = uri.toUrl() as? AbsoluteUrl

override suspend fun close() {
override fun close() {
}

override suspend fun properties(): Try<Resource.Properties, ReadError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class CachingReadable(
}
}

override suspend fun close() {}
override fun close() {}
}

internal class CachingContainer(
Expand All @@ -81,7 +81,7 @@ internal class CachingContainer(
return blobContext
}

override suspend fun close() {
override fun close() {
cache.forEach { it.value.close() }
cache.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

package org.readium.r2.shared.util.data

import kotlin.io.use
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.util.AbsoluteUrl
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.use

/**
* A container provides access to a list of [Readable] entries.
*/
public interface Container<out E : Readable> : Iterable<Url>, SuspendingCloseable {
public interface Container<out E : Readable> : Iterable<Url>, Closeable {

/**
* Direct source to this container, when available.
Expand Down Expand Up @@ -45,7 +46,7 @@ public class EmptyContainer<E : Readable> :

override fun get(url: Url): E? = null

override suspend fun close() {}
override fun close() {}
}

/**
Expand All @@ -69,7 +70,7 @@ public class CompositeContainer<E : Readable>(
override fun get(url: Url): E? =
containers.firstNotNullOfOrNull { it[url] }

override suspend fun close() {
override fun close() {
containers.forEach { it.close() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ package org.readium.r2.shared.util.data

import java.io.IOException
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.DebugError
import org.readium.r2.shared.util.Error
import org.readium.r2.shared.util.ErrorException
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.ThrowableError
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.getOrElse

/**
* Acts as a proxy to an actual data source by handling read access.
*/
public interface Readable : SuspendingCloseable {
public interface Readable : Closeable {

/**
* Returns data length from metadata if available, or calculated from reading the bytes otherwise.
Expand Down Expand Up @@ -120,7 +120,7 @@ private class BorrowedReadable(
private val readable: Readable
) : Readable by readable {

override suspend fun close() {
override fun close() {
// Do nothing
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DirectoryContainer(
?.let { File(root, it) }
?.let { FileResource(it) }

override suspend fun close() {}
override fun close() {}

public companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import java.io.FileNotFoundException
import java.io.IOException
import java.io.RandomAccessFile
import java.nio.channels.Channels
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.readium.r2.shared.InternalReadiumApi
import org.readium.r2.shared.extensions.*
Expand Down Expand Up @@ -58,11 +61,14 @@ public class FileResource(
return Try.success(properties)
}

override suspend fun close() {
withContext(Dispatchers.IO) {
if (::randomAccessFile.isLazyInitialized) {
randomAccessFile.onSuccess {
tryOrLog { it.close() }
@OptIn(DelicateCoroutinesApi::class)
override fun close() {
if (::randomAccessFile.isLazyInitialized) {
GlobalScope.launch {
withContext(Dispatchers.IO) {
randomAccessFile.onSuccess {
tryOrLog { it.close() }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public class HttpContainer(
}
}

override suspend fun close() {}
override fun close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class HttpResource(
}
}

override suspend fun close() {}
override fun close() {}

override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> = withContext(
Dispatchers.IO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.PublicationServicesHolder
import org.readium.r2.shared.publication.ReadingProgression
import org.readium.r2.shared.publication.services.cacheService
import org.readium.r2.shared.util.SuspendingCloseable
import org.readium.r2.shared.util.Closeable
import org.readium.r2.shared.util.Url
import org.readium.r2.shared.util.cache.Cache
import org.readium.r2.shared.util.cache.getOrTryPut
Expand Down Expand Up @@ -72,7 +72,7 @@ private class CachingPdfDocumentFactory<T : PdfDocument>(
/**
* Represents a PDF document.
*/
public interface PdfDocument : SuspendingCloseable {
public interface PdfDocument : Closeable {

/**
* Permanent identifier based on the contents of the file at the time it was originally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FallbackResource(
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> =
withResource { read(range) }

override suspend fun close() {
override fun close() {
if (::_resource.isInitialized) {
_resource.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class InMemoryResource(
return _bytes.map { it.read(range) }
}

override suspend fun close() {}
override fun close() {}

override fun toString(): String =
"${javaClass.simpleName}(${runBlocking { length() }} bytes)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public open class LazyResource(
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> =
resource().read(range)

override suspend fun close() {
override fun close() {
if (::_resource.isInitialized) {
_resource.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class FailureResource(
override suspend fun properties(): Try<Resource.Properties, ReadError> = Try.failure(error)
override suspend fun length(): Try<Long, ReadError> = Try.failure(error)
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> = Try.failure(error)
override suspend fun close() {}
override fun close() {}

override fun toString(): String =
"${javaClass.simpleName}($error)"
Expand All @@ -81,7 +81,7 @@ private class BorrowedResource(
private val resource: Resource
) : Resource by resource {

override suspend fun close() {
override fun close() {
// Do nothing
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SingleResourceContainer(
return resource.borrow()
}

override suspend fun close() {
override fun close() {
resource.close()
}
}
Loading
Loading