11/// <reference types="youtube" />
22import { watch } from 'vue'
3+ import type { UseScriptContext } from '@unhead/vue'
4+ import type { MaybePromise } from '../utils'
35import { useRegistryScript } from '../utils'
46import { useHead } from '#imports'
57import type { RegistryScriptInput } from '#nuxt-scripts'
68
79export 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+ } >
930}
1031
1132declare global {
@@ -16,7 +37,7 @@ declare global {
1637
1738export type YouTubePlayerInput = RegistryScriptInput
1839
19- export function useScriptYouTubePlayer < T extends YouTubePlayerApi > ( _options : YouTubePlayerInput ) {
40+ export function useScriptYouTubePlayer < T extends YouTubePlayerApi > ( _options : YouTubePlayerInput ) : UseScriptContext < T > {
2041 let readyPromise : Promise < void > = Promise . resolve ( )
2142 const instance = useRegistryScript < T > ( 'youtubePlayer' , ( ) => ( {
2243 scriptInput : {
@@ -26,7 +47,7 @@ export function useScriptYouTubePlayer<T extends YouTubePlayerApi>(_options: You
2647 scriptOptions : {
2748 use ( ) {
2849 return {
29- YT : readyPromise . then ( ( ) => {
50+ YT : window . YT || readyPromise . then ( ( ) => {
3051 return window . YT
3152 } ) ,
3253 }
0 commit comments