Skip to content

Commit

Permalink
Added a ServiceClient for direct and future references (cross lang)
Browse files Browse the repository at this point in the history
Added another test case for the new client
  • Loading branch information
mrinc committed Feb 24, 2024
1 parent ddfde91 commit 1e125b4
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
5 changes: 4 additions & 1 deletion nodejs/sec-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default:
service-default0X:
plugin: service-default0
enabled: true
config:
config:
testa: 5
testb: 6
service-default1X:
Expand All @@ -21,3 +21,6 @@ default:
service-default3X:
plugin: service-default3
enabled: true
service-default4X:
plugin: service-default4
enabled: true
2 changes: 1 addition & 1 deletion nodejs/src/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export abstract class MainBase {
* The name of the plugin
* This is also the mapped name, or the name defined in the config rather than it's original defined name
*/
public readonly pluginName!: string;
public declare readonly pluginName: string;

constructor(config: MainBaseConfig) {
this.appId = config.appId;
Expand Down
2 changes: 2 additions & 0 deletions nodejs/src/base/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export abstract class BSBService<
? null
: BSBReferencePluginConfigDefinition<ReferencedConfig>
> {
public static PLUGIN_NAME: string;
public abstract readonly initBeforePlugins?: Array<string>;
public abstract readonly initAfterPlugins?: Array<string>;
public abstract readonly runBeforePlugins?: Array<string>;
Expand Down Expand Up @@ -63,6 +64,7 @@ export abstract class BSBService<
* DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS
*/
export class BSBServiceRef extends BSBService<null> {
public static PLUGIN_NAME = "BSBServiceRef";
public methods = {};
public initBeforePlugins?: string[] | undefined;
public initAfterPlugins?: string[] | undefined;
Expand Down
54 changes: 50 additions & 4 deletions nodejs/src/base/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import {
IPluginLogger,
DynamicallyReferencedMethodCallable,
} from "../interfaces";
import { BSBService, BSBError, PluginEvents } from "./index";
import { BSBService, BSBError, PluginEvents, BSBServiceRef } from "./index";
import { DynamicallyReferencedMethodType } from "@bettercorp/tools/lib/Interfaces";

/**
* @deprecated Use ServiceClient instead
* @description [NOT REALLY DEPRECATED] - ONLY USE THIS IF YOU NEED SPECIFIC CLIENT LOGIC, OTHERWISE USE ServiceClient
*/
export abstract class BSBServiceClient<Service extends BSBService = any> {
protected readonly log!: IPluginLogger;
protected readonly events!: PluginEvents<
protected declare readonly log: IPluginLogger;
protected declare readonly events: PluginEvents<
Service["_virtual_internal_events"]["emitEvents"],
Service["_virtual_internal_events"]["onEvents"],
Service["_virtual_internal_events"]["emitReturnableEvents"],
Service["_virtual_internal_events"]["onReturnableEvents"],
Service["_virtual_internal_events"]["emitBroadcast"],
Service["_virtual_internal_events"]["onBroadcast"]
>;
public callMethod<TA extends keyof Service["methods"]>(
protected callMethod<TA extends keyof Service["methods"]>(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...args: DynamicallyReferencedMethodCallable<
DynamicallyReferencedMethodType<Service["methods"]>,
Expand Down Expand Up @@ -47,6 +51,48 @@ export abstract class BSBServiceClient<Service extends BSBService = any> {
public abstract run?(): Promise<void>;
}

export class ServiceClient<
Service extends BSBService,
ServiceT extends typeof BSBServiceRef = any
> extends BSBServiceClient<Service> {
public readonly pluginName: string = "{UNSET SERVICE CLIENT PLUGIN NAME}";
public readonly initBeforePlugins?: Array<string>;
public readonly initAfterPlugins?: Array<string>;
public readonly runBeforePlugins?: Array<string>;
public readonly runAfterPlugins?: Array<string>;
public dispose?(): void;
public init?(): Promise<void>;
public run?(): Promise<void>;

public override callMethod<TA extends keyof Service["methods"]>(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...args: DynamicallyReferencedMethodCallable<
DynamicallyReferencedMethodType<Service["methods"]>,
TA
>
): DynamicallyReferencedMethodCallable<
DynamicallyReferencedMethodType<Service["methods"]>,
TA,
false
> {
return this.callMethod(...args);
}

public declare events: PluginEvents<
Service["_virtual_internal_events"]["emitEvents"],
Service["_virtual_internal_events"]["onEvents"],
Service["_virtual_internal_events"]["emitReturnableEvents"],
Service["_virtual_internal_events"]["onReturnableEvents"],
Service["_virtual_internal_events"]["emitBroadcast"],
Service["_virtual_internal_events"]["onBroadcast"]
>;

constructor(service: ServiceT, context: BSBService) {
super(context);
this.pluginName = service.PLUGIN_NAME;
}
}

/**
* DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS
*/
Expand Down
2 changes: 1 addition & 1 deletion nodejs/src/plugins/service-default1/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Plugin extends BSBService<null, Events> {
run?(): void | Promise<void>;

private count = 0;
public override async init() {
public async init() {
this.log.info("INIT SERVICE");
this.events.onEvent("onReceivable", async (a: number, b: number) => {
this.count++;
Expand Down
4 changes: 2 additions & 2 deletions nodejs/src/plugins/service-default2/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export class Plugin extends BSBService<null> {
public methods = {};
dispose?(): void;
init?(): void | Promise<void>;
public override initAfterPlugins: string[] = ["service-default1"];
public initAfterPlugins: string[] = ["service-default1"];
private testClient: testClient;
constructor(config: BSBServiceConstructor) {
super(config);
this.testClient = new testClient(this);
}
public override async run() {
public async run() {
await this.testClient.abc(10, 12, 11, 13);
}
}
19 changes: 15 additions & 4 deletions nodejs/src/plugins/service-default3/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ import { BSBService, BSBServiceConstructor } from "../../";
import { testClient } from "../service-default1";

export class Plugin extends BSBService<null> {
public static PLUGIN_NAME = "service-default3";
public initBeforePlugins?: string[] | undefined;
public runBeforePlugins?: string[] | undefined;
public runAfterPlugins?: string[] | undefined;
public methods = {};
public methods = {
testMethod: () => {
this.log.info("TEST CALLABLE OK");
return "test";
},
};
dispose?(): void;
init?(): void | Promise<void>;
public override initAfterPlugins: string[] = ["service-default2"];
public initAfterPlugins: string[] = ["service-default2"];
private testClient: testClient;
constructor(config: BSBServiceConstructor) {
super(config);
this.testClient = new testClient(this);
}
public override async run() {
public async init() {
await this.events.onReturnableEvent("onReverseReturnable", async (tex: string) => {
this.log.warn("onReverseReturnable ({tex})", { tex });
return tex.split("").reverse().join("");
});
}
public async run() {
await this.testClient.abc(18, 19, 20, 21);
this.log.error("Error {a}", new Error("err"), { a: "b" });
}
Expand Down
29 changes: 29 additions & 0 deletions nodejs/src/plugins/service-default4/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BSBService, BSBServiceConstructor, ServiceClient } from "../../";
import { Plugin as S3Plugin } from "../service-default3/plugin";

export class Plugin extends BSBService<null> {
public initBeforePlugins?: string[] | undefined;
public runBeforePlugins?: string[] | undefined;
public runAfterPlugins?: string[] | undefined;
public methods = {};
dispose?(): void;
init?(): void | Promise<void>;
public override initAfterPlugins: string[] = [];
private service3: ServiceClient<S3Plugin>;
constructor(config: BSBServiceConstructor) {
super(config);
this.service3 = new ServiceClient(S3Plugin, this);
}
public override async run() {
this.log.info("TEST CALLABLE OK ? [{result}]", {
result: this.service3.callMethod("testMethod"),
});
this.log.info("TEST RETURNABLE OK ? [{result}]", {
result: await this.service3.events.emitEventAndReturn(
"onReverseReturnable",
5,
"teXt"
),
});
}
}

0 comments on commit 1e125b4

Please sign in to comment.