Skip to content

Commit c4eab48

Browse files
authored
Remove SuspendingCloseable (#532)
1 parent 636e1fd commit c4eab48

File tree

28 files changed

+92
-78
lines changed

28 files changed

+92
-78
lines changed

readium/adapters/pdfium/document/src/main/java/org/readium/adapter/pdfium/document/PdfiumDocument.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class PdfiumDocument(
7272
core.getTableOfContents(document).map { it.toOutlineNode() }
7373
}
7474

75-
override suspend fun close() {
75+
override fun close() {
7676
tryOrLog {
7777
core.closeDocument(document)
7878
}

readium/adapters/pspdfkit/document/src/main/java/org/readium/adapter/pspdfkit/document/PsPdfKitDocument.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public class PsPdfKitDocument(
115115
document.outline.toOutlineNodes()
116116
}
117117

118-
override suspend fun close() {}
118+
override fun close() {}
119119
}
120120

121121
private fun List<OutlineElement>.toOutlineNodes(): List<PdfDocument.OutlineNode> =

readium/shared/src/main/java/org/readium/r2/shared/publication/Publication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public class Publication(
199199
/**
200200
* Closes any opened resource associated with the [Publication], including services.
201201
*/
202-
override suspend fun close() {
202+
override fun close() {
203203
container.close()
204204
services.close()
205205
}

readium/shared/src/main/java/org/readium/r2/shared/publication/PublicationServicesHolder.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ package org.readium.r2.shared.publication
1111
import kotlin.reflect.KClass
1212
import org.readium.r2.shared.InternalReadiumApi
1313
import org.readium.r2.shared.extensions.tryOrLog
14-
import org.readium.r2.shared.util.SuspendingCloseable
14+
import org.readium.r2.shared.util.Closeable
1515

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

40-
override suspend fun close() {
40+
override fun close() {
4141
for (service in services) {
4242
tryOrLog { service.close() }
4343
}

readium/shared/src/main/java/org/readium/r2/shared/publication/services/search/SearchService.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import org.readium.r2.shared.ExperimentalReadiumApi
1212
import org.readium.r2.shared.publication.LocatorCollection
1313
import org.readium.r2.shared.publication.Publication
1414
import org.readium.r2.shared.publication.ServiceFactory
15+
import org.readium.r2.shared.util.Closeable
1516
import org.readium.r2.shared.util.Error
16-
import org.readium.r2.shared.util.SuspendingCloseable
1717
import org.readium.r2.shared.util.Try
1818
import org.readium.r2.shared.util.data.ReadError
1919

@@ -134,7 +134,7 @@ public var Publication.ServicesBuilder.searchServiceFactory: ServiceFactory?
134134
* Iterates through search results.
135135
*/
136136
@ExperimentalReadiumApi
137-
public interface SearchIterator : SuspendingCloseable {
137+
public interface SearchIterator : Closeable {
138138

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

162162
/**
163163
* Performs the given operation on each result page of this [SearchIterator].

readium/shared/src/main/java/org/readium/r2/shared/util/Closeable.kt

+1-13
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,11 @@ public interface Closeable {
1717
public fun close()
1818
}
1919

20-
/**
21-
* A [SuspendingCloseable] is an object holding closeable resources, such as open files or streams.
22-
*/
23-
public interface SuspendingCloseable {
24-
25-
/**
26-
* Closes this object and releases any resources associated with it.
27-
* If the object is already closed then invoking this method has no effect.
28-
*/
29-
public suspend fun close()
30-
}
31-
3220
/**
3321
* Executes the given block function on this resource and then closes it down correctly whether
3422
* an exception is thrown or not.
3523
*/
36-
public suspend inline fun <T : SuspendingCloseable?, R> T.use(block: (T) -> R): R {
24+
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
3725
var exception: Throwable? = null
3826
try {
3927
return block(this)

readium/shared/src/main/java/org/readium/r2/shared/util/asset/Asset.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

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

9-
import org.readium.r2.shared.util.SuspendingCloseable
9+
import org.readium.r2.shared.util.Closeable
1010
import org.readium.r2.shared.util.data.Container
1111
import org.readium.r2.shared.util.format.Format
1212
import org.readium.r2.shared.util.resource.Resource
1313

1414
/**
1515
* An asset which is either a single resource or a container that holds multiple resources.
1616
*/
17-
public sealed class Asset : SuspendingCloseable {
17+
public sealed class Asset : Closeable {
1818

1919
/**
2020
* Format of the asset.
@@ -33,7 +33,7 @@ public class ContainerAsset(
3333
public val container: Container<Resource>
3434
) : Asset() {
3535

36-
override suspend fun close() {
36+
override fun close() {
3737
container.close()
3838
}
3939
}
@@ -49,7 +49,7 @@ public class ResourceAsset(
4949
public val resource: Resource
5050
) : Asset() {
5151

52-
override suspend fun close() {
52+
override fun close() {
5353
resource.close()
5454
}
5555
}

readium/shared/src/main/java/org/readium/r2/shared/util/cache/Cache.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import kotlinx.coroutines.sync.withLock
1616
import org.readium.r2.shared.InternalReadiumApi
1717
import org.readium.r2.shared.util.Closeable
1818
import org.readium.r2.shared.util.MemoryObserver
19-
import org.readium.r2.shared.util.SuspendingCloseable
2019
import org.readium.r2.shared.util.Try
2120

2221
/**
@@ -25,7 +24,7 @@ import org.readium.r2.shared.util.Try
2524
* It implements [MemoryObserver] to flush unused in-memory objects when necessary.
2625
*/
2726
@InternalReadiumApi
28-
public interface Cache<V> : SuspendingCloseable, MemoryObserver {
27+
public interface Cache<V> : Closeable, MemoryObserver {
2928
/**
3029
* Performs an atomic [block] transaction on this cache.
3130
*/
@@ -111,11 +110,13 @@ public class InMemoryCache<V> : Cache<V> {
111110
}
112111
}
113112

114-
override suspend fun close() {
115-
transaction {
116-
for ((_, value) in values) {
117-
(value as? Closeable)?.close()
118-
(value as? SuspendingCloseable)?.close()
113+
@OptIn(DelicateCoroutinesApi::class)
114+
override fun close() {
115+
GlobalScope.launch {
116+
transaction {
117+
for ((_, value) in values) {
118+
(value as? Closeable)?.close()
119+
}
119120
}
120121
}
121122
}

readium/shared/src/main/java/org/readium/r2/shared/util/content/ContentResource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ContentResource(
4747

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

50-
override suspend fun close() {
50+
override fun close() {
5151
}
5252

5353
override suspend fun properties(): Try<Resource.Properties, ReadError> {

readium/shared/src/main/java/org/readium/r2/shared/util/data/Caching.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal class CachingReadable(
5858
}
5959
}
6060

61-
override suspend fun close() {}
61+
override fun close() {}
6262
}
6363

6464
internal class CachingContainer(
@@ -81,7 +81,7 @@ internal class CachingContainer(
8181
return blobContext
8282
}
8383

84-
override suspend fun close() {
84+
override fun close() {
8585
cache.forEach { it.value.close() }
8686
cache.clear()
8787
}

readium/shared/src/main/java/org/readium/r2/shared/util/data/Container.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

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

9+
import kotlin.io.use
910
import org.readium.r2.shared.InternalReadiumApi
1011
import org.readium.r2.shared.util.AbsoluteUrl
11-
import org.readium.r2.shared.util.SuspendingCloseable
12+
import org.readium.r2.shared.util.Closeable
1213
import org.readium.r2.shared.util.Try
1314
import org.readium.r2.shared.util.Url
1415
import org.readium.r2.shared.util.use
1516

1617
/**
1718
* A container provides access to a list of [Readable] entries.
1819
*/
19-
public interface Container<out E : Readable> : Iterable<Url>, SuspendingCloseable {
20+
public interface Container<out E : Readable> : Iterable<Url>, Closeable {
2021

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

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

48-
override suspend fun close() {}
49+
override fun close() {}
4950
}
5051

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

72-
override suspend fun close() {
73+
override fun close() {
7374
containers.forEach { it.close() }
7475
}
7576
}

readium/shared/src/main/java/org/readium/r2/shared/util/data/Reading.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ package org.readium.r2.shared.util.data
88

99
import java.io.IOException
1010
import org.readium.r2.shared.InternalReadiumApi
11+
import org.readium.r2.shared.util.Closeable
1112
import org.readium.r2.shared.util.DebugError
1213
import org.readium.r2.shared.util.Error
1314
import org.readium.r2.shared.util.ErrorException
14-
import org.readium.r2.shared.util.SuspendingCloseable
1515
import org.readium.r2.shared.util.ThrowableError
1616
import org.readium.r2.shared.util.Try
1717
import org.readium.r2.shared.util.getOrElse
1818

1919
/**
2020
* Acts as a proxy to an actual data source by handling read access.
2121
*/
22-
public interface Readable : SuspendingCloseable {
22+
public interface Readable : Closeable {
2323

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

123-
override suspend fun close() {
123+
override fun close() {
124124
// Do nothing
125125
}
126126
}

readium/shared/src/main/java/org/readium/r2/shared/util/file/DirectoryContainer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class DirectoryContainer(
3030
?.let { File(root, it) }
3131
?.let { FileResource(it) }
3232

33-
override suspend fun close() {}
33+
override fun close() {}
3434

3535
public companion object {
3636

readium/shared/src/main/java/org/readium/r2/shared/util/file/FileResource.kt

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import java.io.FileNotFoundException
1313
import java.io.IOException
1414
import java.io.RandomAccessFile
1515
import java.nio.channels.Channels
16+
import kotlinx.coroutines.DelicateCoroutinesApi
1617
import kotlinx.coroutines.Dispatchers
18+
import kotlinx.coroutines.GlobalScope
19+
import kotlinx.coroutines.launch
1720
import kotlinx.coroutines.withContext
1821
import org.readium.r2.shared.InternalReadiumApi
1922
import org.readium.r2.shared.extensions.*
@@ -58,11 +61,14 @@ public class FileResource(
5861
return Try.success(properties)
5962
}
6063

61-
override suspend fun close() {
62-
withContext(Dispatchers.IO) {
63-
if (::randomAccessFile.isLazyInitialized) {
64-
randomAccessFile.onSuccess {
65-
tryOrLog { it.close() }
64+
@OptIn(DelicateCoroutinesApi::class)
65+
override fun close() {
66+
if (::randomAccessFile.isLazyInitialized) {
67+
GlobalScope.launch {
68+
withContext(Dispatchers.IO) {
69+
randomAccessFile.onSuccess {
70+
tryOrLog { it.close() }
71+
}
6672
}
6773
}
6874
}

readium/shared/src/main/java/org/readium/r2/shared/util/http/HttpContainer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ public class HttpContainer(
3939
}
4040
}
4141

42-
override suspend fun close() {}
42+
override fun close() {}
4343
}

readium/shared/src/main/java/org/readium/r2/shared/util/http/HttpResource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class HttpResource(
6161
}
6262
}
6363

64-
override suspend fun close() {}
64+
override fun close() {}
6565

6666
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> = withContext(
6767
Dispatchers.IO

readium/shared/src/main/java/org/readium/r2/shared/util/pdf/PdfDocument.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.readium.r2.shared.publication.Publication
2121
import org.readium.r2.shared.publication.PublicationServicesHolder
2222
import org.readium.r2.shared.publication.ReadingProgression
2323
import org.readium.r2.shared.publication.services.cacheService
24-
import org.readium.r2.shared.util.SuspendingCloseable
24+
import org.readium.r2.shared.util.Closeable
2525
import org.readium.r2.shared.util.Url
2626
import org.readium.r2.shared.util.cache.Cache
2727
import org.readium.r2.shared.util.cache.getOrTryPut
@@ -72,7 +72,7 @@ private class CachingPdfDocumentFactory<T : PdfDocument>(
7272
/**
7373
* Represents a PDF document.
7474
*/
75-
public interface PdfDocument : SuspendingCloseable {
75+
public interface PdfDocument : Closeable {
7676

7777
/**
7878
* Permanent identifier based on the contents of the file at the time it was originally

readium/shared/src/main/java/org/readium/r2/shared/util/resource/FallbackResource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class FallbackResource(
2929
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> =
3030
withResource { read(range) }
3131

32-
override suspend fun close() {
32+
override fun close() {
3333
if (::_resource.isInitialized) {
3434
_resource.close()
3535
}

readium/shared/src/main/java/org/readium/r2/shared/util/resource/InMemoryResource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class InMemoryResource(
6060
return _bytes.map { it.read(range) }
6161
}
6262

63-
override suspend fun close() {}
63+
override fun close() {}
6464

6565
override fun toString(): String =
6666
"${javaClass.simpleName}(${runBlocking { length() }} bytes)"

readium/shared/src/main/java/org/readium/r2/shared/util/resource/LazyResource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public open class LazyResource(
3737
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> =
3838
resource().read(range)
3939

40-
override suspend fun close() {
40+
override fun close() {
4141
if (::_resource.isInitialized) {
4242
_resource.close()
4343
}

readium/shared/src/main/java/org/readium/r2/shared/util/resource/Resource.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class FailureResource(
6262
override suspend fun properties(): Try<Resource.Properties, ReadError> = Try.failure(error)
6363
override suspend fun length(): Try<Long, ReadError> = Try.failure(error)
6464
override suspend fun read(range: LongRange?): Try<ByteArray, ReadError> = Try.failure(error)
65-
override suspend fun close() {}
65+
override fun close() {}
6666

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

84-
override suspend fun close() {
84+
override fun close() {
8585
// Do nothing
8686
}
8787
}

readium/shared/src/main/java/org/readium/r2/shared/util/resource/SingleResourceContainer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SingleResourceContainer(
2525
return resource.borrow()
2626
}
2727

28-
override suspend fun close() {
28+
override fun close() {
2929
resource.close()
3030
}
3131
}

0 commit comments

Comments
 (0)