@notabene/javascript-sdk 2.0.0-next.17 → 2.0.0-next.19

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,8 +1,26 @@
1
1
  import { ComponentMessage, HostMessage } from '../types';
2
2
 
3
+ /**
4
+ * Callback function for handling component messages.
5
+ *
6
+ * @typeParam T - The type of data contained in the component message
7
+ * @param message - The message object containing the component data and type
8
+ * @public
9
+ */
10
+
3
11
  export type MessageCallback<T> = (message: ComponentMessage<T>) => void;
4
12
 
5
- export class MessageEventManager<T> {
13
+ /**
14
+ * Manages message event communication through MessagePorts.
15
+ *
16
+ * This class handles bidirectional communication between components using MessagePorts,
17
+ * allowing subscription to specific message types and dispatching messages to registered callbacks.
18
+ *
19
+ * @typeParam T - The type of data contained in messages received from components
20
+ * @typeParam O - The type of data contained in messages sent from the host
21
+ */
22
+
23
+ export class MessageEventManager<T, O> {
6
24
  private listeners: Map<string, Set<MessageCallback<T>>> = new Map();
7
25
  private port?: MessagePort;
8
26
 
@@ -10,12 +28,31 @@ export class MessageEventManager<T> {
10
28
  this.handleMessage = this.handleMessage.bind(this);
11
29
  }
12
30
 
31
+ /**
32
+ * Sets up the message port for communication.
33
+ *
34
+ * Initializes the MessagePort for receiving messages by setting up the message handler
35
+ * and starting the port.
36
+ *
37
+ * @param port - The MessagePort instance to use for communication
38
+ */
39
+
13
40
  setPort(port: MessagePort): void {
14
41
  this.port = port;
15
42
  this.port.onmessage = this.handleMessage;
16
43
  this.port.start();
17
44
  }
18
45
 
46
+ /**
47
+ * Registers a callback for a specific message type.
48
+ *
49
+ * When messages of the specified type are received, the callback will be executed
50
+ * with the message data.
51
+ *
52
+ * @param messageType - The type of message to listen for
53
+ * @param callback - The callback function to execute when matching messages are received
54
+ */
55
+
19
56
  on(messageType: string, callback: MessageCallback<T>): void {
20
57
  if (!this.listeners.has(messageType)) {
21
58
  this.listeners.set(messageType, new Set());
@@ -23,6 +60,16 @@ export class MessageEventManager<T> {
23
60
  this.listeners.get(messageType)!.add(callback);
24
61
  }
25
62
 
63
+ /**
64
+ * Removes a callback for a specific message type.
65
+ *
66
+ * If the callback is the last one registered for the message type,
67
+ * the message type entry will be removed entirely.
68
+ *
69
+ * @param messageType - The type of message to remove listener from
70
+ * @param callback - The callback function to remove
71
+ */
72
+
26
73
  off(messageType: string, callback: MessageCallback<T>): void {
27
74
  const callbacks = this.listeners.get(messageType);
28
75
  if (callbacks) {
@@ -33,6 +80,15 @@ export class MessageEventManager<T> {
33
80
  }
34
81
  }
35
82
 
83
+ /**
84
+ * Internal message handler for processing received messages.
85
+ *
86
+ * Validates incoming messages and dispatches them to registered callbacks
87
+ * based on the message type.
88
+ *
89
+ * @param event - The message event containing the component message
90
+ */
91
+
36
92
  private handleMessage(event: MessageEvent<ComponentMessage<T>>): void {
37
93
  // console.log('received message', event.data);
38
94
  const message = event.data;
@@ -45,8 +101,11 @@ export class MessageEventManager<T> {
45
101
  }
46
102
  }
47
103
 
48
- // Method to send messages through the port
49
- send(message: HostMessage<T>): void {
104
+ /**
105
+ * Sends a message through the message port
106
+ * @param message The host message to send
107
+ */
108
+ send(message: HostMessage<T, O>): void {
50
109
  if (this.port) {
51
110
  this.port.postMessage(message);
52
111
  }
@@ -1,9 +1,14 @@
1
1
  import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { HMType, UpdateValue, Withdrawal } from '../../types';
2
+ import {
3
+ HMType,
4
+ type TransactionOptions,
5
+ type UpdateValue,
6
+ type Withdrawal,
7
+ } from '../../types';
3
8
  import { MessageEventManager } from '../MessageEventManager';
4
9
 
5
10
  describe('MessageEventManager', () => {
6
- let messageEventManager: MessageEventManager<Withdrawal>;
11
+ let messageEventManager: MessageEventManager<Withdrawal, TransactionOptions>;
7
12
  let mockPort: MessagePort;
8
13
 
9
14
  beforeEach(() => {
@@ -13,7 +18,10 @@ describe('MessageEventManager', () => {
13
18
  start: vi.fn(),
14
19
  } as unknown as MessagePort;
15
20
 
16
- messageEventManager = new MessageEventManager();
21
+ messageEventManager = new MessageEventManager<
22
+ Withdrawal,
23
+ TransactionOptions
24
+ >();
17
25
  });
18
26
 
19
27
  describe('setPort', () => {
@@ -73,7 +81,7 @@ describe('MessageEventManager', () => {
73
81
  });
74
82
 
75
83
  it('should send messages through the port', () => {
76
- const message: UpdateValue<Withdrawal> = {
84
+ const message: UpdateValue<Withdrawal, TransactionOptions> = {
77
85
  type: HMType.UPDATE,
78
86
  value: { requestId: 'id' },
79
87
  };