space-react-client 0.2.0 → 0.2.2

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.
package/dist/index.d.ts CHANGED
@@ -40,10 +40,11 @@ declare class SpaceClient$1 {
40
40
  readonly tokenService: TokenService$1;
41
41
  private userId;
42
42
  constructor(config: SpaceConfiguration);
43
+ disconnectWebSocket(): void;
43
44
  /**
44
45
  * Connects to the SPACE WebSocket and handles incoming events.
45
46
  */
46
- private connectWebSocket;
47
+ connectWebSocket(): void;
47
48
  /**
48
49
  * Listen to SPACE and connection events.
49
50
  * @param event The event key to listen for.
@@ -58,6 +59,12 @@ declare class SpaceClient$1 {
58
59
  * @throws Will throw an error if the callback is not a function.
59
60
  */
60
61
  on(event: SpaceEvents, callback: (data: any) => void): void;
62
+ /**
63
+ * Removes event listeners.
64
+ * @param event (optional) The event to remove listeners for. If omitted, removes all listeners.
65
+ * @param callback (optional) The specific callback to remove.
66
+ */
67
+ off(event?: SpaceEvents, callback?: (data: any) => void): void;
61
68
  /**
62
69
  * Sets the user ID for the client and loads the pricing token for that user.
63
70
  * @param userId The user ID to set for the client.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import React, { createContext, useMemo, useContext, useState, useEffect } from 'react';
2
+ import React, { createContext, useMemo, useEffect, useContext, useState } from 'react';
3
3
  import { TinyEmitter } from 'tiny-emitter';
4
4
  import axios from 'axios';
5
5
  import { io } from 'socket.io-client';
@@ -105,6 +105,12 @@ class SpaceClient {
105
105
  });
106
106
  this.connectWebSocket();
107
107
  }
108
+ disconnectWebSocket() {
109
+ if (this.pricingSocketNamespace.connected) {
110
+ this.pricingSocketNamespace.disconnect();
111
+ this.emitter.off(); // Remove all listeners
112
+ }
113
+ }
108
114
  /**
109
115
  * Connects to the SPACE WebSocket and handles incoming events.
110
116
  */
@@ -113,11 +119,11 @@ class SpaceClient {
113
119
  console.log('Connected to SPACE');
114
120
  this.emitter.emit('synchronized', 'WebSocket connection established');
115
121
  });
116
- this.pricingSocketNamespace.on('message', data => {
122
+ this.pricingSocketNamespace.on('message', (data) => {
117
123
  const event = data.code.toLowerCase();
118
124
  this.emitter.emit(event, data.details);
119
125
  });
120
- this.pricingSocketNamespace.on('connect_error', error => {
126
+ this.pricingSocketNamespace.on('connect_error', (error) => {
121
127
  this.emitter.emit('error', error);
122
128
  });
123
129
  }
@@ -150,6 +156,22 @@ class SpaceClient {
150
156
  }
151
157
  this.emitter.on(event, callback);
152
158
  }
159
+ /**
160
+ * Removes event listeners.
161
+ * @param event (optional) The event to remove listeners for. If omitted, removes all listeners.
162
+ * @param callback (optional) The specific callback to remove.
163
+ */
164
+ off(event, callback) {
165
+ if (event && callback) {
166
+ this.emitter.off(event, callback);
167
+ }
168
+ else if (event) {
169
+ this.emitter.off(event);
170
+ }
171
+ else {
172
+ this.emitter.off();
173
+ }
174
+ }
153
175
  /**
154
176
  * Sets the user ID for the client and loads the pricing token for that user.
155
177
  * @param userId The user ID to set for the client.
@@ -198,7 +220,8 @@ const SpaceContext = createContext(undefined);
198
220
  const SpaceProvider = ({ config, children, }) => {
199
221
  // Memorize the client to avoid unnecessary re-instantiation
200
222
  const context = useMemo(() => {
201
- const client = config.allowConnectionWithSpace ? new SpaceClient(config) : undefined;
223
+ const denyConnectionWithSpace = config.allowConnectionWithSpace === false;
224
+ const client = denyConnectionWithSpace ? undefined : new SpaceClient(config);
202
225
  let tokenService;
203
226
  if (!client) {
204
227
  tokenService = new TokenService();
@@ -211,6 +234,13 @@ const SpaceProvider = ({ config, children, }) => {
211
234
  tokenService: tokenService,
212
235
  };
213
236
  }, [config.url, config.apiKey]);
237
+ useEffect(() => {
238
+ return () => {
239
+ if (context.client && typeof context.client.disconnectWebSocket === 'function') {
240
+ context.client.disconnectWebSocket();
241
+ }
242
+ };
243
+ }, [context.client]);
214
244
  return jsx(SpaceContext.Provider, { value: context, children: children });
215
245
  };
216
246
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "space-react-client",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",