@momo2555/koppeliajs 0.0.167 → 0.0.168
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import type { Console } from "./console.js";
|
|
2
2
|
/**
|
|
3
3
|
* Manages the registration and network-wide execution of custom callbacks.
|
|
4
|
-
*
|
|
4
|
+
* Supports multiple listeners for the same callback name.
|
|
5
5
|
*/
|
|
6
6
|
export declare class CustomCallbacks {
|
|
7
7
|
private _console;
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Internal storage for callbacks.
|
|
10
|
+
* _registry: Map<uniqueId, { name: string, func: Function }>
|
|
11
|
+
*/
|
|
12
|
+
private _registry;
|
|
13
|
+
/**
|
|
14
|
+
* Quick lookup to find all unique IDs associated with a specific callback name.
|
|
15
|
+
* _nameToIds: Map<callbackName, Set<uniqueId>>
|
|
16
|
+
*/
|
|
17
|
+
private _nameToIds;
|
|
9
18
|
constructor(console: Console);
|
|
10
19
|
/**
|
|
11
20
|
* Broadcasts a request across the network to execute a registered custom callback.
|
|
@@ -19,13 +28,26 @@ export declare class CustomCallbacks {
|
|
|
19
28
|
* Registers a local function to be executed when a specific custom callback request is received.
|
|
20
29
|
* @param name The identifier name for this callback.
|
|
21
30
|
* @param callback The function to execute when triggered by the network.
|
|
31
|
+
* @returns A unique ID for this specific registration.
|
|
22
32
|
*/
|
|
23
33
|
registerCustomCallback(name: string, callback: (args: {
|
|
24
34
|
[key: string]: any;
|
|
25
|
-
}) => void):
|
|
35
|
+
}) => void): string;
|
|
26
36
|
/**
|
|
27
|
-
* Removes
|
|
28
|
-
* @param name The identifier name of the
|
|
37
|
+
* Removes all listeners registered under a specific callback name.
|
|
38
|
+
* @param name The identifier name of the callbacks to remove.
|
|
29
39
|
*/
|
|
30
40
|
unregisterCustomCallback(name: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Removes a specific listener using its unique registration ID.
|
|
43
|
+
* @param id The unique identifier returned by registerCustomCallback.
|
|
44
|
+
* @returns True if the callback was found and removed, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
unregisterById(id: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Internal method to trigger all local functions associated with a callback name.
|
|
49
|
+
* @param name The callback name received from the network.
|
|
50
|
+
* @param args The arguments received from the network.
|
|
51
|
+
*/
|
|
52
|
+
private _executeLocalCallbacks;
|
|
31
53
|
}
|
|
@@ -2,22 +2,28 @@ import { logger } from "./logger.js";
|
|
|
2
2
|
import { Message, MessageType, PeerType } from "./message.js";
|
|
3
3
|
/**
|
|
4
4
|
* Manages the registration and network-wide execution of custom callbacks.
|
|
5
|
-
*
|
|
5
|
+
* Supports multiple listeners for the same callback name.
|
|
6
6
|
*/
|
|
7
7
|
export class CustomCallbacks {
|
|
8
8
|
_console;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Internal storage for callbacks.
|
|
11
|
+
* _registry: Map<uniqueId, { name: string, func: Function }>
|
|
12
|
+
*/
|
|
13
|
+
_registry;
|
|
14
|
+
/**
|
|
15
|
+
* Quick lookup to find all unique IDs associated with a specific callback name.
|
|
16
|
+
* _nameToIds: Map<callbackName, Set<uniqueId>>
|
|
17
|
+
*/
|
|
18
|
+
_nameToIds;
|
|
10
19
|
constructor(console) {
|
|
11
20
|
this._console = console;
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
this._customCallbacks = {};
|
|
21
|
+
this._registry = new Map();
|
|
22
|
+
this._nameToIds = new Map();
|
|
15
23
|
this._console.onDataExchange((from, data) => {
|
|
16
24
|
if (data.customCallbackName != undefined &&
|
|
17
25
|
data.customCallbackArgs != undefined) {
|
|
18
|
-
|
|
19
|
-
this._customCallbacks[data.customCallbackName](data.customCallbackArgs);
|
|
20
|
-
}
|
|
26
|
+
this._executeLocalCallbacks(data.customCallbackName, data.customCallbackArgs);
|
|
21
27
|
}
|
|
22
28
|
});
|
|
23
29
|
}
|
|
@@ -38,17 +44,74 @@ export class CustomCallbacks {
|
|
|
38
44
|
* Registers a local function to be executed when a specific custom callback request is received.
|
|
39
45
|
* @param name The identifier name for this callback.
|
|
40
46
|
* @param callback The function to execute when triggered by the network.
|
|
47
|
+
* @returns A unique ID for this specific registration.
|
|
41
48
|
*/
|
|
42
49
|
registerCustomCallback(name, callback) {
|
|
43
|
-
|
|
50
|
+
const id = Math.random().toString(36).substring(2, 15);
|
|
51
|
+
// Register the function in the main registry
|
|
52
|
+
this._registry.set(id, { name, func: callback });
|
|
53
|
+
// Add the ID to the name-based lookup table
|
|
54
|
+
if (!this._nameToIds.has(name)) {
|
|
55
|
+
this._nameToIds.set(name, new Set());
|
|
56
|
+
}
|
|
57
|
+
this._nameToIds.get(name).add(id);
|
|
58
|
+
return id;
|
|
44
59
|
}
|
|
45
60
|
/**
|
|
46
|
-
* Removes
|
|
47
|
-
* @param name The identifier name of the
|
|
61
|
+
* Removes all listeners registered under a specific callback name.
|
|
62
|
+
* @param name The identifier name of the callbacks to remove.
|
|
48
63
|
*/
|
|
49
64
|
unregisterCustomCallback(name) {
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
const ids = this._nameToIds.get(name);
|
|
66
|
+
if (ids) {
|
|
67
|
+
for (const id of ids) {
|
|
68
|
+
this._registry.delete(id);
|
|
69
|
+
}
|
|
70
|
+
this._nameToIds.delete(name);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Removes a specific listener using its unique registration ID.
|
|
75
|
+
* @param id The unique identifier returned by registerCustomCallback.
|
|
76
|
+
* @returns True if the callback was found and removed, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
unregisterById(id) {
|
|
79
|
+
const entry = this._registry.get(id);
|
|
80
|
+
if (entry) {
|
|
81
|
+
const { name } = entry;
|
|
82
|
+
// Remove from the main registry
|
|
83
|
+
this._registry.delete(id);
|
|
84
|
+
// Remove from the name-based lookup table
|
|
85
|
+
const ids = this._nameToIds.get(name);
|
|
86
|
+
if (ids) {
|
|
87
|
+
ids.delete(id);
|
|
88
|
+
if (ids.size === 0) {
|
|
89
|
+
this._nameToIds.delete(name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Internal method to trigger all local functions associated with a callback name.
|
|
98
|
+
* @param name The callback name received from the network.
|
|
99
|
+
* @param args The arguments received from the network.
|
|
100
|
+
*/
|
|
101
|
+
_executeLocalCallbacks(name, args) {
|
|
102
|
+
const ids = this._nameToIds.get(name);
|
|
103
|
+
if (ids) {
|
|
104
|
+
for (const id of ids) {
|
|
105
|
+
const entry = this._registry.get(id);
|
|
106
|
+
if (entry) {
|
|
107
|
+
try {
|
|
108
|
+
entry.func(args);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
logger.log(`Error executing custom callback "${name}" (ID: ${id}):`, error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
52
115
|
}
|
|
53
116
|
}
|
|
54
117
|
}
|
|
@@ -240,10 +240,15 @@ export declare class Koppelia {
|
|
|
240
240
|
*/
|
|
241
241
|
on(callbackName: string, callback: (args: {
|
|
242
242
|
[key: string]: any;
|
|
243
|
-
}) => void):
|
|
243
|
+
}) => void): string;
|
|
244
244
|
/**
|
|
245
245
|
* Unregisters a locally listening custom callback.
|
|
246
|
-
* @param callbackName The
|
|
246
|
+
* @param callbackName The name of the callbacks to remove.
|
|
247
247
|
*/
|
|
248
248
|
unsub(callbackName: string): void;
|
|
249
|
+
/**
|
|
250
|
+
* Unregisters a locally listening custom callback using its unique ID
|
|
251
|
+
* @param callbackId The identifier of the callback to remove.
|
|
252
|
+
*/
|
|
253
|
+
unsubById(callbackId: string): void;
|
|
249
254
|
}
|
package/dist/scripts/koppelia.js
CHANGED
|
@@ -518,13 +518,20 @@ export class Koppelia {
|
|
|
518
518
|
* @param callback The local function to execute.
|
|
519
519
|
*/
|
|
520
520
|
on(callbackName, callback) {
|
|
521
|
-
this._callbacks.registerCustomCallback(callbackName, callback);
|
|
521
|
+
return this._callbacks.registerCustomCallback(callbackName, callback);
|
|
522
522
|
}
|
|
523
523
|
/**
|
|
524
524
|
* Unregisters a locally listening custom callback.
|
|
525
|
-
* @param callbackName The
|
|
525
|
+
* @param callbackName The name of the callbacks to remove.
|
|
526
526
|
*/
|
|
527
527
|
unsub(callbackName) {
|
|
528
528
|
this._callbacks.unregisterCustomCallback(callbackName);
|
|
529
529
|
}
|
|
530
|
+
/**
|
|
531
|
+
* Unregisters a locally listening custom callback using its unique ID
|
|
532
|
+
* @param callbackId The identifier of the callback to remove.
|
|
533
|
+
*/
|
|
534
|
+
unsubById(callbackId) {
|
|
535
|
+
this._callbacks.unregisterById(callbackId);
|
|
536
|
+
}
|
|
530
537
|
}
|