@pryv/monitor 2.4.6 → 3.0.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 +6 -3
- package/src/Monitor.js +17 -17
- package/src/UpdateMethod/EventsTimer.js +6 -1
- package/src/UpdateMethod/Socket.js +9 -2
- package/src/UpdateMethod/UpdateMethod.js +10 -8
- package/src/index.d.ts +58 -13
- package/src/lib/Changes.js +1 -1
- package/src/lib/updateStreams.js +1 -1
- package/test/load-helpers.js +0 -1
- package/test/monitor.test.js +1 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pryv/monitor",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Extends `pryv` with event-driven notifications for changes on a Pryv.io account",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Pryv",
|
|
@@ -15,10 +15,13 @@
|
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "git://github.com/pryv/lib-js"
|
|
18
|
+
"url": "git://github.com/pryv/lib-js.git"
|
|
19
19
|
},
|
|
20
20
|
"license": "BSD-3-Clause",
|
|
21
21
|
"author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
|
|
22
22
|
"main": "src/index.js",
|
|
23
|
-
"types": "src/index.d.ts"
|
|
23
|
+
"types": "src/index.d.ts",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.0.0"
|
|
26
|
+
}
|
|
24
27
|
}
|
package/src/Monitor.js
CHANGED
|
@@ -9,16 +9,20 @@ const _updateStreams = require('./lib/updateStreams');
|
|
|
9
9
|
const Changes = require('./lib/Changes');
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
+
* Monitor changes on a Pryv.io account.
|
|
13
|
+
* Emits events when data changes are detected.
|
|
12
14
|
* @memberof pryv
|
|
15
|
+
* @extends EventEmitter
|
|
13
16
|
*/
|
|
14
17
|
class Monitor extends EventEmitter {
|
|
15
18
|
/**
|
|
16
|
-
*
|
|
17
|
-
* @param {(
|
|
18
|
-
* @param {
|
|
19
|
+
* Create a new Monitor
|
|
20
|
+
* @param {(string|Connection)} apiEndpointOrConnection - API endpoint URL or Connection instance
|
|
21
|
+
* @param {MonitorScope} [eventsGetScope={}] - The scope to monitor (events.get parameters)
|
|
19
22
|
*/
|
|
20
23
|
constructor (apiEndpointOrConnection, eventsGetScope = {}) {
|
|
21
24
|
super();
|
|
25
|
+
// @ts-ignore - pryv is set at runtime by extendPryvMonitor
|
|
22
26
|
if (!Monitor.pryv) {
|
|
23
27
|
throw new Error('package \'@pryv/monitor\' must loaded after package \'pryv\'');
|
|
24
28
|
}
|
|
@@ -30,9 +34,11 @@ class Monitor extends EventEmitter {
|
|
|
30
34
|
};
|
|
31
35
|
Object.assign(this.eventsGetScope, eventsGetScope);
|
|
32
36
|
|
|
37
|
+
// @ts-ignore - pryv is set at runtime
|
|
33
38
|
if (apiEndpointOrConnection instanceof Monitor.pryv.Connection) {
|
|
34
39
|
this.connection = apiEndpointOrConnection;
|
|
35
40
|
} else {
|
|
41
|
+
// @ts-ignore - pryv is set at runtime
|
|
36
42
|
this.connection = new Monitor.pryv.Connection(apiEndpointOrConnection);
|
|
37
43
|
}
|
|
38
44
|
this.states = {
|
|
@@ -44,8 +50,8 @@ class Monitor extends EventEmitter {
|
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
/**
|
|
47
|
-
* Start the monitor
|
|
48
|
-
* @returns {Monitor} this
|
|
53
|
+
* Start the monitor and perform initial sync
|
|
54
|
+
* @returns {Promise<Monitor>} Promise resolving to this Monitor instance
|
|
49
55
|
*/
|
|
50
56
|
async start () {
|
|
51
57
|
if (this.states.started || this.states.starting) return this;
|
|
@@ -63,8 +69,8 @@ class Monitor extends EventEmitter {
|
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @returns {Monitor} this
|
|
72
|
+
* Request an events update according to the current scope
|
|
73
|
+
* @returns {Promise<Monitor>} Promise resolving to this Monitor instance
|
|
68
74
|
*/
|
|
69
75
|
async updateEvents () {
|
|
70
76
|
if (!this.states.started) {
|
|
@@ -95,8 +101,8 @@ class Monitor extends EventEmitter {
|
|
|
95
101
|
}
|
|
96
102
|
|
|
97
103
|
/**
|
|
98
|
-
*
|
|
99
|
-
* @returns {Monitor} this
|
|
104
|
+
* Request a streams update
|
|
105
|
+
* @returns {Promise<Monitor>} Promise resolving to this Monitor instance
|
|
100
106
|
*/
|
|
101
107
|
async updateStreams () {
|
|
102
108
|
if (!this.states.started) {
|
|
@@ -157,17 +163,11 @@ class Monitor extends EventEmitter {
|
|
|
157
163
|
return this.states.started;
|
|
158
164
|
}
|
|
159
165
|
|
|
160
|
-
/**
|
|
161
|
-
* Initialize the updateMethods with this Monitor
|
|
162
|
-
* @callback Monitor~UpdateMethod
|
|
163
|
-
* @param {Monitor} setMonitor
|
|
164
|
-
*/
|
|
165
|
-
|
|
166
166
|
/**
|
|
167
167
|
* @private
|
|
168
|
-
* Called
|
|
168
|
+
* Called by UpdateMethod to share cross references
|
|
169
169
|
* Set a custom update method
|
|
170
|
-
* @param {
|
|
170
|
+
* @param {Object} updateMethod - the auto-update method
|
|
171
171
|
* @returns {Monitor} this
|
|
172
172
|
*/
|
|
173
173
|
addUpdateMethod (updateMethod) {
|
|
@@ -4,9 +4,14 @@
|
|
|
4
4
|
*/
|
|
5
5
|
const UpdateMethod = require('./UpdateMethod');
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Update method that polls for event changes at a fixed interval.
|
|
9
|
+
* @memberof pryv.Monitor
|
|
10
|
+
* @extends UpdateMethod
|
|
11
|
+
*/
|
|
7
12
|
class EventsTimer extends UpdateMethod {
|
|
8
13
|
/**
|
|
9
|
-
* @param {
|
|
14
|
+
* @param {number} updateRateMS - The refresh rate in milliseconds (must be > 1)
|
|
10
15
|
*/
|
|
11
16
|
constructor (updateRateMS) {
|
|
12
17
|
super();
|
|
@@ -6,17 +6,24 @@
|
|
|
6
6
|
const UpdateMethod = require('./UpdateMethod');
|
|
7
7
|
const Changes = require('../lib/Changes');
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Update method that uses @pryv/socket.io events for real-time updates.
|
|
11
|
+
* Requires @pryv/socket.io to be loaded.
|
|
12
|
+
* @memberof pryv.Monitor
|
|
13
|
+
* @extends UpdateMethod
|
|
14
|
+
*/
|
|
9
15
|
class Socket extends UpdateMethod {
|
|
10
16
|
async ready () {
|
|
11
17
|
if (this.socket) return;
|
|
18
|
+
// @ts-ignore - socket is added by @pryv/socket.io extension
|
|
12
19
|
if (!this.monitor.connection.socket) {
|
|
13
20
|
throw new Error('You should load package @pryv/socket.io to use monitor with websockets');
|
|
14
21
|
}
|
|
22
|
+
// @ts-ignore - socket is added by @pryv/socket.io extension
|
|
15
23
|
this.socket = await this.monitor.connection.socket.open();
|
|
16
24
|
this.socket.on('eventsChanged', () => { this.monitor.updateEvents(); });
|
|
17
25
|
this.socket.on('streamsChanged', () => { this.monitor.updateStreams(); });
|
|
18
|
-
|
|
19
|
-
this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR.error); });
|
|
26
|
+
this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR, error); });
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
async stop () {
|
|
@@ -4,22 +4,24 @@
|
|
|
4
4
|
*/
|
|
5
5
|
const Changes = require('../lib/Changes');
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Base class for update methods used by Monitor.
|
|
8
|
+
* Subclass this to create custom update strategies.
|
|
8
9
|
* @memberof pryv.Monitor
|
|
9
|
-
* @constructor {Monitor~UpdateMethod} updateMethod.setMonitor - set only once
|
|
10
10
|
*/
|
|
11
11
|
class UpdateMethod {
|
|
12
12
|
/**
|
|
13
13
|
* Assign a Monitor to this updater.
|
|
14
14
|
* Usually called by the monitor itself on monitor.addUpdateMethod()
|
|
15
|
-
* @param {Monitor} monitor
|
|
15
|
+
* @param {Monitor} monitor - The monitor to attach to
|
|
16
16
|
*/
|
|
17
17
|
setMonitor (monitor) {
|
|
18
18
|
if (this.monitor) {
|
|
19
19
|
throw new Error('An update Method can be assigned to one monitor only');
|
|
20
20
|
}
|
|
21
21
|
this.monitor = monitor;
|
|
22
|
+
// @ts-ignore - Changes.READY and Changes.STOP are valid event names
|
|
22
23
|
monitor.on(Changes.READY, this.ready.bind(this));
|
|
24
|
+
// @ts-ignore
|
|
23
25
|
monitor.on(Changes.STOP, this.stop.bind(this));
|
|
24
26
|
if (monitor.started) {
|
|
25
27
|
this.ready();
|
|
@@ -27,15 +29,15 @@ class UpdateMethod {
|
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
32
|
+
* Called when all update tasks are done and monitor is ready for next update.
|
|
33
|
+
* Override in subclasses to implement custom behavior.
|
|
34
|
+
* @returns {Promise<void>}
|
|
33
35
|
*/
|
|
34
36
|
async ready () { }
|
|
35
37
|
|
|
36
38
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
+
* Called when monitor is stopped. Override to clean up resources.
|
|
40
|
+
* @returns {Promise<void>}
|
|
39
41
|
*/
|
|
40
42
|
async stop () { }
|
|
41
43
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,28 +1,40 @@
|
|
|
1
|
-
import pryv from 'pryv';
|
|
1
|
+
import pryv, { Service, Connection, Auth, Browser, utils } from 'pryv';
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Type for the pryv library parameter - accepts both default and namespace imports
|
|
6
|
+
*/
|
|
7
|
+
export type PryvLibrary = {
|
|
8
|
+
Service: typeof Service;
|
|
9
|
+
Connection: typeof Connection;
|
|
10
|
+
Auth: typeof Auth;
|
|
11
|
+
Browser: typeof Browser;
|
|
12
|
+
utils: typeof utils;
|
|
13
|
+
version?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
4
16
|
/**
|
|
5
17
|
* Extends a `pryv` instance with monitoring capabilities.
|
|
6
18
|
*
|
|
7
19
|
* Typical usage:
|
|
8
20
|
* ```ts
|
|
9
21
|
* import pryv from 'pryv';
|
|
10
|
-
* import
|
|
22
|
+
* import extendPryvMonitor from '@pryv/monitor';
|
|
11
23
|
*
|
|
12
24
|
* extendPryvMonitor(pryv);
|
|
13
25
|
*
|
|
14
|
-
* const
|
|
26
|
+
* const mon = new pryv.Monitor('https://token@user.pryv.me', {
|
|
15
27
|
* streams: ['data'],
|
|
16
28
|
* });
|
|
17
29
|
*
|
|
18
|
-
*
|
|
30
|
+
* mon.on(pryv.Monitor.Changes.EVENT, (event) => {
|
|
19
31
|
* // handle new or changed event
|
|
20
32
|
* });
|
|
21
33
|
*
|
|
22
|
-
* await
|
|
34
|
+
* await mon.start();
|
|
23
35
|
* ```
|
|
24
36
|
*/
|
|
25
|
-
export default function extendPryvMonitor(pryvLib:
|
|
37
|
+
export default function extendPryvMonitor(pryvLib: PryvLibrary): typeof pryv.Monitor;
|
|
26
38
|
|
|
27
39
|
declare module 'pryv' {
|
|
28
40
|
|
|
@@ -60,6 +72,9 @@ declare module 'pryv' {
|
|
|
60
72
|
export class Monitor extends EventEmitter {
|
|
61
73
|
constructor(apiEndpointOrConnection: APIEndpoint | Connection, eventsGetScope?: MonitorScope);
|
|
62
74
|
|
|
75
|
+
/** The connection used by this monitor */
|
|
76
|
+
readonly connection: Connection;
|
|
77
|
+
|
|
63
78
|
/**
|
|
64
79
|
* Start the monitor and perform initial sync.
|
|
65
80
|
* Resolves with the same monitor instance.
|
|
@@ -93,6 +108,7 @@ declare module 'pryv' {
|
|
|
93
108
|
|
|
94
109
|
/**
|
|
95
110
|
* Attach an auto-update method implementation.
|
|
111
|
+
* @param updateMethod - The update method to use (e.g., EventsTimer, Socket)
|
|
96
112
|
*/
|
|
97
113
|
addUpdateMethod(updateMethod: MonitorUpdateMethod): Monitor;
|
|
98
114
|
|
|
@@ -101,19 +117,18 @@ declare module 'pryv' {
|
|
|
101
117
|
*
|
|
102
118
|
* Events:
|
|
103
119
|
* - `event` – new or updated event
|
|
104
|
-
* - `eventDelete` – deleted event
|
|
120
|
+
* - `eventDelete` – deleted event (with id property)
|
|
105
121
|
* - `streams` – updated streams list
|
|
106
122
|
* - `error` – error object
|
|
107
123
|
* - `ready` – monitor is ready for next update
|
|
108
124
|
* - `stop` – monitor has been stopped
|
|
109
125
|
*/
|
|
110
126
|
on(event: 'event', listener: (event: Event) => void): this;
|
|
111
|
-
on(event: 'eventDelete', listener: (
|
|
127
|
+
on(event: 'eventDelete', listener: (deletion: ItemDeletion) => void): this;
|
|
112
128
|
on(event: 'streams', listener: (streams: Stream[]) => void): this;
|
|
113
|
-
on(event: 'error', listener: (error:
|
|
129
|
+
on(event: 'error', listener: (error: Error | unknown) => void): this;
|
|
114
130
|
on(event: 'ready', listener: () => void): this;
|
|
115
131
|
on(event: 'stop', listener: () => void): this;
|
|
116
|
-
on(event: string, listener: (...args: any[]) => void): this;
|
|
117
132
|
|
|
118
133
|
/**
|
|
119
134
|
* Static access to available update methods.
|
|
@@ -128,15 +143,35 @@ declare module 'pryv' {
|
|
|
128
143
|
* Static enum of change names.
|
|
129
144
|
*/
|
|
130
145
|
static Changes: typeof MonitorChanges;
|
|
146
|
+
|
|
147
|
+
/** Reference to pryv library (set during extension) */
|
|
148
|
+
static pryv: typeof pryv;
|
|
131
149
|
}
|
|
132
150
|
|
|
133
151
|
/**
|
|
134
|
-
* Base
|
|
152
|
+
* Base class for update methods used by Monitor.
|
|
153
|
+
* Subclass this to create custom update strategies.
|
|
135
154
|
*/
|
|
136
155
|
export class MonitorUpdateMethod {
|
|
156
|
+
/** The monitor this update method is attached to */
|
|
137
157
|
protected monitor?: Monitor;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Assign a Monitor to this updater.
|
|
161
|
+
* Usually called by the monitor itself on monitor.addUpdateMethod()
|
|
162
|
+
* @param monitor - The monitor to attach to
|
|
163
|
+
*/
|
|
138
164
|
setMonitor(monitor: Monitor): void;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Called when all update tasks are done and monitor is ready for next update.
|
|
168
|
+
* Override in subclasses to implement custom behavior.
|
|
169
|
+
*/
|
|
139
170
|
ready(): Promise<void>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Called when monitor is stopped. Override to clean up resources.
|
|
174
|
+
*/
|
|
140
175
|
stop(): Promise<void>;
|
|
141
176
|
}
|
|
142
177
|
|
|
@@ -144,13 +179,23 @@ declare module 'pryv' {
|
|
|
144
179
|
* Update method that polls for event changes at a fixed interval.
|
|
145
180
|
*/
|
|
146
181
|
export class EventsTimerUpdateMethod extends MonitorUpdateMethod {
|
|
182
|
+
/**
|
|
183
|
+
* @param updateRateMS - The refresh rate in milliseconds (must be > 1)
|
|
184
|
+
*/
|
|
147
185
|
constructor(updateRateMS: number);
|
|
186
|
+
|
|
187
|
+
/** The configured update rate in milliseconds */
|
|
188
|
+
readonly updateRateMS: number;
|
|
148
189
|
}
|
|
149
190
|
|
|
150
191
|
/**
|
|
151
|
-
* Update method that uses @pryv/socket.io events.
|
|
192
|
+
* Update method that uses @pryv/socket.io events for real-time updates.
|
|
193
|
+
* Requires @pryv/socket.io to be loaded.
|
|
152
194
|
*/
|
|
153
|
-
export class SocketUpdateMethod extends MonitorUpdateMethod {
|
|
195
|
+
export class SocketUpdateMethod extends MonitorUpdateMethod {
|
|
196
|
+
/** The socket instance (set after ready() is called) */
|
|
197
|
+
protected socket?: SocketIO;
|
|
198
|
+
}
|
|
154
199
|
}
|
|
155
200
|
|
|
156
201
|
|
package/src/lib/Changes.js
CHANGED
package/src/lib/updateStreams.js
CHANGED
|
@@ -7,7 +7,7 @@ const Changes = require('./Changes');
|
|
|
7
7
|
module.exports = async function _updateStreams (monitor) {
|
|
8
8
|
try {
|
|
9
9
|
const result = await monitor.connection.get('streams');
|
|
10
|
-
if (!result.streams) { throw new Error('Invalid response ' + JSON.
|
|
10
|
+
if (!result.streams) { throw new Error('Invalid response ' + JSON.stringify(result)); }
|
|
11
11
|
monitor.emit(Changes.STREAMS, result.streams);
|
|
12
12
|
} catch (e) {
|
|
13
13
|
monitor.emit(Changes.ERROR, e);
|
package/test/load-helpers.js
CHANGED
package/test/monitor.test.js
CHANGED
|
@@ -28,8 +28,7 @@ describe('Monitor', function () {
|
|
|
28
28
|
it('throw Error on invalid apiEndpoint', async () => {
|
|
29
29
|
let passed = true;
|
|
30
30
|
try {
|
|
31
|
-
|
|
32
|
-
const monitor = new pryv.Monitor('BlipBlop', { limit: 1 });
|
|
31
|
+
pryv.Monitor('BlipBlop', { limit: 1 });
|
|
33
32
|
passed = false;
|
|
34
33
|
} catch (e) {
|
|
35
34
|
|