11/// <reference no-default-lib="true" />
22/// <reference lib="esnext" />
33/// <reference lib="dom" />
4+
45import type {
56 Manager ,
67 ManagerOptions ,
@@ -9,22 +10,7 @@ import type {
910} from "./types/socketIO/index.ts" ;
1011export type { Manager , ManagerOptions , Socket , SocketOptions } ;
1112
12- type IO = (
13- uri : string ,
14- opts ?: Partial < ManagerOptions & SocketOptions > ,
15- ) => Socket ;
16-
17- declare global {
18- interface Window {
19- io ?: IO ;
20- }
21- }
22- const version = "4.2.0" ;
23- const url =
24- `https://cdnjs.cloudflare.com/ajax/libs/socket.io/${ version } /socket.io.min.js` ;
25- let error : string | Event | undefined ;
26-
27- export async function socketIO ( ) : Promise < Socket > {
13+ export const socketIO = async ( ) : Promise < Socket > => {
2814 const io = await importSocketIO ( ) ;
2915 const socket = io ( "https://scrapbox.io" , {
3016 reconnectionDelay : 5000 ,
@@ -40,9 +26,20 @@ export async function socketIO(): Promise<Socket> {
4026 socket . once ( "disconnect" , onDisconnect ) ;
4127 } ) ;
4228 return socket ;
43- }
29+ } ;
30+
31+ type IO = (
32+ uri : string ,
33+ opts ?: Partial < ManagerOptions & SocketOptions > ,
34+ ) => Socket ;
35+ declare const io : IO | undefined ;
36+ const version = "4.2.0" ;
37+ const url =
38+ `https://cdnjs.cloudflare.com/ajax/libs/socket.io/${ version } /socket.io.min.js` ;
39+ let error : string | Event | undefined ;
4440
45- async function importSocketIO ( ) : Promise < IO > {
41+ const importSocketIO = async ( ) : Promise < IO > => {
42+ if ( ! error ) throw error ;
4643 if ( ! document . querySelector ( `script[src="${ url } "]` ) ) {
4744 const script = document . createElement ( "script" ) ;
4845 script . src = url ;
@@ -56,11 +53,11 @@ async function importSocketIO(): Promise<IO> {
5653 } ) ;
5754 }
5855
59- return new Promise ( ( resolve , reject ) => {
56+ return new Promise ( ( resolve ) => {
6057 const id = setInterval ( ( ) => {
61- if ( ! window . io ) return ;
58+ if ( ! io ) return ;
6259 clearInterval ( id ) ;
63- resolve ( window . io ) ;
60+ resolve ( io ) ;
6461 } , 500 ) ;
6562 } ) ;
66- }
63+ } ;
0 commit comments