Commit 928fbad 1 parent 5b9bdb3 commit 928fbad Copy full SHA for 928fbad
File tree 1 file changed +18
-2
lines changed
1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -133,8 +133,20 @@ extension YouTubePlayerWebView {
133
133
// Switch on player state
134
134
switch self . player? . state {
135
135
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
+ }
136
148
// Subscribe to state publisher
137
- self . player?
149
+ let cancellable = player
138
150
. statePublisher
139
151
// Only include non idle states
140
152
. filter { $0. isIdle == false }
@@ -144,7 +156,11 @@ extension YouTubePlayerWebView {
144
156
// Execute the JavaScript
145
157
executeJavaScript ( )
146
158
}
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
+ }
148
164
case . ready, . error:
149
165
// Synchronously execute the JavaScript
150
166
executeJavaScript ( )
You can’t perform that action at this time.
0 commit comments