@evoke-platform/context 1.0.0-dev.112 → 1.0.0-dev.114

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/README.md CHANGED
@@ -17,7 +17,7 @@ available and no further installation is necessary.
17
17
 
18
18
  - [Working With Objects](#working-with-objects)
19
19
  - [REST API Calls](#rest-api-calls)
20
- - [SignalR Connection](#signalr-connection)
20
+ - [Notifications](#notifications)
21
21
 
22
22
  ### Working With Objects
23
23
 
@@ -206,26 +206,27 @@ absolute URL.
206
206
 
207
207
  ##### `delete(url, options)`
208
208
 
209
- ### SignalR Connection
209
+ ### Notifications
210
210
 
211
- Deprecated
212
-
213
- - [useSignalRConnection](#usesignalrconnection)
211
+ - [useNofitication](#usenotification)
214
212
  - [documentChanges](#documentchangessubscribeobjectidinstanceid-data-documentchange)
215
213
  - [instanceChanges](#instancechangessubscribeobjectid-instanceids-instancechange)
214
+ - [useSignalRConnection](#usesignalrconnection) (_Deprecated_)
215
+ - [documentChanges](#documentchangessubscribeobjectidinstanceid-data-documentchange) (_Deprecated_)
216
+ - [instanceChanges](#instancechangessubscribeobjectid-instanceids-instancechange) (_Deprecated_)
216
217
 
217
- #### `useSignalRConnection()`
218
+ #### `useNofitication()`
218
219
 
219
- Hook used to obtain an instanceChanges instance of `SignalRConnection` and a documentChanges instance of `SignalRConnection`.
220
+ Hook used to obtain an instanceChanges instance and a documentChanges instance.
220
221
 
221
- ##### `documentChanges.subscribe('{objectId}/{instanceId}', (data: DocumentChange[]]) => {})`
222
+ ##### `documentChanges.subscribe(objectId, instanceId, (data: DocumentChange[]]) => {})`
222
223
 
223
- Subscribe to the specified object instance document changes.
224
+ Subscribe to the specified object instance changes.
224
225
 
225
226
  ```javascript
226
- const { documentChanges } = useSignalRConnection();
227
+ const { documentChanges } = useNotification();
227
228
 
228
- documentChanges.subscribe('myObjectId/myInstanceId', (data) => {
229
+ documentChanges.subscribe('myObjectId', 'myInstanceId', (data) => {
229
230
  console.log(data);
230
231
  });
231
232
  ```
@@ -242,53 +243,61 @@ following data:
242
243
  - `type`
243
244
  - The type of update. Possible values are `BlobCreated`, `BlobDeleted`, and `BlobMetadataUpdated`.
244
245
 
245
- ##### `documentChanges.unsubscribe('{objectId}/{instanceId}', (data: DocumentChange[]) => {})`
246
+ ##### `documentChanges.unsubscribe(objectId, instanceId, (changes: DocumentChange[]) => {})`
246
247
 
247
- Unsubscribe to the specified object instance document changes.
248
+ Unsubscribe to the specified object instance changes.
248
249
 
249
250
  Callback function is optional.
251
+ If callback function is not defined, all subscriptions will be removed.
250
252
  If callback function is defined, you must pass the exact same Function instance as was previously passed to `documentChanges.subscribe`.
251
253
  Passing a different instance (even if the function body is the same) will not remove the subscription.
252
254
 
253
255
  ```javascript
254
- const { documentChanges } = useSignalRConnection();
256
+ const { documentChanges } = useNotification();
255
257
 
256
- const callback = (data: DocumentChange[]) => {
257
- console.log(data);
258
+ const callback = (changes: DocumentChange[]) => {
259
+ console.log(changes);
258
260
  };
259
261
 
260
- documentChanges.subscribe('myObjectId/myInstanceId', callback);
262
+ documentChanges.subscribe('myObjectId', 'myInstanceId', callback);
261
263
 
262
- documentChanges.unsubscribe('myObjectId/myInstanceId', callback);
264
+ documentChanges.unsubscribe('myObjectId', 'myInstanceId', callback);
263
265
  ```
264
266
 
265
- ##### `instanceChanges.subscribe('{objectId}', (instanceIds: InstanceChange[]) => {})`
267
+ ##### `instanceChanges.subscribe(objectId, (changes: InstanceChange[]) => {})`
266
268
 
267
- Subscribe to the specified object instance changes.
269
+ Subscribe to the specified object changes.
268
270
 
269
271
  ```javascript
270
- const { instanceChanges } = useSignalRConnection();
272
+ const { instanceChanges } = useNotification();
271
273
 
272
- instanceChanges.subscribe('myObjectId', (instanceIds) => {
273
- console.log(instanceIds);
274
+ instanceChanges.subscribe('myObjectId', (changes) => {
275
+ console.log(changes);
274
276
  });
275
277
  ```
276
278
 
277
- The data provided to the callback will be an array of instance IDs that were updated.
279
+ The data provided to the callback will be an array of `InstanceChange` which contains the
280
+ following data:
278
281
 
279
- ##### `instanceChanges.unsubscribe('{objectId}', (instanceIds: InstanceChange[]) => {})`
282
+ - `objectId`
283
+ - Object describing the instance associated with the updated document.
284
+ - `instanceId`
285
+ - Instance that the updated document is associated with.
280
286
 
281
- Unsubscribe to the specified object instance changes.
287
+ ##### `instanceChanges.unsubscribe(objectId, (changes: InstanceChange[]) => {})`
288
+
289
+ Unsubscribe to the specified object changes.
282
290
 
283
291
  Callback function is optional.
292
+ If callback function is not defined, all subscriptions will be removed.
284
293
  If callback function is defined, you must pass the exact same Function instance as was previously passed to `instanceChanges.subscribe`.
285
294
  Passing a different instance (even if the function body is the same) will not remove the subscription.
286
295
 
287
296
  ```javascript
288
- const { instanceChanges } = useSignalRConnection();
297
+ const { instanceChanges } = useNotification();
289
298
 
290
- const callback = (instanceIds: InstanceChange[]) => {
291
- console.log(instanceIds);
299
+ const callback = (changes: InstanceChange[]) => {
300
+ console.log(changes);
292
301
  };
293
302
 
294
303
  instanceChanges.subscribe('myObjectId', callback);
@@ -296,24 +305,24 @@ instanceChanges.subscribe('myObjectId', callback);
296
305
  instanceChanges.unsubscribe('myObjectId', callback);
297
306
  ```
298
307
 
299
- ### Notification
308
+ #### `useSignalRConnection()`
300
309
 
301
- - [useNofitication](#usenotification)
302
- - [documentChanges](#documentchangessubscribeobjectidinstanceid-data-documentchange)
303
- - [instanceChanges](#instancechangessubscribeobjectid-instanceids-instancechange)
310
+ > **Deprecated**
311
+ > This has been deprecated in favor of [useNotification](#usenofitication).
304
312
 
305
- #### `useNofitication()`
313
+ Hook used to obtain an instanceChanges instance of `SignalRConnection` and a documentChanges instance of `SignalRConnection`.
306
314
 
307
- Hook used to obtain an instanceChanges instance and a documentChanges instance.
315
+ ##### `documentChanges.subscribe('{objectId}/{instanceId}', (data: DocumentChange[]]) => {})`
308
316
 
309
- ##### `documentChanges.subscribe(objectId, instanceId, (data: DocumentChange[]]) => {})`
317
+ > **Deprecated**
318
+ > This has been deprecated in favor of [useNotification](#usenofitication).
310
319
 
311
- Subscribe to the specified object instance changes.
320
+ Subscribe to the specified object instance document changes.
312
321
 
313
322
  ```javascript
314
- const { documentChanges } = useNotification();
323
+ const { documentChanges } = useSignalRConnection();
315
324
 
316
- documentChanges.subscribe('myObjectId', 'myInstanceId', (data) => {
325
+ documentChanges.subscribe('myObjectId/myInstanceId', (data) => {
317
326
  console.log(data);
318
327
  });
319
328
  ```
@@ -330,61 +339,62 @@ following data:
330
339
  - `type`
331
340
  - The type of update. Possible values are `BlobCreated`, `BlobDeleted`, and `BlobMetadataUpdated`.
332
341
 
333
- ##### `documentChanges.unsubscribe(objectId, instanceId, (changes: DocumentChange[]) => {})`
342
+ ##### `documentChanges.unsubscribe('{objectId}/{instanceId}', (data: DocumentChange[]) => {})`
334
343
 
335
- Unsubscribe to the specified object instance changes.
344
+ > **Deprecated**
345
+ > This has been deprecated in favor of [useNotification](#usenofitication).
346
+
347
+ Unsubscribe to the specified object instance document changes.
336
348
 
337
349
  Callback function is optional.
338
- If callback function is not defined, all subscriptions will be removed.
339
350
  If callback function is defined, you must pass the exact same Function instance as was previously passed to `documentChanges.subscribe`.
340
351
  Passing a different instance (even if the function body is the same) will not remove the subscription.
341
352
 
342
353
  ```javascript
343
- const { documentChanges } = useNotification();
354
+ const { documentChanges } = useSignalRConnection();
344
355
 
345
- const callback = (changes: DocumentChange[]) => {
346
- console.log(changes);
356
+ const callback = (data: DocumentChange[]) => {
357
+ console.log(data);
347
358
  };
348
359
 
349
- documentChanges.subscribe('myObjectId', 'myInstanceId', callback);
360
+ documentChanges.subscribe('myObjectId/myInstanceId', callback);
350
361
 
351
- documentChanges.unsubscribe('myObjectId', 'myInstanceId', callback);
362
+ documentChanges.unsubscribe('myObjectId/myInstanceId', callback);
352
363
  ```
353
364
 
354
- ##### `instanceChanges.subscribe(objectId, (changes: InstanceChange[]) => {})`
365
+ ##### `instanceChanges.subscribe('{objectId}', (instanceIds: InstanceChange[]) => {})`
355
366
 
356
- Subscribe to the specified object changes.
367
+ > **Deprecated**
368
+ > This has been deprecated in favor of [useNotification](#usenofitication).
369
+
370
+ Subscribe to the specified object instance changes.
357
371
 
358
372
  ```javascript
359
- const { instanceChanges } = useNotification();
373
+ const { instanceChanges } = useSignalRConnection();
360
374
 
361
- instanceChanges.subscribe('myObjectId', (changes) => {
362
- console.log(changes);
375
+ instanceChanges.subscribe('myObjectId', (instanceIds) => {
376
+ console.log(instanceIds);
363
377
  });
364
378
  ```
365
379
 
366
- The data provided to the callback will be an array of `InstanceChange` which contains the
367
- following data:
380
+ The data provided to the callback will be an array of instance IDs that were updated.
368
381
 
369
- - `objectId`
370
- - Object describing the instance associated with the updated document.
371
- - `instanceId`
372
- - Instance that the updated document is associated with.
382
+ ##### `instanceChanges.unsubscribe('{objectId}', (instanceIds: InstanceChange[]) => {})`
373
383
 
374
- ##### `instanceChanges.unsubscribe(objectId, (changes: InstanceChange[]) => {})`
384
+ > **Deprecated**
385
+ > This has been deprecated in favor of [useNotification](#usenofitication).
375
386
 
376
- Unsubscribe to the specified object changes.
387
+ Unsubscribe to the specified object instance changes.
377
388
 
378
389
  Callback function is optional.
379
- If callback function is not defined, all subscriptions will be removed.
380
390
  If callback function is defined, you must pass the exact same Function instance as was previously passed to `instanceChanges.subscribe`.
381
391
  Passing a different instance (even if the function body is the same) will not remove the subscription.
382
392
 
383
393
  ```javascript
384
- const { instanceChanges } = useNotification();
394
+ const { instanceChanges } = useSignalRConnection();
385
395
 
386
- const callback = (changes: InstanceChange[]) => {
387
- console.log(changes);
396
+ const callback = (instanceIds: InstanceChange[]) => {
397
+ console.log(instanceIds);
388
398
  };
389
399
 
390
400
  instanceChanges.subscribe('myObjectId', callback);
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
- import { HubConnectionBuilder, LogLevel, } from '@microsoft/signalr/dist/esm/index.js';
11
+ import { HubConnectionBuilder, LogLevel, } from '@microsoft/signalr';
12
12
  import { createContext, useContext, useEffect, useState } from 'react';
13
13
  import { useApiServices } from '../api/index.js';
14
14
  export const NotificationContext = createContext({});
@@ -1,142 +1,53 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { jsx as _jsx } from "react/jsx-runtime";
11
2
  // Copyright (c) 2023 System Automation Corporation.
12
3
  // This file is licensed under the MIT License.
13
- import { HubConnectionBuilder, LogLevel } from '@microsoft/signalr/dist/esm/index.js';
14
- import { createContext, useContext, useEffect, useState } from 'react';
15
- import { useApiServices } from '../api/index.js';
4
+ import { createContext, useContext, useState } from 'react';
5
+ import { useNotification } from '../notification/index.js';
16
6
  export const SignalRConnectionContext = createContext({});
17
7
  SignalRConnectionContext.displayName = 'SignalRConnectionContext';
18
8
  function SignalRConnectionProvider({ children }) {
19
- const [instancesSignalRConnection, setInstancesSignalRConnection] = useState();
20
- const [documentsSignalRConnection, setDocumentsSignalRConnection] = useState();
21
- const api = useApiServices();
22
- useEffect(() => {
23
- const getConnectionInfo = (hubName) => {
24
- return api.post(`/signalr/hubs/${hubName}/negotiate`);
25
- };
26
- const getConnection = () => __awaiter(this, void 0, void 0, function* () {
27
- try {
28
- const instancesConnectionInfo = yield getConnectionInfo('instanceChanges');
29
- const documentsConnectionInfo = yield getConnectionInfo('documentChanges');
30
- if (instancesConnectionInfo) {
31
- const options = {
32
- accessTokenFactory: () => __awaiter(this, void 0, void 0, function* () {
33
- if (instancesConnectionInfo.accessToken) {
34
- return instancesConnectionInfo.accessToken;
35
- }
36
- else {
37
- return getConnection();
38
- }
39
- }),
40
- };
41
- const connection = new HubConnectionBuilder()
42
- .withUrl(instancesConnectionInfo.url, options)
43
- .configureLogging(LogLevel.Error)
44
- .withAutomaticReconnect()
45
- .build();
46
- setInstancesSignalRConnection(connection);
47
- }
48
- if (documentsConnectionInfo) {
49
- const options = {
50
- accessTokenFactory: () => __awaiter(this, void 0, void 0, function* () {
51
- if (documentsConnectionInfo.accessToken) {
52
- return documentsConnectionInfo.accessToken;
53
- }
54
- else {
55
- return getConnection();
56
- }
57
- }),
58
- };
59
- const connection = new HubConnectionBuilder()
60
- .withUrl(documentsConnectionInfo.url, options)
61
- .configureLogging(LogLevel.Error)
62
- .withAutomaticReconnect()
63
- .build();
64
- setDocumentsSignalRConnection(connection);
65
- }
66
- // eslint-disable-next-line no-empty
67
- }
68
- catch (err) { }
69
- });
70
- getConnection();
71
- }, []);
72
- useEffect(() => {
73
- let documentsConnectionStopped = false;
74
- const startConnection = (connection, numOfAttempts) => __awaiter(this, void 0, void 0, function* () {
75
- yield connection.start().catch((error) => {
76
- if (numOfAttempts < 4 && !documentsConnectionStopped) {
77
- setTimeout(() => {
78
- if (!documentsConnectionStopped) {
79
- startConnection(connection, numOfAttempts + 1);
80
- }
81
- }, 2000);
82
- }
83
- else {
84
- console.warn(`Cannot start connection to SignalR due to error "${error}"`);
85
- }
86
- });
87
- });
88
- if (documentsSignalRConnection) {
89
- startConnection(documentsSignalRConnection, 0);
90
- }
91
- return () => {
92
- documentsSignalRConnection === null || documentsSignalRConnection === void 0 ? void 0 : documentsSignalRConnection.stop();
93
- documentsConnectionStopped = true;
94
- };
95
- }, [documentsSignalRConnection]);
96
- useEffect(() => {
97
- let instancesConnectionStopped = false;
98
- const startConnection = (connection, numOfAttempts) => __awaiter(this, void 0, void 0, function* () {
99
- yield connection.start().catch((error) => {
100
- if (numOfAttempts < 4 && !instancesConnectionStopped) {
101
- setTimeout(() => {
102
- if (!instancesConnectionStopped) {
103
- startConnection(connection, numOfAttempts + 1);
104
- }
105
- }, 2000);
106
- }
107
- else {
108
- console.warn(`Cannot start connection to SignalR due to error "${error}"`);
109
- }
110
- });
111
- });
112
- if (instancesSignalRConnection) {
113
- startConnection(instancesSignalRConnection, 0);
114
- }
115
- return () => {
116
- instancesSignalRConnection === null || instancesSignalRConnection === void 0 ? void 0 : instancesSignalRConnection.stop();
117
- instancesConnectionStopped = true;
118
- };
119
- }, [instancesSignalRConnection]);
9
+ const notifications = useNotification();
10
+ const [instanceCallbacks] = useState(
11
+ // Map provided callbacks to our wrappers that are sent to the underlying
12
+ // notification provider.
13
+ new WeakMap());
120
14
  return (_jsx(SignalRConnectionContext.Provider, { value: {
121
- documentChanges: documentsSignalRConnection
122
- ? {
123
- subscribe: (topicName, callback) => documentsSignalRConnection.on(topicName, callback),
124
- unsubscribe: (topicName, callback) => callback
125
- ? documentsSignalRConnection.off(topicName, callback)
126
- : documentsSignalRConnection.off(topicName),
127
- }
128
- : undefined,
129
- instanceChanges: instancesSignalRConnection
130
- ? {
131
- subscribe: (topicName, callback) => instancesSignalRConnection.on(topicName, callback),
132
- unsubscribe: (topicName, callback) => callback
133
- ? instancesSignalRConnection.off(topicName, callback)
134
- : instancesSignalRConnection.off(topicName),
135
- }
136
- : undefined,
15
+ documentChanges: {
16
+ subscribe: (topicName, callback) => {
17
+ var _a;
18
+ const [objectId, instanceId] = topicName.split('/');
19
+ (_a = notifications.documentChanges) === null || _a === void 0 ? void 0 : _a.subscribe(objectId, instanceId, callback);
20
+ },
21
+ unsubscribe: (topicName, callback) => {
22
+ var _a;
23
+ const [objectId, instanceId] = topicName.split('/');
24
+ (_a = notifications.documentChanges) === null || _a === void 0 ? void 0 : _a.unsubscribe(objectId, instanceId, callback);
25
+ },
26
+ },
27
+ instanceChanges: {
28
+ subscribe: (objectId, callback) => {
29
+ var _a;
30
+ // If there is already a wrapper for the given callback, we must reuse the
31
+ // same one. Otherwise, if we overwrite the entry in our cache, we'll lose
32
+ // track of the original wrapper.
33
+ let wrapper = instanceCallbacks.get(callback);
34
+ if (!wrapper) {
35
+ wrapper = (...changes) => {
36
+ callback(...changes.map((change) => change.instanceId));
37
+ };
38
+ instanceCallbacks.set(callback, wrapper);
39
+ }
40
+ (_a = notifications.instanceChanges) === null || _a === void 0 ? void 0 : _a.subscribe(objectId, wrapper);
41
+ },
42
+ unsubscribe: (objectId, callback) => {
43
+ var _a;
44
+ (_a = notifications.instanceChanges) === null || _a === void 0 ? void 0 : _a.unsubscribe(objectId, callback && instanceCallbacks.get(callback));
45
+ },
46
+ },
137
47
  }, children: children }));
138
48
  }
139
49
  export function useSignalRConnection() {
50
+ console.warn('Use of useSignalRConnection is deprecated. Use useNotification instead.');
140
51
  return useContext(SignalRConnectionContext);
141
52
  }
142
53
  export default SignalRConnectionProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evoke-platform/context",
3
- "version": "1.0.0-dev.112",
3
+ "version": "1.0.0-dev.114",
4
4
  "description": "Utilities that provide context to Evoke platform widgets",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,20 +37,25 @@
37
37
  "devDependencies": {
38
38
  "@azure/msal-browser": "^2.38.4",
39
39
  "@azure/msal-react": "^1.5.9",
40
+ "@testing-library/dom": "^10.4.0",
41
+ "@testing-library/react": "^16.0.1",
40
42
  "@types/chai": "^4.3.11",
41
43
  "@types/dirty-chai": "^2.0.4",
42
44
  "@types/mocha": "^10.0.6",
43
45
  "@types/node": "^18.15.7",
44
46
  "@types/react": "^18.2.28",
45
- "@types/uuid": "^9.0.8",
46
47
  "@types/sinon": "^17.0.3",
48
+ "@types/uuid": "^9.0.8",
47
49
  "chai": "^4.4.1",
48
50
  "commit-and-tag-version": "^12.1.0",
49
51
  "dirty-chai": "^2.0.1",
50
52
  "eslint-plugin-react": "^7.33.2",
53
+ "global-jsdom": "^25.0.0",
54
+ "jsdom": "^25.0.1",
51
55
  "mocha": "^10.2.0",
52
56
  "msw": "^1.3.1",
53
57
  "react": "^18.2.0",
58
+ "react-dom": "^18.3.1",
54
59
  "react-router-dom": "^6.16.0",
55
60
  "sinon": "^18.0.0",
56
61
  "typescript": "^5.3.3"
@@ -63,7 +68,7 @@
63
68
  },
64
69
  "dependencies": {
65
70
  "@microsoft/signalr": "^7.0.12",
66
- "axios": "^1.6.7",
71
+ "axios": "^1.7.7",
67
72
  "uuid": "^9.0.1"
68
73
  }
69
74
  }