Skip to content

Commit 5594084

Browse files
committed
Cancel tasks on deinit
1 parent cd42bbe commit 5594084

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
@@ -148,6 +148,10 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
148148
public convenience init(_ url: URL, config: LiveSessionConfiguration = .init()) where R == EmptyRegistry {
149149
self.init(url, config: config, customRegistryType: EmptyRegistry.self)
150150
}
151+
152+
deinit {
153+
self.liveReloadListenerLoop?.cancel()
154+
}
151155

152156
/// Connects this coordinator to the LiveView channel.
153157
///
@@ -202,10 +206,14 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
202206
guard let url = await URL(string: style, relativeTo: self.url)
203207
else { continue }
204208
group.addTask {
205-
let (data, _) = try await URLSession.shared.data(from: url)
206-
guard let contents = String(data: data, encoding: .utf8)
207-
else { return await Stylesheet<R>(content: [], classes: [:]) }
208-
return try await Stylesheet<R>(from: contents, in: .init())
209+
if let cached = await StylesheetCache.shared.read(for: url, registry: R.self) {
210+
return cached
211+
} else {
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+
}
209217
}
210218
}
211219

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)