space-react-client 0.2.0 → 0.2.1
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 +2 -1
- package/dist/index.js +18 -4
- package/package.json +1 -1
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
|
-
|
|
47
|
+
connectWebSocket(): void;
|
|
47
48
|
/**
|
|
48
49
|
* Listen to SPACE and connection events.
|
|
49
50
|
* @param event The event key to listen for.
|
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
|
|
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
|
}
|
|
@@ -198,7 +204,8 @@ const SpaceContext = createContext(undefined);
|
|
|
198
204
|
const SpaceProvider = ({ config, children, }) => {
|
|
199
205
|
// Memorize the client to avoid unnecessary re-instantiation
|
|
200
206
|
const context = useMemo(() => {
|
|
201
|
-
const
|
|
207
|
+
const denyConnectionWithSpace = config.allowConnectionWithSpace === false;
|
|
208
|
+
const client = denyConnectionWithSpace ? undefined : new SpaceClient(config);
|
|
202
209
|
let tokenService;
|
|
203
210
|
if (!client) {
|
|
204
211
|
tokenService = new TokenService();
|
|
@@ -211,6 +218,13 @@ const SpaceProvider = ({ config, children, }) => {
|
|
|
211
218
|
tokenService: tokenService,
|
|
212
219
|
};
|
|
213
220
|
}, [config.url, config.apiKey]);
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
return () => {
|
|
223
|
+
if (context.client && typeof context.client.disconnectWebSocket === 'function') {
|
|
224
|
+
context.client.disconnectWebSocket();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}, [context.client]);
|
|
214
228
|
return jsx(SpaceContext.Provider, { value: context, children: children });
|
|
215
229
|
};
|
|
216
230
|
|