Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit ea5f6f2

Browse files
committed
Smaller improvements to ServiceBundle
1 parent 3eadae1 commit ea5f6f2

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

nodecg-io-core/extension/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import { MessageManager } from "./messageManager";
55
import { InstanceManager } from "./instanceManager";
66
import { Service, ServiceProvider } from "./types";
77
import { PersistenceManager } from "./persistenceManager";
8-
import * as fs from "fs";
9-
import * as path from "path";
108

119
/**
1210
* Main type of NodeCG extension that the core bundle exposes.
1311
* Contains references to all internal modules.
1412
*/
1513
export interface NodeCGIOCore {
1614
registerService<R, C>(service: Service<R, C>): ServiceProvider<C>;
17-
readSchema(...path: string[]): Record<string, unknown> | undefined;
1815
}
1916

2017
module.exports = (nodecg: NodeCG): NodeCGIOCore => {
@@ -34,15 +31,5 @@ module.exports = (nodecg: NodeCG): NodeCGIOCore => {
3431
serviceManager.registerService(service);
3532
return bundleManager.createServiceProvider(service);
3633
},
37-
readSchema(...pathSegments: string[]) {
38-
const joinedPath = path.resolve(...pathSegments);
39-
try {
40-
const fileContent = fs.readFileSync(joinedPath, "utf8");
41-
return JSON.parse(fileContent);
42-
} catch (err) {
43-
nodecg.log.error("Couldn't read and parse service schema at " + joinedPath.toString());
44-
return undefined;
45-
}
46-
},
4734
};
4835
};

nodecg-io-core/extension/serviceBundle.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
1818
public core: NodeCGIOCore | undefined;
1919
public nodecg: NodeCG;
2020
public serviceType: string;
21-
public schema: any;
21+
public schema: unknown;
22+
23+
/**
24+
* The default value for the config.
25+
*/
26+
public defaultConfig?: R;
2227

2328
/**
2429
* This constructor creates the service and gets the nodecg-io-core
@@ -31,7 +36,7 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
3136
this.serviceType = serviceName;
3237
this.schema = this.readSchema(pathSegments);
3338

34-
this.nodecg.log.info(this.serviceType + " bundle started");
39+
this.nodecg.log.info(this.serviceType + " bundle started.");
3540
this.core = (this.nodecg.extensions["nodecg-io-core"] as unknown) as NodeCGIOCore | undefined;
3641
if (this.core === undefined) {
3742
this.nodecg.log.error(
@@ -40,16 +45,16 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
4045
}
4146
}
4247

48+
/**
49+
* Registers this service bundle at the core bundle, makes it appear in the GUI and makes it usable.
50+
* @return a service provider for this service, can be used by bundles to depend on this service.
51+
*/
4352
public register(): ServiceProvider<C> | undefined {
44-
if (this.core === undefined) {
45-
return undefined;
46-
} else {
47-
// Hide nodecg variable from serialization.
48-
// The service is saved in a Replicant and nodecg tries to serialize everything in there, including
49-
// nodecg instances, which throw errors when serialized.
50-
Object.defineProperty(this, "nodecg", { enumerable: false });
51-
return this.core.registerService(this);
52-
}
53+
// Hide nodecg variable from serialization.
54+
// The service is saved in a Replicant and nodecg tries to serialize everything in there, including
55+
// nodecg instances, which throw errors when serialized.
56+
Object.defineProperty(this, "nodecg", { enumerable: false });
57+
return this.core?.registerService(this);
5358
}
5459

5560
/**
@@ -77,7 +82,7 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
7782
*/
7883
abstract stopClient(client: C): void;
7984

80-
private readSchema(pathSegments: string[]) {
85+
private readSchema(pathSegments: string[]): unknown {
8186
const joinedPath = path.resolve(...pathSegments);
8287
try {
8388
const fileContent = fs.readFileSync(joinedPath, "utf8");

0 commit comments

Comments
 (0)