Skip to content

Commit a9b396a

Browse files
authored
Merge pull request #13 from docBliny/more-api-like
Changes to make it easier to consume as library.
2 parents ed23105 + 64a02a9 commit a9b396a

File tree

12 files changed

+238
-269
lines changed

12 files changed

+238
-269
lines changed

Controller.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as tcp from './utils/tcp';
99
import * as services from './services';
1010
import * as fs from 'fs';
1111
import Database = require('better-sqlite3');
12+
import { DiscoveryMessage, ServicePorts } from './types';
1213

1314
interface 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-
7266
interface 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;

announce.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createSocket, Socket as UDPSocket } from 'dgram';
1111
import { subnet } from 'ip';
1212
import { networkInterfaces } from 'os';
1313
import { WriteContext } from './utils/WriteContext';
14+
import type { DiscoveryMessage } from './types';
1415

1516
function findBroadcastIPs(): string[] {
1617
const interfaces = Object.values(networkInterfaces());

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './announce';
2+
export * from './common';
3+
export * from './Controller';
4+
export * from './services';
5+
export * from './types';
6+
export * from './utils';

0 commit comments

Comments
 (0)