@@ -9,6 +9,7 @@ import * as tcp from './utils/tcp';
99import * as services from './services' ;
1010import * as fs from 'fs' ;
1111import Database = require( 'better-sqlite3' ) ;
12+ import { DiscoveryMessage , ServicePorts } from './types' ;
1213
1314interface ConnectionInfo extends DiscoveryMessage {
1415 address : string ;
@@ -62,13 +63,6 @@ async function discover(): Promise<ConnectionInfo> {
6263 } ) ;
6364}
6465
65- // FIXME: Pretty sure this can be improved upon
66- interface Services {
67- StateMap : services . StateMap ;
68- FileTransfer : services . FileTransfer ;
69- }
70- type SupportedTypes = services . StateMap | services . FileTransfer ;
71-
7266interface SourceAndTrackPath {
7367 source : string ;
7468 trackPath : string ;
@@ -81,10 +75,7 @@ export class Controller {
8175 private port : number = 0 ;
8276 private serviceRequestAllowed = false ;
8377 private servicePorts : ServicePorts = { } ;
84- private services : Services = {
85- StateMap : null ,
86- FileTransfer : null ,
87- } ;
78+ private services : Record < string , InstanceType < typeof services . Service > > = { } ;
8879 private timeAlive : number = 0 ;
8980 private connectedSources : {
9081 [ key : string ] : {
@@ -167,24 +158,24 @@ export class Controller {
167158 }
168159
169160 // Factory function
170- async connectToService < T extends SupportedTypes > ( c : {
161+ async connectToService < T extends InstanceType < typeof services . Service > > ( ctor : {
171162 new ( p_address : string , p_port : number , p_controller : Controller ) : T ;
172163 } ) : Promise < T > {
173164 assert ( this . connection ) ;
174165 // FIXME: find out why we need these waits before connecting to a service
175166 await sleep ( 500 ) ;
176167
177- const serviceName = c . name ;
168+ const serviceName = ctor . name ;
178169
179170 if ( this . services [ serviceName ] ) {
180- return this . services [ serviceName ] ;
171+ return this . services [ serviceName ] as T ;
181172 }
182173
183174 assert ( this . servicePorts . hasOwnProperty ( serviceName ) ) ;
184175 assert ( this . servicePorts [ serviceName ] > 0 ) ;
185176 const port = this . servicePorts [ serviceName ] ;
186177
187- const service = new c ( this . address , port , this ) ;
178+ const service = new ctor ( this . address , port , this ) ;
188179
189180 await service . connect ( ) ;
190181 this . services [ serviceName ] = service ;
@@ -200,7 +191,7 @@ export class Controller {
200191 // Get all album art extensions
201192 const stmt = db . prepare ( 'SELECT * FROM AlbumArt WHERE albumArt NOT NULL' ) ;
202193 const result = stmt . all ( ) ;
203- const albumArtExtensions = { } ;
194+ const albumArtExtensions : Record < string , string | null > = { } ;
204195 for ( const entry of result ) {
205196 const filetype = await FileType . fromBuffer ( entry . albumArt ) ;
206197 albumArtExtensions [ entry . id ] = filetype ? filetype . ext : null ;
0 commit comments