@momo2555/koppeliajs 0.0.167 → 0.0.169

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
- * Allows different nodes in the system to broadcast and react to custom function calls.
4
+ * Supports multiple listeners for the same callback name.
5
5
  */
6
6
  export declare class CustomCallbacks {
7
7
  private _console;
8
- private _customCallbacks;
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): void;
35
+ }) => void): string;
26
36
  /**
27
- * Removes a previously registered custom callback from the local listener registry.
28
- * @param name The identifier name of the callback to remove.
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
- * Allows different nodes in the system to broadcast and react to custom function calls.
5
+ * Supports multiple listeners for the same callback name.
6
6
  */
7
7
  export class CustomCallbacks {
8
8
  _console;
9
- _customCallbacks;
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._console.onReady(() => {
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
- if (Object.hasOwn(this._customCallbacks, data.customCallbackName)) {
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
- this._customCallbacks[name] = callback;
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 a previously registered custom callback from the local listener registry.
47
- * @param name The identifier name of the callback to remove.
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
- if (Object.hasOwn(this._customCallbacks, name)) {
51
- delete this._customCallbacks[name];
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
  }
@@ -41,6 +41,11 @@ export declare class Device {
41
41
  * @param callback Function to execute with the incoming speed data.
42
42
  */
43
43
  onBiking(callback: (speed: number) => void): string;
44
+ /**
45
+ * Enables the biking module and listens for speed updates.
46
+ * @param callback Function to execute with the incoming speed data.
47
+ */
48
+ onRotation(callback: (omega: number) => void): string;
44
49
  /**
45
50
  * Enables the vertical detector module and listens for orientation updates.
46
51
  * @param callback Function to execute with the boolean vertical state.
@@ -78,6 +78,21 @@ export class Device {
78
78
  this._callbackIds.push(callbackId);
79
79
  return callbackId;
80
80
  }
81
+ /**
82
+ * Enables the biking module and listens for speed updates.
83
+ * @param callback Function to execute with the incoming speed data.
84
+ */
85
+ onRotation(callback) {
86
+ this._enableModule("rot");
87
+ this._attachEvent("rotation");
88
+ let callbackId = this._console.onRequest((request, params, form, address) => {
89
+ if (request == "rotation" && address == this._address) {
90
+ callback(params.omega);
91
+ }
92
+ });
93
+ this._callbackIds.push(callbackId);
94
+ return callbackId;
95
+ }
81
96
  /**
82
97
  * Enables the vertical detector module and listens for orientation updates.
83
98
  * @param callback Function to execute with the boolean vertical state.
@@ -240,10 +240,15 @@ export declare class Koppelia {
240
240
  */
241
241
  on(callbackName: string, callback: (args: {
242
242
  [key: string]: any;
243
- }) => void): void;
243
+ }) => void): string;
244
244
  /**
245
245
  * Unregisters a locally listening custom callback.
246
- * @param callbackName The identifier of the callback to remove.
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
  }
@@ -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 identifier of the callback to remove.
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo2555/koppeliajs",
3
- "version": "0.0.167",
3
+ "version": "0.0.169",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",