Skip to content

Commit

Permalink
fix native transport
Browse files Browse the repository at this point in the history
  • Loading branch information
azahnen committed Jan 8, 2025
1 parent 148ae60 commit 98a3151
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
3 changes: 2 additions & 1 deletion libxtracfg/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
ext {
targetos = osdetector.os.replace('osx', 'darwin')
targetarch = osdetector.arch.replace('x86_64', 'amd64').replace('aarch_64', 'arm64')
isCi = project.hasProperty('CI') || System.getenv('CI') != null
}

group = 'de.interactive_instruments'
Expand All @@ -27,7 +28,7 @@ repositories {

dependencies {
implementation group: 'de.interactive_instruments', name: 'ldproxy-cfg', version: '4.3.0-SNAPSHOT'
implementation group: 'org.slf4j', name: project.hasProperty('CI') ? 'slf4j-nop' : 'slf4j-simple', version: '2.0.16'
implementation group: 'org.slf4j', name: isCi ? 'slf4j-nop' : 'slf4j-simple', version: '2.0.16'

// 24.x is for JDK 21
compileOnly group: 'org.graalvm.nativeimage', name: 'svm', version: '24.1.0'
Expand Down
56 changes: 37 additions & 19 deletions libxtracfg/js/xtracfg-transport-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Transport, TransportCreator, Response, Listener } from "@xtracfg/core";
import {
Transport,
TransportCreator,
TransportOptions,
Response,
Listener,
} from "@xtracfg/core";
import addon from "@xtracfg/native";

const allListeners: Listener[][] = [];
Expand All @@ -7,29 +13,41 @@ const broadcast = (response: Response) => {
allListeners.flat().forEach((listener) => listener(response));
};

const transport: TransportCreator = () => {
return async (): Promise<Transport> => {
const listeners: Listener[] = [];
allListeners.push(listeners);
const transport: TransportCreator = ({ debug }: TransportOptions) => {
const listeners: Listener[] = [];
allListeners.push(listeners);

addon.subscribe((response: string) => broadcast(JSON.parse(response)));
if (debug) {
console.log("native transport created");
}

return {
send: async (request) => {
if (listeners.length > 0) {
const response = addon.execute(JSON.stringify(request));
addon.subscribe((response: string) => broadcast(JSON.parse(response)));

broadcast(JSON.parse(response));
const transport: Transport = {
send: async (request) => {
if (debug) {
console.log("sending to native xtracfg", request, listeners.length);
}

if (listeners.length > 0) {
const response = addon.execute(JSON.stringify(request));

if (debug) {
console.log("received from native xtracfg", response);
}
},
listen: async (listener) => {
listeners.push(listener);
},
stop: async () => {
listeners.length = 0;
},
};

broadcast(JSON.parse(response));
}
},
listen: async (listener) => {
listeners.push(listener);
},
stop: async () => {
listeners.length = 0;
},
};

return async (): Promise<Transport> => transport;
};

export default transport;
4 changes: 3 additions & 1 deletion libxtracfg/js/xtracfg-transport-native/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { connect } from "@xtracfg/core";
import transport from "../src";

const xtracfg = connect(transport);
const xtracfg = connect(transport, { debug: true });

xtracfg.listen(
(response) => {
Expand All @@ -16,3 +16,5 @@ xtracfg.send({
command: "info",
source: "/Users/pascal/Documents/GitHub/demo",
});

//TODO: proper unit test that checks the response

0 comments on commit 98a3151

Please sign in to comment.