Skip to content

Commit c4eab48

Browse files
authored
Remove SuspendingCloseable (readium#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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 13 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 8 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)