Skip to content

Commit

Permalink
Ensure to retain cancellable on main queue
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenTiigi committed Jan 9, 2024
1 parent 5b9bdb3 commit 928fbad
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Sources/WebView/YouTubePlayerWebView+Evaluate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,20 @@ extension YouTubePlayerWebView {
// Switch on player state
switch self.player?.state {
case nil, .idle:
// Verify a player is available
guard let player = self.player else {
// Otherwise return out of function and complete with failure
return completion(
.failure(
.init(
javaScript: javaScript.rawValue,
reason: "YouTubePlayer has been deallocated"
)
)
)
}
// Subscribe to state publisher
self.player?
let cancellable = player
.statePublisher
// Only include non idle states
.filter { $0.isIdle == false }
Expand All @@ -144,7 +156,11 @@ extension YouTubePlayerWebView {
// Execute the JavaScript
executeJavaScript()
}
.store(in: &self.cancellables)
// Dispatch on main queue
DispatchQueue.main.async { [weak self] in
// Retain cancellable
self?.cancellables.insert(cancellable)
}
case .ready, .error:
// Synchronously execute the JavaScript
executeJavaScript()
Expand Down

0 comments on commit 928fbad

Please sign in to comment.