space-react-client 0.2.4 → 0.2.5

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
@@ -2,6 +2,7 @@ import React, { JSX } from 'react';
2
2
 
3
3
  declare class TokenService$1 {
4
4
  private tokenPayload;
5
+ private listeners;
5
6
  /**
6
7
  * Retrieves the stored pricing token's payload.
7
8
  * @returns The stored pricing token payload.
@@ -20,6 +21,12 @@ declare class TokenService$1 {
20
21
  updatePricingToken(token: string): void;
21
22
  evaluateFeature(featureId: string): boolean | null;
22
23
  private _validToken;
24
+ /**
25
+ * Subscribe to pricing token updates. Returns an unsubscribe function.
26
+ */
27
+ subscribe(listener: () => void): () => void;
28
+ /** Notify all listeners that the token has changed. */
29
+ private _notify;
23
30
  }
24
31
 
25
32
  /**
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ function isTokenExpired(tokenPayload) {
22
22
  class TokenService {
23
23
  constructor() {
24
24
  this.tokenPayload = null;
25
+ this.listeners = new Set();
25
26
  }
26
27
  /**
27
28
  * Retrieves the stored pricing token's payload.
@@ -51,6 +52,7 @@ class TokenService {
51
52
  updatePricingToken(token) {
52
53
  const parsedToken = parseJwt(token);
53
54
  this.tokenPayload = parsedToken;
55
+ this._notify();
54
56
  }
55
57
  evaluateFeature(featureId) {
56
58
  if (!this._validToken()) {
@@ -76,6 +78,26 @@ class TokenService {
76
78
  }
77
79
  return true;
78
80
  }
81
+ /**
82
+ * Subscribe to pricing token updates. Returns an unsubscribe function.
83
+ */
84
+ subscribe(listener) {
85
+ this.listeners.add(listener);
86
+ return () => {
87
+ this.listeners.delete(listener);
88
+ };
89
+ }
90
+ /** Notify all listeners that the token has changed. */
91
+ _notify() {
92
+ this.listeners.forEach((l) => {
93
+ try {
94
+ l();
95
+ }
96
+ catch (e) {
97
+ console.error(e);
98
+ }
99
+ });
100
+ }
79
101
  }
80
102
 
81
103
  /**
@@ -234,7 +256,7 @@ const SpaceProvider = ({ config, children, }) => {
234
256
  client: client,
235
257
  tokenService: tokenService,
236
258
  };
237
- }, [config.url, config.apiKey]);
259
+ }, [config.url, config.apiKey, config.allowConnectionWithSpace]);
238
260
  useEffect(() => {
239
261
  return () => {
240
262
  if (context.client && typeof context.client.disconnectWebSocket === 'function') {
@@ -300,25 +322,36 @@ const Feature = ({ id, children }) => {
300
322
  // Validate id
301
323
  const isValidId = useMemo(() => id.includes('-'), [id]);
302
324
  useEffect(() => {
303
- if (!isValidId) {
304
- setStatus('error');
305
- return;
306
- }
307
- if (tokenService.getPricingToken() === null) {
308
- setStatus('error');
309
- return;
310
- }
311
- setStatus('loading');
312
- setResult(null);
313
- const evaluationResult = tokenService.evaluateFeature(id);
314
- if (evaluationResult === null || evaluationResult === undefined) {
315
- setStatus('error');
316
- }
317
- else {
318
- setResult(evaluationResult);
319
- setStatus('success');
320
- }
321
- }, [id, isValidId, tokenService.tokenPayload]);
325
+ const evaluate = () => {
326
+ if (!isValidId) {
327
+ setStatus('error');
328
+ return;
329
+ }
330
+ if (tokenService.getPricingToken() === null) {
331
+ setStatus('error');
332
+ return;
333
+ }
334
+ setStatus('loading');
335
+ setResult(null);
336
+ const evaluationResult = tokenService.evaluateFeature(id);
337
+ if (evaluationResult === null || evaluationResult === undefined) {
338
+ setStatus('error');
339
+ }
340
+ else {
341
+ setResult(evaluationResult);
342
+ setStatus('success');
343
+ }
344
+ };
345
+ // Initial evaluation
346
+ evaluate();
347
+ // Subscribe to token changes to re-evaluate
348
+ const unsubscribe = tokenService.subscribe(() => {
349
+ evaluate();
350
+ });
351
+ return () => {
352
+ unsubscribe();
353
+ };
354
+ }, [id, isValidId, tokenService]);
322
355
  if (status === 'loading') {
323
356
  return jsx(Fragment, { children: getChildrenOfType(children, Loading) });
324
357
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "space-react-client",
3
3
  "type": "module",
4
- "version": "0.2.4",
4
+ "version": "0.2.5",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",