Skip to content

Commit 23c5ad0

Browse files
committed
Cancel tasks on deinit
1 parent 7d75ab0 commit 23c5ad0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
164164
public convenience init(_ url: URL, config: LiveSessionConfiguration = .init()) where R == EmptyRegistry {
165165
self.init(url, config: config, customRegistryType: EmptyRegistry.self)
166166
}
167+
168+
deinit {
169+
self.liveReloadListenerLoop?.cancel()
170+
}
167171

168172
/// Connects this coordinator to the LiveView channel.
169173
///
@@ -209,10 +213,14 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
209213
guard let url = await URL(string: style, relativeTo: self.url)
210214
else { continue }
211215
group.addTask {
212-
let (data, _) = try await URLSession.shared.data(from: url)
213-
guard let contents = String(data: data, encoding: .utf8)
214-
else { return await Stylesheet<R>(content: [], classes: [:]) }
215-
return try await Stylesheet<R>(from: contents, in: .init())
216+
if let cached = await StylesheetCache.shared.read(for: url, registry: R.self) {
217+
return cached
218+
} else {
219+
let (data, _) = try await URLSession.shared.data(from: url)
220+
guard let contents = String(data: data, encoding: .utf8)
221+
else { return await Stylesheet<R>(content: [], classes: [:]) }
222+
return try await Stylesheet<R>(from: contents, in: .init())
223+
}
216224
}
217225
}
218226

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
6565
self.session = session
6666
self.url = url
6767
}
68+
69+
deinit {
70+
self.eventListenerLoop?.cancel()
71+
self.statusListenerLoop?.cancel()
72+
}
6873

6974
/// Pushes a LiveView event with the given name and payload to the server.
7075
///

0 commit comments

Comments
 (0)