Skip to content

Commit 928fbad

Browse files
committed
Ensure to retain cancellable on main queue
#79
1 parent 5b9bdb3 commit 928fbad

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Sources/WebView/YouTubePlayerWebView+Evaluate.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,20 @@ extension YouTubePlayerWebView {
133133
// Switch on player state
134134
switch self.player?.state {
135135
case nil, .idle:
136+
// Verify a player is available
137+
guard let player = self.player else {
138+
// Otherwise return out of function and complete with failure
139+
return completion(
140+
.failure(
141+
.init(
142+
javaScript: javaScript.rawValue,
143+
reason: "YouTubePlayer has been deallocated"
144+
)
145+
)
146+
)
147+
}
136148
// Subscribe to state publisher
137-
self.player?
149+
let cancellable = player
138150
.statePublisher
139151
// Only include non idle states
140152
.filter { $0.isIdle == false }
@@ -144,7 +156,11 @@ extension YouTubePlayerWebView {
144156
// Execute the JavaScript
145157
executeJavaScript()
146158
}
147-
.store(in: &self.cancellables)
159+
// Dispatch on main queue
160+
DispatchQueue.main.async { [weak self] in
161+
// Retain cancellable
162+
self?.cancellables.insert(cancellable)
163+
}
148164
case .ready, .error:
149165
// Synchronously execute the JavaScript
150166
executeJavaScript()

0 commit comments

Comments
 (0)