Skip to content

Commit b603df6

Browse files
authored
Deprecate close() and Closeable APIs (#538)
1 parent 071bcb7 commit b603df6

File tree

27 files changed

+13
-123
lines changed

27 files changed

+13
-123
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ All notable changes to this project will be documented in this file. Take a look
1212

1313
* Support for streaming ZIP packages over HTTP. This lets you open a remote EPUB, audiobook, or any other ZIP-based publication without needing to download it first.
1414

15+
### Deprecated
16+
17+
* The `close()` and `Closeable` APIs are now deprecated. Resources are automatically released upon `deinit`, which aligns better with Swift.
18+
1519

1620
## [3.0.0-beta.2]
1721

Sources/Adapters/GCDWebServer/ResourceResponse.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,4 @@ class ResourceResponse: ReadiumGCDWebServerFileResponse, Loggable {
112112

113113
return (try? lastReadData?.get()) ?? Data()
114114
}
115-
116-
override open func close() {
117-
resource.close()
118-
}
119115
}

Sources/LCP/Content Protection/LCPDecryptor.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ final class LCPDecryptor {
101101
self.encryption = encryption
102102
}
103103

104-
func close() {
105-
resource.close()
106-
}
107-
108104
let sourceURL: AbsoluteURL? = nil
109105

110106
func properties() async -> ReadResult<ResourceProperties> {

Sources/Navigator/Audiobook/PublicationMediaLoader.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ final class PublicationMediaLoader: NSObject, AVAssetResourceLoaderDelegate, Log
101101
req.task.cancel()
102102

103103
if reqs.isEmpty {
104-
if let (_, res) = resources.removeValue(forKey: href) {
105-
res.close()
106-
}
104+
resources.removeValue(forKey: href)
107105
requests.removeValue(forKey: href)
108106
} else {
109107
requests[href] = reqs

Sources/Shared/Publication/Publication.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,6 @@ public class Publication: Closeable, Loggable {
9797
?? container[href.anyURL.removingQuery().removingFragment()]
9898
}
9999

100-
/// Closes any opened resource associated with the `Publication`, including `services`.
101-
public func close() {
102-
container.close()
103-
104-
for service in services {
105-
service.close()
106-
}
107-
}
108-
109100
/// Finds the first `Publication.Service` implementing the given service type.
110101
///
111102
/// e.g. `findService(PositionsService.self)`

Sources/Shared/Publication/Services/Content/Iterators/HTMLResourceContentIterator.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public class HTMLResourceContentIterator: ContentIterator {
102102
.tryMap { try SwiftSoup.parse($0) }
103103
.tryMap { try parse(document: $0, locator: locator, beforeMaxLength: beforeMaxLength) }
104104
.asyncMap { await adjustProgressions(of: $0) }
105-
resource.close()
106105
return try result.get()
107106
}
108107

Sources/Shared/Toolkit/Archive/Archive.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public protocol Archive {
2929
var entries: [ArchiveEntry] { get }
3030
func entry(at path: ArchivePath) -> ArchiveEntry?
3131
func readEntry(at path: ArchivePath) -> ArchiveEntryReader?
32-
func close()
3332
}
3433

3534
@available(*, unavailable)
@@ -56,9 +55,6 @@ public protocol ArchiveEntryReader {
5655
/// When `range` is nil, the whole content is returned. Out-of-range indexes are clamped to the available length
5756
/// automatically.
5857
func read(range: Range<UInt64>?) -> ArchiveResult<Data>
59-
60-
/// Closes any pending resources for this entry.
61-
func close()
6258
}
6359

6460
@available(*, unavailable, renamed: "ArchiveOpener")

Sources/Shared/Toolkit/Closeable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
public protocol Closeable {
1111
/// Closes this object and releases any resources associated with it.
1212
/// If the object is already closed then invoking this method has no effect.
13+
@available(*, deprecated, message: "Handle Resource deallocation with `deinit` instead.")
1314
func close()
1415
}
1516

@@ -20,6 +21,7 @@ public extension Closeable {
2021
public extension Closeable {
2122
/// Executes the given block function on this resource and then closes it down correctly whether
2223
/// an error is thrown or not.
24+
@available(*, deprecated, message: "The resource is automatically closed when deallocated")
2325
@inlinable func use<T>(_ block: (Self) throws -> T) rethrows -> T {
2426
// Can't use `defer` with async functions.
2527
do {

Sources/Shared/Toolkit/Data/Asset/Asset.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ public enum Asset: AssetProtocol {
3838
}
3939
}
4040
}
41-
42-
public func close() {
43-
switch self {
44-
case let .resource(asset):
45-
asset.close()
46-
case let .container(asset):
47-
asset.close()
48-
}
49-
}
5041
}
5142

5243
/// A single resource asset.
@@ -58,10 +49,6 @@ public struct ResourceAsset: AssetProtocol {
5849
self.resource = resource
5950
self.format = format
6051
}
61-
62-
public func close() {
63-
resource.close()
64-
}
6552
}
6653

6754
/// A container asset providing access to several resources.
@@ -73,8 +60,4 @@ public struct ContainerAsset: AssetProtocol {
7360
self.container = container
7461
self.format = format
7562
}
76-
77-
public func close() {
78-
container.close()
79-
}
8063
}

Sources/Shared/Toolkit/Data/Container/Container.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,4 @@ public class CompositeContainer: Container {
5959
container[url]
6060
}
6161
}
62-
63-
public func close() {
64-
for container in containers {
65-
container.close()
66-
}
67-
}
6862
}

0 commit comments

Comments
 (0)