Skip to content

Commit 95b5e01

Browse files
committed
Remove native clients
Closes codeoverflow-org#91 and removes the whole concept of native clients as disscussed there and on Discord. Previously simplifications are now added by extending the original client and adding these methods in a subclass. Other changes in this PR: - rcon: Remove `sendMessage(msg: string)`, use `send(msg: string)` instead. - twitch-chat: Rename `TwitchServiceClient` to `TwitchChatServiceClient` because of naming convention - twitch-pubsub: Rename `PubSubServiceClient` to `TwitchPubSubServiceClient` because of naming convention
1 parent fc20200 commit 95b5e01

File tree

67 files changed

+336
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+336
-551
lines changed

nodecg-io-ahk/extension/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { NodeCG } from "nodecg/types/server";
2-
import { Result, emptySuccess, success, ServiceBundle, ServiceClient } from "nodecg-io-core";
2+
import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";
33
import { AHK } from "./AHK";
44

55
interface AHKServiceConfig {
66
host: string;
77
port: number;
88
}
99

10-
export type AHKServiceClient = ServiceClient<AHK>;
10+
export type AHKServiceClient = AHK;
1111

1212
module.exports = (nodecg: NodeCG) => {
1313
new AhkService(nodecg, "ahk", __dirname, "../ahk-schema.json").register();
@@ -22,11 +22,7 @@ class AhkService extends ServiceBundle<AHKServiceConfig, AHKServiceClient> {
2222

2323
async createClient(config: AHKServiceConfig): Promise<Result<AHKServiceClient>> {
2424
const ahk = new AHK(this.nodecg, config.host, config.port);
25-
return success({
26-
getNativeClient() {
27-
return ahk;
28-
},
29-
});
25+
return success(ahk);
3026
}
3127

3228
stopClient() {

nodecg-io-android/extension/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { NodeCG } from "nodecg/types/server";
2-
import { Result, emptySuccess, success, ServiceBundle, ServiceClient } from "nodecg-io-core";
2+
import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";
33
import { Android } from "./android";
44

55
interface AndroidServiceConfig {
66
device: string;
77
}
88

9-
export type AndroidServiceClient = ServiceClient<Android>;
9+
export type AndroidServiceClient = Android;
1010

1111
module.exports = (nodecg: NodeCG) => {
1212
new AndroidService(nodecg, "android", __dirname, "../android-schema.json").register();
@@ -24,17 +24,12 @@ class AndroidService extends ServiceBundle<AndroidServiceConfig, AndroidServiceC
2424
const client = new Android(this.nodecg, config.device);
2525
await client.connect();
2626
this.nodecg.log.info("Successfully connected to adb.");
27-
return success({
28-
getNativeClient() {
29-
return client;
30-
},
31-
});
27+
return success(client);
3228
}
3329

3430
async stopClient(client: AndroidServiceClient): Promise<void> {
3531
try {
36-
const rawClient = client.getNativeClient();
37-
await rawClient.disconnect();
32+
await client.disconnect();
3833
} catch (err) {
3934
this.nodecg.log.error(err);
4035
// Do nothing. If we did not catch it it'd cause an infinite loop.

nodecg-io-core/extension/bundleManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NodeCG } from "nodecg/types/server";
2-
import { ObjectMap, Service, ServiceClient, ServiceDependency, ServiceInstance } from "./types";
2+
import { ObjectMap, Service, ServiceDependency, ServiceInstance } from "./types";
33
import { emptySuccess, error, Result } from "./utils/result";
44
import { EventEmitter } from "events";
55

@@ -27,7 +27,7 @@ export class BundleManager extends EventEmitter {
2727
* @param service the service that the bundle depends upon.
2828
* @param clientUpdate the callback that should be called if a client becomes available or gets updated.
2929
*/
30-
registerServiceDependency<C extends ServiceClient<unknown>>(
30+
registerServiceDependency<C>(
3131
bundleName: string,
3232
service: Service<unknown, C>,
3333
clientUpdate: (client?: C) => void,

nodecg-io-core/extension/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ServiceManager } from "./serviceManager";
33
import { BundleManager } from "./bundleManager";
44
import { MessageManager } from "./messageManager";
55
import { InstanceManager } from "./instanceManager";
6-
import { Service, ServiceClient } from "./types";
6+
import { Service } from "./types";
77
import { PersistenceManager } from "./persistenceManager";
88
import { ServiceClientWrapper } from "./serviceClientWrapper";
99

@@ -12,11 +12,8 @@ import { ServiceClientWrapper } from "./serviceClientWrapper";
1212
* Contains references to all internal modules.
1313
*/
1414
export interface NodeCGIOCore {
15-
registerService<R, C extends ServiceClient<unknown>>(service: Service<R, C>): void;
16-
requireService<C extends ServiceClient<unknown>>(
17-
nodecg: NodeCG,
18-
serviceType: string,
19-
): ServiceClientWrapper<C> | undefined;
15+
registerService<R, C>(service: Service<R, C>): void;
16+
requireService<C>(nodecg: NodeCG, serviceType: string): ServiceClientWrapper<C> | undefined;
2017
}
2118

2219
module.exports = (nodecg: NodeCG): NodeCGIOCore => {
@@ -40,13 +37,10 @@ module.exports = (nodecg: NodeCG): NodeCGIOCore => {
4037
// We use a extra object instead of returning a object containing all the managers and so on, because
4138
// any loaded bundle would be able to call any (public or private) of the managers which is not intended.
4239
return {
43-
registerService<R, C extends ServiceClient<unknown>>(service: Service<R, C>): void {
40+
registerService<R, C>(service: Service<R, C>): void {
4441
serviceManager.registerService(service);
4542
},
46-
requireService<C extends ServiceClient<unknown>>(
47-
nodecg: NodeCG,
48-
serviceType: string,
49-
): ServiceClientWrapper<C> | undefined {
43+
requireService<C>(nodecg: NodeCG, serviceType: string): ServiceClientWrapper<C> | undefined {
5044
const bundleName = nodecg.bundleName;
5145
const svc = serviceManager.getService(serviceType);
5246

nodecg-io-core/extension/instanceManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NodeCG } from "nodecg/types/server";
2-
import { ObjectMap, Service, ServiceClient, ServiceInstance } from "./types";
2+
import { ObjectMap, Service, ServiceInstance } from "./types";
33
import { emptySuccess, error, Result } from "./utils/result";
44
import { ServiceManager } from "./serviceManager";
55
import { BundleManager } from "./bundleManager";
@@ -179,7 +179,7 @@ export class InstanceManager extends EventEmitter {
179179
* @param instanceName the name of the service instance, used for letting all bundles know of the new client.
180180
* @param service the service of the service instance, needed to stop old client
181181
*/
182-
private async updateInstanceClient<R, C extends ServiceClient<unknown>>(
182+
private async updateInstanceClient<R, C>(
183183
inst: ServiceInstance<R, C>,
184184
instanceName: string,
185185
service: Service<R, C>,

nodecg-io-core/extension/serviceBundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NodeCGIOCore } from ".";
22
import { NodeCG } from "nodecg/types/server";
3-
import { Service, ServiceClient } from "./types";
3+
import { Service } from "./types";
44
import { Result } from "./utils/result";
55

66
import * as fs from "fs";
@@ -14,7 +14,7 @@ import * as path from "path";
1414
* Intended to hold configurations and authentication information that the service needs to provide a client.
1515
* @typeParam C the type of a client that the service will provide to bundles using {@link createClient}.
1616
*/
17-
export abstract class ServiceBundle<R, C extends ServiceClient<unknown>> implements Service<R, C> {
17+
export abstract class ServiceBundle<R, C> implements Service<R, C> {
1818
public core: NodeCGIOCore | undefined;
1919
public nodecg: NodeCG;
2020
public serviceType: string;

nodecg-io-core/extension/serviceClientWrapper.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { EventEmitter } from "events";
22
import { NodeCGIOCore } from "./index";
33
import { NodeCG } from "nodecg/types/server";
4-
import { ServiceClient } from "./types";
54

65
/**
76
* A wrapper around a ServiceClient that has helper functions for setting up callbacks and
87
* a function which just returns the current ServiceClient.
98
* This class gets its client updates through the framework, namely using the {@link ServiceDependency.clientUpdateCallback}.
109
*/
11-
export class ServiceClientWrapper<C extends ServiceClient<unknown>> extends EventEmitter {
10+
export class ServiceClientWrapper<C> extends EventEmitter {
1211
private currentClient: C | undefined;
1312

1413
constructor() {
@@ -62,10 +61,7 @@ export class ServiceClientWrapper<C extends ServiceClient<unknown>> extends Even
6261
* @return {ServiceClientWrapper<C> | undefined} a service client wrapper for access to the service client
6362
* or undefined if the core wasn't loaded or the service type doesn't exist.
6463
*/
65-
export function requireService<C extends ServiceClient<unknown>>(
66-
nodecg: NodeCG,
67-
serviceType: string,
68-
): ServiceClientWrapper<C> | undefined {
64+
export function requireService<C>(nodecg: NodeCG, serviceType: string): ServiceClientWrapper<C> | undefined {
6965
const core = (nodecg.extensions["nodecg-io-core"] as unknown) as NodeCGIOCore | undefined;
7066
if (core === undefined) {
7167
nodecg.log.error(

nodecg-io-core/extension/serviceManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Service, ServiceClient } from "./types";
1+
import { Service } from "./types";
22
import { NodeCG } from "nodecg/types/server";
33
import { error, Result, success } from "./utils/result";
44

@@ -16,7 +16,7 @@ export class ServiceManager {
1616
* Registers the passed service which show it in the GUI and allows it to be instanced using {@link createServiceInstance}.
1717
* @param service the service you want to register.
1818
*/
19-
registerService<R, C extends ServiceClient<unknown>>(service: Service<R, C>): void {
19+
registerService<R, C>(service: Service<R, C>): void {
2020
this.services.push(service);
2121
this.nodecg.log.info(`Service ${service.serviceType} has been registered.`);
2222
}

nodecg-io-core/extension/types.d.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,3 @@ export interface ServiceDependency<C> {
129129
*/
130130
readonly clientUpdateCallback(client?: C): void;
131131
}
132-
133-
/**
134-
* A common interface between all service clients.
135-
* Currently this only ensures that all services allow access to the underlying client
136-
* by providing a getNativeClient() function.
137-
* @typeParam T the type of the underlying client.
138-
*/
139-
export interface ServiceClient<T> {
140-
getNativeClient(): T;
141-
}

nodecg-io-core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This can be used by services and bundles to import everything by just using "nodecg-io-core" and they don't need to know paths
33
// or where the thing they want to import is located.
44
// You can obviously still provide the full path if you wish.
5-
export type { ObjectMap, Service, ServiceClient, ServiceDependency, ServiceInstance } from "./extension/types";
5+
export type { ObjectMap, Service, ServiceDependency, ServiceInstance } from "./extension/types";
66
export * from "./extension/utils/result";
77
export { ServiceBundle } from "./extension/serviceBundle";
88
export { requireService } from "./extension/serviceClientWrapper";

0 commit comments

Comments
 (0)