@pryv/monitor 3.6.0 → 3.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pryv/monitor",
3
- "version": "3.6.0",
3
+ "version": "3.7.0",
4
4
  "description": "Extends `pryv` with event-driven notifications for changes on a Pryv.io account",
5
5
  "keywords": [
6
6
  "Pryv",
package/src/Monitor.js CHANGED
@@ -179,7 +179,7 @@ class Monitor extends EventEmitter {
179
179
  * `connection.accessInfo` cache when this event fires, so the next
180
180
  * `connection.accessInfo()` call returns the freshly-fetched copy.
181
181
  *
182
- * @param {Function} handler - called with `(payload)` on each event
182
+ * @param {(payload: any) => void} handler - called with `(payload)` on each event
183
183
  * @returns {Monitor} this (chainable)
184
184
  */
185
185
  onAccessUpdated (handler) {
@@ -21,8 +21,38 @@ class Socket extends UpdateMethod {
21
21
  }
22
22
  // @ts-ignore - socket is added by @pryv/socket.io extension
23
23
  this.socket = await this.monitor.connection.socket.open();
24
- this.socket.on('eventsChanged', () => { this.monitor.updateEvents(); });
25
- this.socket.on('streamsChanged', () => { this.monitor.updateStreams(); });
24
+
25
+ // Scoped notifications (opt-in): register this monitor's scope so the server
26
+ // wakes us only when a matching change occurs, delivered as a single
27
+ // `notificationsChanged({ keys })`. Because a scoped connection opts out of
28
+ // the coarse broadcasts server-side, we register BOTH an events scope (so
29
+ // `updateEvents` fires) and a streams scope (so `updateStreams` fires).
30
+ // Older servers reject `subscribe` (the wildcard treats it as an unknown
31
+ // method); we then fall back to the legacy coarse signals.
32
+ const onEvents = () => { this.monitor.updateEvents(); };
33
+ const onStreams = () => { this.monitor.updateStreams(); };
34
+ this.socket.on('notificationsChanged', (payload) => {
35
+ const keys = (payload && payload.keys) || [];
36
+ if (keys.includes('monEvents')) onEvents();
37
+ if (keys.includes('monStreams')) onStreams();
38
+ });
39
+ const scope = this.monitor.eventsGetScope || {};
40
+ const eventsQuery = {};
41
+ for (const f of ['streams', 'types', 'content', 'clientData']) {
42
+ if (scope[f] != null) eventsQuery[f] = scope[f];
43
+ }
44
+ const streamsQuery = scope.streams != null ? { streams: scope.streams } : {};
45
+ const ack = await this.socket.subscribe({
46
+ scopes: {
47
+ monEvents: { kind: 'events', query: eventsQuery },
48
+ monStreams: { kind: 'streams', query: streamsQuery }
49
+ }
50
+ });
51
+ if (!ack || ack.ok !== true) {
52
+ // Server does not support scoped subscriptions — use coarse signals.
53
+ this.socket.on('eventsChanged', onEvents);
54
+ this.socket.on('streamsChanged', onStreams);
55
+ }
26
56
  // Re-emit the server's fine-grained `accessUpdated` event on the
27
57
  // Monitor itself so consumers can subscribe via
28
58
  // `monitor.onAccessUpdated(handler)`. The underlying SocketIO has