1
1
/// <reference types="youtube" />
2
2
import { watch } from 'vue'
3
+ import type { UseScriptContext } from '@unhead/vue'
4
+ import type { MaybePromise } from '../utils'
3
5
import { useRegistryScript } from '../utils'
4
6
import { useHead } from '#imports'
5
7
import type { RegistryScriptInput } from '#nuxt-scripts'
6
8
7
9
export interface YouTubePlayerApi {
8
- YT : typeof YT & { ready : ( fn : ( ) => void ) => void }
10
+ YT : MaybePromise < {
11
+ Player : YT . Player
12
+ PlayerState : YT . PlayerState
13
+ get ( k : string ) : any
14
+ loaded : 0 | 1
15
+ loading : 0 | 1
16
+ ready ( f : ( ) => void ) : void
17
+ scan ( ) : void
18
+ setConfig ( config : YT . PlayerOptions ) : void
19
+ subscribe < EventName extends keyof YT . Events > (
20
+ event : EventName ,
21
+ listener : YT . Events [ EventName ] ,
22
+ context ?: any
23
+ ) : void
24
+ unsubscribe < EventName extends keyof YT . Events > (
25
+ event : EventName ,
26
+ listener : YT . Events [ EventName ] ,
27
+ context ?: any
28
+ ) : void
29
+ } >
9
30
}
10
31
11
32
declare global {
@@ -16,7 +37,7 @@ declare global {
16
37
17
38
export type YouTubePlayerInput = RegistryScriptInput
18
39
19
- export function useScriptYouTubePlayer < T extends YouTubePlayerApi > ( _options : YouTubePlayerInput ) {
40
+ export function useScriptYouTubePlayer < T extends YouTubePlayerApi > ( _options : YouTubePlayerInput ) : UseScriptContext < T > {
20
41
let readyPromise : Promise < void > = Promise . resolve ( )
21
42
const instance = useRegistryScript < T > ( 'youtubePlayer' , ( ) => ( {
22
43
scriptInput : {
@@ -26,7 +47,7 @@ export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options: You
26
47
scriptOptions : {
27
48
use ( ) {
28
49
return {
29
- YT : readyPromise . then ( ( ) => {
50
+ YT : window . YT || readyPromise . then ( ( ) => {
30
51
return window . YT
31
52
} ) ,
32
53
}
0 commit comments