11import { PropsWithChildren , useEffect , useRef } from 'react' ;
2- import { PornSocketContext , PornSocketService } from './porn-socket-service.tsx' ;
2+ import {
3+ PornSocketContext ,
4+ PornSocketService ,
5+ } from './porn-socket-service.tsx' ;
36import { useImages } from '../../settings' ;
47import { ImageServiceType , ImageType } from '../../types' ;
58
@@ -30,8 +33,7 @@ export interface UserResponse {
3033
3134export class WalltakerSocketService implements PornSocketService {
3235 private socket : WebSocket | null = null ;
33- onChange : ( link : LinkResponse ) => void = ( ) => {
34- } ;
36+ onChange : ( link : LinkResponse ) => void = ( ) => { } ;
3537
3638 connect ( ) : Promise < void > {
3739 return new Promise ( ( res , rej ) => {
@@ -62,7 +64,7 @@ export class WalltakerSocketService implements PornSocketService {
6264 }
6365
6466 disconnect ( ) : Promise < void > {
65- return new Promise ( ( res ) => {
67+ return new Promise ( res => {
6668 if ( this . socket ) {
6769 this . socket . addEventListener ( 'close' , ( ) => res ( ) ) ;
6870 this . socket . close ( ) ;
@@ -81,7 +83,10 @@ export class WalltakerSocketService implements PornSocketService {
8183 const channelId = this . channelIdentifierFor ( id ) ;
8284 const message = { command : 'subscribe' , identifier : channelId } ;
8385
84- this . socket . addEventListener ( 'message' , this . firstMessageHandlerFor ( 'confirm_subscription' , id , rej , res ) ) ;
86+ this . socket . addEventListener (
87+ 'message' ,
88+ this . firstMessageHandlerFor ( 'confirm_subscription' , id , rej , res )
89+ ) ;
8590 this . socket . send ( JSON . stringify ( message ) ) ;
8691 } ) ;
8792 }
@@ -102,13 +107,15 @@ export class WalltakerSocketService implements PornSocketService {
102107 }
103108
104109 static async getLink ( id : number ) {
105- return fetch ( `https://walltaker.joi.how/api/links/${ id } .json` )
106- . then ( res => res . json ( ) as unknown as LinkResponse ) ;
110+ return fetch ( `https://walltaker.joi.how/api/links/${ id } .json` ) . then (
111+ res => res . json ( ) as unknown as LinkResponse
112+ ) ;
107113 }
108114
109115 static async getLinksFromUsername ( username : string ) {
110- const result = await fetch ( `https://walltaker.joi.how/api/users/${ username } .json` )
111- . then ( res => res . json ( ) as unknown as UserResponse ) ;
116+ const result = await fetch (
117+ `https://walltaker.joi.how/api/users/${ username } .json`
118+ ) . then ( res => res . json ( ) as unknown as UserResponse ) ;
112119 return result . links ;
113120 }
114121
@@ -123,47 +130,66 @@ export class WalltakerSocketService implements PornSocketService {
123130 return JSON . stringify ( { channel : 'LinkChannel' , id } ) ;
124131 }
125132
126- private firstMessageHandlerFor ( eventName : string , id : number , rej : ( message ?: string ) => void , res : ( ) => void ) {
133+ private firstMessageHandlerFor (
134+ eventName : string ,
135+ id : number ,
136+ rej : ( message ?: string ) => void ,
137+ res : ( ) => void
138+ ) {
127139 const MAX_RETRIES = 4 ;
128140 let tries = 0 ;
129141
130142 const handler = ( message : MessageEvent < string > ) => {
131143 const content = JSON . parse ( message . data ) ;
132- if ( content . type === eventName && content . identifier === this . channelIdentifierFor ( id ) ) {
144+ if (
145+ content . type === eventName &&
146+ content . identifier === this . channelIdentifierFor ( id )
147+ ) {
133148 this . socket ?. removeEventListener ( 'message' , handler ) ;
134149 res ( ) ;
135150 return ;
136151 }
137152
138153 if ( ++ tries > MAX_RETRIES ) {
139154 this . socket ?. removeEventListener ( 'message' , handler ) ;
140- rej ( `Max retries hit when waiting for subscription confirmation for Link#${ id } .` ) ;
155+ rej (
156+ `Max retries hit when waiting for subscription confirmation for Link#${ id } .`
157+ ) ;
141158 }
142159 } ;
143160
144161 return handler ;
145162 }
146163}
147164
148- export function WalltakerSocketServiceProvider ( { children } : PropsWithChildren < object > ) {
165+ export function WalltakerSocketServiceProvider ( {
166+ children,
167+ } : PropsWithChildren < object > ) {
149168 const [ images , setImages ] = useImages ( ) ;
150169 const walltakerSocketService = useRef ( new WalltakerSocketService ( ) ) ;
151170
152171 useEffect ( ( ) => {
153- walltakerSocketService . current . onChange = ( link ) => {
172+ walltakerSocketService . current . onChange = link => {
154173 if ( images . find ( image => image . full === link . post_url ) ) return ;
155174
156- setImages ( [ ...images , {
157- thumbnail : link . post_thumbnail_url ,
158- preview : link . post_thumbnail_url ,
159- full : link . post_url ,
160- type : ImageType . image ,
161- source : link . post_url ,
162- service : ImageServiceType . e621 ,
163- id : link . post_url ,
164- } ] ) ;
175+ setImages ( [
176+ ...images ,
177+ {
178+ thumbnail : link . post_thumbnail_url ,
179+ preview : link . post_thumbnail_url ,
180+ full : link . post_url ,
181+ type : ImageType . image ,
182+ source : link . post_url ,
183+ service : ImageServiceType . e621 ,
184+ id : link . post_url ,
185+ } ,
186+ ] ) ;
165187 } ;
166188 } , [ images , setImages ] ) ;
167189
168- return < PornSocketContext . Provider value = { walltakerSocketService . current } > { children } </ PornSocketContext . Provider > ;
169- }
190+ return (
191+ < PornSocketContext . Provider value = { walltakerSocketService . current } >
192+ { children }
193+ </ PornSocketContext . Provider >
194+ ) ;
195+ }
0 commit comments