Skip to content

Commit 1d9ee73

Browse files
committed
add remote object count and ref/unref events
1 parent 0f2ed40 commit 1d9ee73

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

lib/node/node-wsh.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import EventEmitter from 'node:events';
12
import PATH from 'node:path';
23
import { Syncline} from "@arcticnotes/syncline";
34

@@ -7,7 +8,7 @@ const SCRIPT_FILE = PATH.join( PATH.dirname( import.meta.dirname), 'wsh', 'host.
78
const PROXY = Symbol();
89
const TRACE_REF = 1;
910

10-
export class WindowsScriptingHost {
11+
export class WindowsScriptingHost extends EventEmitter {
1112

1213
static async connect( options = {}) {
1314
const command = options.command || COMMAND;
@@ -24,15 +25,25 @@ export class WindowsScriptingHost {
2425
#Enumerator;
2526

2627
constructor( syncline, options) {
28+
super();
2729
this.#syncline = syncline;
2830
this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
2931
this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
30-
this.#proxies = new Proxies( syncline, options);
32+
this.#proxies = new Proxies( syncline, options, this);
3133
this.#WScript = this.#proxies.getOrCreateObject( 0);
3234
this.#GetObject = this.#proxies.getOrCreateFunction( 1);
3335
this.#Enumerator = this.#proxies.getOrCreateFunction( 2);
3436
}
3537

38+
get remoteObjects() {
39+
const proxies = this.#proxies;
40+
return {
41+
get count() {
42+
proxies.count;
43+
},
44+
};
45+
}
46+
3647
get WScript() {
3748
return this.#WScript;
3849
}
@@ -54,6 +65,7 @@ class Proxies {
5465

5566
#syncline;
5667
#trace;
68+
#eventEmitter;
5769
#finalizer = new FinalizationRegistry( this.#finalized.bind( this));
5870
#ref2proxy = new Map();
5971
#proxy2ref = new Map();
@@ -110,9 +122,14 @@ class Proxies {
110122
},
111123
};
112124

113-
constructor( syncline, options) {
125+
constructor( syncline, options, eventEmitter) {
114126
this.#syncline = syncline;
115127
this.#trace = options.trace;
128+
this.#eventEmitter = eventEmitter;
129+
}
130+
131+
get count() {
132+
return this.#ref2proxy.size;
116133
}
117134

118135
getOrCreateObject( ref) {
@@ -134,6 +151,7 @@ class Proxies {
134151
this.#ref2proxy.set( ref, newProxy);
135152
this.#proxy2ref.set( newProxy, ref);
136153
this.#finalizer.register( newProxy, ref);
154+
this.#eventEmitter.emit( 'ref', ref, newProxy);
137155
return newProxy;
138156
}
139157

@@ -150,6 +168,7 @@ class Proxies {
150168
default:
151169
console.log( `unknown response: ${ output[ 0]}`);
152170
}
171+
this.#eventEmitter.emit( 'unref', ref);
153172
}
154173

155174
#encode( decoded) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@arcticnotes/node-wsh",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "A Node.js package that runs Windows Scripting Host (WSH) as a child process and exposes the resources from the WSH world to the Node.js world",
55
"author": "Paul <paul@arcticnotes.com>",
66
"license": "MIT",

test/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { WindowsScriptingHost} from '@arcticnotes/node-wsh';
44

55
TEST( 'smoke-test', async() => {
66
const wsh = await WindowsScriptingHost.connect();
7+
wsh.on( 'ref', ( ref, obj) => console.log( 'ref', ref, obj));
8+
wsh.on( 'unref', ref => console.log( 'unref', ref));
79
try {
810
const { WScript, GetObject, Enumerator} = wsh;
911
console.log( WScript.Version);
@@ -14,6 +16,7 @@ TEST( 'smoke-test', async() => {
1416
ASSERT.equal( typeof enumerator.item().ProcessId, 'number');
1517
ASSERT.equal( typeof enumerator.item().Name, 'string');
1618
}
19+
console.log( 'remote objects:', wsh.remoteObjects.count);
1720
} finally {
1821
await wsh.disconnect();
1922
}

0 commit comments

Comments
 (0)