@pryv/monitor 3.0.2 → 3.1.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.0.2",
3
+ "version": "3.1.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
@@ -163,6 +163,30 @@ class Monitor extends EventEmitter {
163
163
  return this.states.started;
164
164
  }
165
165
 
166
+ /**
167
+ * Plan 66: register a handler for the server's fine-grained
168
+ * `accessUpdated` event. Fires whenever a Pryv.io ≥ 2.0.0-pre.X server
169
+ * emits the event after a successful `accesses.update` — payload is
170
+ * `{ type: 'access-updated', accessId: '<base>:<serial>', serial }`.
171
+ *
172
+ * Requires `Monitor.UpdateMethod.Socket` to be active (the event
173
+ * is server-pushed via `@pryv/socket.io`). With other update methods
174
+ * (e.g. `EventsTimer` polling), the handler will never fire —
175
+ * polling-based consumers should call `connection.accessInfo(true)`
176
+ * themselves when they need to refresh the cache.
177
+ *
178
+ * The underlying `connection.socket` already busts the
179
+ * `connection.accessInfo` cache when this event fires, so the next
180
+ * `connection.accessInfo()` call returns the freshly-fetched copy.
181
+ *
182
+ * @param {Function} handler - called with `(payload)` on each event
183
+ * @returns {Monitor} this (chainable)
184
+ */
185
+ onAccessUpdated (handler) {
186
+ this.on('accessUpdated', handler);
187
+ return this;
188
+ }
189
+
166
190
  /**
167
191
  * @private
168
192
  * Called by UpdateMethod to share cross references
@@ -23,7 +23,25 @@ class Socket extends UpdateMethod {
23
23
  this.socket = await this.monitor.connection.socket.open();
24
24
  this.socket.on('eventsChanged', () => { this.monitor.updateEvents(); });
25
25
  this.socket.on('streamsChanged', () => { this.monitor.updateStreams(); });
26
- this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR, error); });
26
+ // Plan 66: re-emit the server's fine-grained `accessUpdated` event
27
+ // on the Monitor itself so consumers can subscribe via
28
+ // `monitor.onAccessUpdated(handler)`. The underlying SocketIO has
29
+ // already busted the connection's accessInfo cache; the payload
30
+ // carries the new composite accessId + serial for fine-grained
31
+ // reactions.
32
+ this.socket.on('accessUpdated', (payload) => {
33
+ this.monitor.emit('accessUpdated', payload);
34
+ });
35
+ this.socket.on('error', (error) => {
36
+ this.monitor.emit(Changes.ERROR, error);
37
+ // If the underlying socket.io-client transport has been torn down
38
+ // (i.e. SocketIO emitted 'error' after reconnect_failed), drop our
39
+ // reference so a future Changes.READY can rebuild instead of
40
+ // short-circuiting on the cached, dead handle.
41
+ if (this.socket && !this.socket._io) {
42
+ this.socket = null;
43
+ }
44
+ });
27
45
  }
28
46
 
29
47
  async stop () {
package/src/index.d.ts CHANGED
@@ -96,6 +96,14 @@ declare module 'pryv' {
96
96
  */
97
97
  stop(): Monitor;
98
98
 
99
+ /**
100
+ * Plan 66: register a handler for the server's fine-grained
101
+ * `accessUpdated` event. Requires `Monitor.UpdateMethod.Socket`.
102
+ * The underlying `connection.socket` busts the
103
+ * `connection.accessInfo` cache before this fires.
104
+ */
105
+ onAccessUpdated(handler: (payload: { type: 'access-updated', accessId: string, serial: number }) => void): Monitor;
106
+
99
107
  /**
100
108
  * True when the monitor has been started and not yet stopped.
101
109
  */