@pryv/monitor 1.0.5 → 2.3.1
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 +103 -133
- package/package.json +12 -23
- package/src/Monitor.js +28 -28
- package/src/UpdateMethod/EventsTimer.js +8 -4
- package/src/UpdateMethod/Socket.js +10 -9
- package/src/UpdateMethod/UpdateMethod.js +11 -7
- package/src/UpdateMethod/index.js +5 -2
- package/src/browser-index.js +10 -8
- package/src/index.js +15 -12
- package/src/lib/Changes.js +8 -4
- package/src/lib/updateEvents.js +9 -5
- package/src/lib/updateStreams.js +7 -3
- package/test/load-helpers.js +26 -0
- package/test/monitor.test.js +22 -22
- package/test/socket.test.js +24 -22
- package/test/timer.test.js +23 -25
- package/.mocharc.js +0 -11
- package/LICENSE.md +0 -27
- package/examples/index.html +0 -231
- package/examples/screenshot.png +0 -0
- package/examples/simplemonitor.js +0 -32
- package/scripts/setup-environment-dev.sh +0 -28
- package/test/helpers.js +0 -30
- package/webpack.config.js +0 -37
package/README.md
CHANGED
|
@@ -1,242 +1,212 @@
|
|
|
1
|
-
# Monitor add-on for
|
|
1
|
+
# Monitor add-on for `pryv`
|
|
2
2
|
|
|
3
|
-
Extends Pryv
|
|
3
|
+
Extends the [Pryv JavaScript library](https://github.com/pryv/lib-js) with event-driven notifications of changes on a Pryv.io account.
|
|
4
4
|
|
|
5
|
-
## Setup
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
### Browser
|
|
10
|
-
|
|
11
|
-
Note: `pryv-monitor.js` must be loaded **after** `pryv.js`
|
|
12
|
-
|
|
13
|
-
```html
|
|
14
|
-
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
15
|
-
<script src="https://api.pryv.com/lib-js-monitor/pryv-monitor.js"></script>
|
|
16
|
-
```
|
|
6
|
+
## Usage
|
|
17
7
|
|
|
18
|
-
|
|
8
|
+
The add-on extends `pryv` with a `pryv.Monitor` class.
|
|
19
9
|
|
|
20
|
-
- ES6: `https://api.pryv.com/lib-js-monitor/pryv-monitor-es6.js`
|
|
21
|
-
- Socket.io + Monitor + Lib-js: `https://api.pryv.com/lib-js/pryv-socket.io-monitor.js`.
|
|
22
10
|
|
|
23
|
-
###
|
|
11
|
+
### Importing
|
|
24
12
|
|
|
25
|
-
|
|
26
|
-
npm install pryv @pryv/monitor
|
|
27
|
-
```
|
|
13
|
+
#### NPM
|
|
28
14
|
|
|
29
|
-
|
|
15
|
+
`npm install --save pryv @pryv/monitor`, then in your code (the add-on must be loaded **once only**):
|
|
30
16
|
|
|
31
|
-
```
|
|
32
|
-
const
|
|
33
|
-
require('@pryv/monitor')(
|
|
17
|
+
```js
|
|
18
|
+
const pryv = require('pryv');
|
|
19
|
+
require('@pryv/monitor')(pryv);
|
|
34
20
|
```
|
|
35
21
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
Once Monitor has been set up, `Pryv.Monitor` can be instantiated.
|
|
22
|
+
#### `<script>` tag
|
|
39
23
|
|
|
40
|
-
|
|
24
|
+
`pryv-monitor.js` must be loaded **after** `pryv.js`:
|
|
41
25
|
|
|
42
|
-
```
|
|
43
|
-
|
|
26
|
+
```html
|
|
27
|
+
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
28
|
+
<script src="https://api.pryv.com/lib-js/pryv-monitor.js"></script>
|
|
44
29
|
```
|
|
45
30
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Pryv.Connection: [lib-js Connection](https://github.com/pryv/lib-js#obtaining-a-pryvconnection)
|
|
31
|
+
Other distributions available:
|
|
49
32
|
|
|
50
|
-
-
|
|
33
|
+
- ES6 version: `https://api.pryv.com/lib-js-monitor/pryv-monitor-es6.js`
|
|
34
|
+
- `pryv` library bundled with Socket.IO and Monitor add-ons: `https://api.pryv.com/lib-js/pryv-socket.io-monitor.js`.
|
|
51
35
|
|
|
52
|
-
### Event listeners
|
|
53
36
|
|
|
54
|
-
|
|
37
|
+
### Using `pryv.Monitor`
|
|
55
38
|
|
|
56
|
-
|
|
39
|
+
Once the add-on is loaded, `pryv.Monitor` can be instantiated.
|
|
57
40
|
|
|
58
|
-
|
|
41
|
+
#### Constructor
|
|
59
42
|
|
|
60
|
-
```
|
|
61
|
-
|
|
43
|
+
```js
|
|
44
|
+
new pryv.Monitor({apiEndpoint | connection}, eventsGetScope)
|
|
62
45
|
```
|
|
63
46
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
- `
|
|
67
|
-
|
|
68
|
-
callback argument: the Pryv event
|
|
69
|
-
|
|
70
|
-
- `eventDeleted`: on Pryv event deletion
|
|
71
|
-
|
|
72
|
-
callback argument: `{id: "...."}` the id of the deleted Pryv event
|
|
47
|
+
- `apiEndpoint`: an API endpoint URL (see [app guidelines](https://api.pryv.com/guides/app-guidelines/#auto-configuration))
|
|
48
|
+
- `connection`: a `pryv.Connection` instance (see [library README](https://github.com/pryv/lib-js#obtaining-a-pryvconnection))
|
|
49
|
+
- `eventsGetScope`: `event.get` parameters (see [API reference](https://api.pryv.com/reference/#get-events))
|
|
73
50
|
|
|
74
|
-
|
|
51
|
+
#### Event listeners
|
|
75
52
|
|
|
76
|
-
|
|
53
|
+
Register event listeners for data changes on the specified scope. `pryv.Monitor` extends [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) so you can register listeners with:
|
|
77
54
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
55
|
+
```js
|
|
56
|
+
monitor.on(event, callback)
|
|
57
|
+
```
|
|
81
58
|
|
|
82
|
-
|
|
59
|
+
The possible events are:
|
|
83
60
|
|
|
84
|
-
- `
|
|
61
|
+
- `event`: on Pryv event **creation** and **update**; callback argument: the Pryv event
|
|
62
|
+
- `eventDeleted`: on Pryv event deletion; callback argument: an object with the `id` of the deleted Pryv event (e.g. `{id: "...."}`)
|
|
63
|
+
- `streams`: on any change (creation, update, deletion) in the Pryv stream structure; callback argument: a [`streams.get`](https://api.pryv.com/reference/#get-streams) result object (e.g. `{streams: ...}`)
|
|
64
|
+
- `error`: on error; callback argument: the error or error message
|
|
65
|
+
- `ready`: emitted when the monitor is ready (for internal and `UpdateMethod` usage – see below)
|
|
66
|
+
- `stop`: when the monitor stops
|
|
85
67
|
|
|
86
68
|
#### Start monitoring
|
|
87
69
|
|
|
88
|
-
```
|
|
70
|
+
```js
|
|
89
71
|
await monitor.start()
|
|
90
72
|
```
|
|
91
73
|
|
|
92
|
-
|
|
74
|
+
The monitor will fetch the entire dataset in the specified scope then trigger the change events accordingly.
|
|
93
75
|
|
|
94
76
|
#### Trigger Pryv events update
|
|
95
77
|
|
|
96
|
-
```
|
|
78
|
+
```js
|
|
97
79
|
await monitor.updateEvents()
|
|
98
80
|
```
|
|
99
81
|
|
|
100
|
-
This will push a request to update Pryv events into the task stack. It will be executed as soon as the monitor has finished
|
|
82
|
+
This will push a request to update Pryv events into the task stack. It will be executed as soon as the monitor has finished possible pending tasks.
|
|
101
83
|
|
|
102
84
|
#### Trigger streams update
|
|
103
85
|
|
|
104
|
-
```
|
|
86
|
+
```js
|
|
105
87
|
await monitor.updateStreams()
|
|
106
88
|
```
|
|
107
89
|
|
|
108
|
-
This will push a request to update Pryv streams into task
|
|
90
|
+
This will push a request to update Pryv streams into the task stack. It will be executed as soon as the monitor has finished possible pending tasks.
|
|
109
91
|
|
|
110
92
|
#### Add auto-update method
|
|
111
93
|
|
|
112
|
-
```
|
|
113
|
-
monitor.addUpdateMethod(
|
|
94
|
+
```js
|
|
95
|
+
monitor.addUpdateMethod(updateMethod)
|
|
114
96
|
```
|
|
115
97
|
|
|
116
98
|
Update methods can be triggered automatically with:
|
|
117
99
|
|
|
118
|
-
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
new Pryv.Monitor.UpdateMethod.Socket()
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
Based on websockets, it uses [lib-js-socket.io](https://github.com/pryv/lib-js-socket.io) to relay notification from Pryv.io to the monitor.
|
|
133
|
-
|
|
134
|
-
- **Custom**
|
|
135
|
-
|
|
136
|
-
You can design your own UpdateMethod by extending the [UpdateMethod](https://github.com/pryv/lib-js-monitor/blob/master/src/UpdateMethod/UpdateMethod.js) class.
|
|
100
|
+
- `EventsTimer`
|
|
101
|
+
```js
|
|
102
|
+
new pryv.Monitor.UpdateMethod.EventsTimer(ms)
|
|
103
|
+
```
|
|
104
|
+
This will call `monitor.updateEvents()` every `ms` milliseconds (intended for demonstrative purposes).
|
|
105
|
+
- `Socket`
|
|
106
|
+
```js
|
|
107
|
+
new pryv.Monitor.UpdateMethod.Socket()
|
|
108
|
+
```
|
|
109
|
+
This uses the [Socket.IO add-on](https://github.com/pryv/lib-js/tree/master/components/pryv-socket.io#readme) to relay notifications from Pryv.io to the monitor.
|
|
110
|
+
- **Custom**: You can design your own update method by extending the [`UpdateMethod`](https://github.com/pryv/lib-js/tree/master/components/pryv-monitor/src/UpdateMethod/UpdateMethod.js) class.
|
|
137
111
|
|
|
138
112
|
#### Stop monitoring
|
|
139
113
|
|
|
140
|
-
```
|
|
114
|
+
```js
|
|
141
115
|
monitor.stop()
|
|
142
116
|
```
|
|
143
117
|
|
|
144
|
-
The monitor will stop auto updaters and will throw errors if `updateEvents` or `updateStreams`
|
|
118
|
+
The monitor will stop auto updaters and will throw errors if `updateEvents()` or `updateStreams()` are called.
|
|
119
|
+
It can be restarted.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
### Known limitation
|
|
145
123
|
|
|
146
|
-
|
|
124
|
+
If an event's update moves it out of the monitor's scope – for example the event's `streamIds` property is updated to a stream not covered by the scope – the Pryv.io API does not currently provide the necessary synchronization mechanism to detect such a change.
|
|
147
125
|
|
|
148
|
-
## Example
|
|
149
126
|
|
|
127
|
+
### Examples
|
|
150
128
|
|
|
151
|
-
```
|
|
129
|
+
```js
|
|
152
130
|
const apiEndpoint = 'https://ck6bwmcar00041ep87c8ujf90@drtom.pryv.me';
|
|
153
131
|
|
|
154
|
-
//
|
|
132
|
+
// set monitor scope to the stream 'diary'
|
|
155
133
|
const eventsGetScope = {'streamIds': [diary]};
|
|
156
134
|
|
|
157
|
-
// refresh the monitor using the 'timer' method with a
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
.addUpdateMethod(new Pryv.Monitor.UpdateMethod.EventsTimer(1000)); // add refresh timer
|
|
135
|
+
// refresh the monitor using the 'timer' method with a refresh rate of 1 second
|
|
136
|
+
const monitor = new pryv.Monitor(apiEndpoint || connection, eventsGetScope)
|
|
137
|
+
.on('event', (event) => {}) // event created or updated
|
|
138
|
+
.on('streams', (streams) => {}) // streams structure changed
|
|
139
|
+
.on('eventDelete', (event) => {}) // event deleted
|
|
140
|
+
.addUpdateMethod(new pryv.Monitor.UpdateMethod.EventsTimer(1000)); // set refresh timer
|
|
164
141
|
|
|
142
|
+
// start the monitor
|
|
165
143
|
(async () => {
|
|
166
|
-
await monitor.start();
|
|
144
|
+
await monitor.start();
|
|
167
145
|
}())
|
|
168
146
|
```
|
|
169
147
|
|
|
170
|
-
|
|
148
|
+
You can also chain `start()`:
|
|
171
149
|
|
|
172
|
-
```
|
|
150
|
+
```js
|
|
173
151
|
(async () => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
152
|
+
const monitor = await (new pryv.Monitor(apiEndpoint || connection, eventsGetScope)
|
|
153
|
+
.on('event', (event) => {}))
|
|
154
|
+
.start();
|
|
177
155
|
})();
|
|
178
156
|
```
|
|
179
157
|
|
|
180
|
-
|
|
158
|
+
#### Auto-update with web sockets
|
|
181
159
|
|
|
182
|
-
|
|
160
|
+
##### Node.js
|
|
183
161
|
|
|
184
162
|
Install packages: `npm install pryv @pryv/socket.io @pryv/monitor`
|
|
185
163
|
|
|
186
|
-
```
|
|
187
|
-
const
|
|
188
|
-
require('@pryv/socket.io')(
|
|
189
|
-
require('@pryv/monitor')(
|
|
164
|
+
```js
|
|
165
|
+
const pryv = require('pryv');
|
|
166
|
+
require('@pryv/socket.io')(pryv);
|
|
167
|
+
require('@pryv/monitor')(pryv);
|
|
190
168
|
|
|
191
169
|
const apiEndpoint = 'https://ck60yn9yv00011hd3vu1ocpi7@jslibtest.pryv.me';
|
|
192
170
|
(async () => {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
171
|
+
const monitor = await (new pryv.Monitor(apiEndpoint || connection, eventsGetScope)
|
|
172
|
+
.on('event', (event) => {}))
|
|
173
|
+
.addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket())
|
|
174
|
+
.start();
|
|
197
175
|
})();
|
|
198
176
|
```
|
|
199
177
|
|
|
200
|
-
|
|
178
|
+
##### Browser
|
|
201
179
|
|
|
202
180
|
```html
|
|
203
181
|
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
204
182
|
<script src="https://api.pryv.com/lib-js-socket.io/pryv-socket.io.js"></script>
|
|
205
183
|
<script src="https://api.pryv.com/lib-js-monitor/pryv-monitor.js"></script>
|
|
206
|
-
<!--
|
|
207
|
-
|
|
184
|
+
<!--
|
|
185
|
+
The previous lines can be replaced by the bundled version of the library:
|
|
208
186
|
<script src="https://api.pryv.com/lib-js/pryv-socket.io-monitor.js"></script>
|
|
209
187
|
-->
|
|
210
188
|
|
|
211
189
|
<script>
|
|
212
190
|
const apiEndpoint = 'https://ck60yn9yv00011hd3vu1ocpi7@jslibtest.pryv.me';
|
|
213
191
|
(async () => {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
192
|
+
const monitor = await (new pryv.Monitor(apiEndpoint || connection, eventsGetScope)
|
|
193
|
+
.on('event', (event) => {}))
|
|
194
|
+
.addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket())
|
|
195
|
+
.start();
|
|
218
196
|
})();
|
|
219
197
|
</script>
|
|
220
198
|
```
|
|
221
199
|
|
|
200
|
+
#### Example web app
|
|
222
201
|
|
|
202
|
+
See [here](../../examples/monitor.html) for a simple app that allows to sign in to a Pryv.io platform, register to monitor events changes and create notes. You can try it running [there](https://api.pryv.com/lib-js/examples/monitor.html).
|
|
223
203
|
|
|
224
|
-
### Example web app
|
|
225
|
-
|
|
226
|
-

|
|
227
|
-
|
|
228
|
-
The `./examples/index.html` file is a simple demo app that allows loging in a Pryv.io platform, register to events changes and create notes.
|
|
229
|
-
|
|
230
|
-
It can be tested on [http://pryv.github.io/lib-js-monitor](http://pryv.github.io/lib-js-monitor)
|
|
231
204
|
|
|
232
|
-
##
|
|
205
|
+
## Contributing
|
|
233
206
|
|
|
234
|
-
|
|
207
|
+
See the [Pryv JavaScript library README](https://github.com/pryv/lib-js#contributing)
|
|
235
208
|
|
|
236
|
-
- Setup: `npm run setup`
|
|
237
|
-
- Build pryv-monitor.js library for browsers: `npm run build`, the result is published in `./dist`
|
|
238
|
-
- Node Tests: `npm run test`
|
|
239
209
|
|
|
240
|
-
##
|
|
210
|
+
## License
|
|
241
211
|
|
|
242
|
-
-
|
|
212
|
+
[BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pryv/monitor",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Extends `pryv`
|
|
5
|
-
"main": "src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "mocha --reporter spec test/*.test.js",
|
|
8
|
-
"setup": "./scripts/setup-environment-dev.sh",
|
|
9
|
-
"build": "webpack"
|
|
10
|
-
},
|
|
11
|
-
"repository": {
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git://github.com/pryv/lib-js-monitor"
|
|
14
|
-
},
|
|
15
|
-
"bugs": {
|
|
16
|
-
"url": "https://github.com/pryv/lib-js-monitor/issues"
|
|
17
|
-
},
|
|
3
|
+
"version": "2.3.1",
|
|
4
|
+
"description": "Extends `pryv` with event-driven notifications for changes on a Pryv.io account",
|
|
18
5
|
"keywords": [
|
|
19
6
|
"Pryv",
|
|
20
7
|
"Pryv.io",
|
|
@@ -22,13 +9,15 @@
|
|
|
22
9
|
"Scope",
|
|
23
10
|
"Monitor"
|
|
24
11
|
],
|
|
25
|
-
"
|
|
12
|
+
"homepage": "https://github.com/pryv/lib-js/tree/master/components/pryv-monitor#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/pryv/lib-js/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git://github.com/pryv/lib-js"
|
|
19
|
+
},
|
|
26
20
|
"license": "BSD-3-Clause",
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
"@pryv/socket.io": "^1.0.5",
|
|
30
|
-
"chai": "^4.2.0",
|
|
31
|
-
"mocha": "^8.2.1",
|
|
32
|
-
"pryv": "^2.1.7"
|
|
33
|
-
}
|
|
21
|
+
"author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
|
|
22
|
+
"main": "src/index.js"
|
|
34
23
|
}
|
package/src/Monitor.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
const EventEmitter = require('events');
|
|
2
6
|
const UpdateMethod = require('./UpdateMethod/');
|
|
3
7
|
const _updateEvents = require('./lib/updateEvents');
|
|
@@ -5,37 +9,37 @@ const _updateStreams = require('./lib/updateStreams');
|
|
|
5
9
|
const Changes = require('./lib/Changes');
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
|
-
* @memberof
|
|
12
|
+
* @memberof pryv
|
|
9
13
|
*/
|
|
10
14
|
class Monitor extends EventEmitter {
|
|
11
15
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {(
|
|
14
|
-
* @param {
|
|
16
|
+
*
|
|
17
|
+
* @param {(pryv.APIEndpoint|pryv.Connection)} apiEndpointOrConnection APIEndoint or connection to use
|
|
18
|
+
* @param {pryv.Monitor.Scope} [eventsGetScope={}] The Scope to monitor
|
|
15
19
|
*/
|
|
16
|
-
constructor(apiEndpointOrConnection, eventsGetScope = {}) {
|
|
20
|
+
constructor (apiEndpointOrConnection, eventsGetScope = {}) {
|
|
17
21
|
super();
|
|
18
|
-
if (!Monitor.
|
|
22
|
+
if (!Monitor.pryv) {
|
|
19
23
|
throw new Error('package \'@pryv/monitor\' must loaded after package \'pryv\'');
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
this.eventsGetScope = { // default eventsGetScope values
|
|
23
|
-
fromTime: -
|
|
27
|
+
fromTime: -Number.MAX_VALUE,
|
|
24
28
|
toTime: Number.MAX_VALUE,
|
|
25
|
-
modifiedSince: -
|
|
26
|
-
}
|
|
29
|
+
modifiedSince: -Number.MAX_VALUE
|
|
30
|
+
};
|
|
27
31
|
Object.assign(this.eventsGetScope, eventsGetScope);
|
|
28
32
|
|
|
29
|
-
if (apiEndpointOrConnection instanceof Monitor.
|
|
33
|
+
if (apiEndpointOrConnection instanceof Monitor.pryv.Connection) {
|
|
30
34
|
this.connection = apiEndpointOrConnection;
|
|
31
35
|
} else {
|
|
32
|
-
this.connection = new Monitor.
|
|
36
|
+
this.connection = new Monitor.pryv.Connection(apiEndpointOrConnection);
|
|
33
37
|
}
|
|
34
38
|
this.states = {
|
|
35
39
|
started: false,
|
|
36
40
|
starting: false, // in phase of initializing
|
|
37
41
|
updatingEvents: false, // semaphore to prevent updating events in parallel
|
|
38
|
-
updatingStreams: false
|
|
42
|
+
updatingStreams: false // semaphore to prevent updating streams in parallel
|
|
39
43
|
};
|
|
40
44
|
}
|
|
41
45
|
|
|
@@ -43,7 +47,7 @@ class Monitor extends EventEmitter {
|
|
|
43
47
|
* Start the monitor
|
|
44
48
|
* @returns {Monitor} this
|
|
45
49
|
*/
|
|
46
|
-
async start() {
|
|
50
|
+
async start () {
|
|
47
51
|
if (this.states.started || this.states.starting) return this;
|
|
48
52
|
this.states.starting = true;
|
|
49
53
|
await _updateStreams(this);
|
|
@@ -62,7 +66,7 @@ class Monitor extends EventEmitter {
|
|
|
62
66
|
* request and update of events
|
|
63
67
|
* @returns {Monitor} this
|
|
64
68
|
*/
|
|
65
|
-
async updateEvents() {
|
|
69
|
+
async updateEvents () {
|
|
66
70
|
if (!this.states.started) {
|
|
67
71
|
throw new Error('Start Monitor before calling update Events');
|
|
68
72
|
}
|
|
@@ -94,7 +98,7 @@ class Monitor extends EventEmitter {
|
|
|
94
98
|
* request and update of streams
|
|
95
99
|
* @returns {Monitor} this
|
|
96
100
|
*/
|
|
97
|
-
async updateStreams() {
|
|
101
|
+
async updateStreams () {
|
|
98
102
|
if (!this.states.started) {
|
|
99
103
|
throw new Error('Start Monitor before calling update Streams');
|
|
100
104
|
}
|
|
@@ -125,20 +129,19 @@ class Monitor extends EventEmitter {
|
|
|
125
129
|
/**
|
|
126
130
|
* @private
|
|
127
131
|
* Called after init phase and each updateEvents
|
|
128
|
-
* Advertise the update method or any listener that the Monitor is ready
|
|
132
|
+
* Advertise the update method or any listener that the Monitor is ready
|
|
129
133
|
* for a next event update
|
|
130
134
|
*/
|
|
131
|
-
ready() {
|
|
132
|
-
if (!this.states.started) return; // it might be stoped
|
|
135
|
+
ready () {
|
|
136
|
+
if (!this.states.started) return; // it might be stoped
|
|
133
137
|
this.emit(Changes.READY);
|
|
134
138
|
}
|
|
135
139
|
|
|
136
|
-
|
|
137
140
|
/**
|
|
138
141
|
* Stop monitoring (no event will be fired anymore)
|
|
139
142
|
* @returns {Monitor} this
|
|
140
143
|
*/
|
|
141
|
-
stop() {
|
|
144
|
+
stop () {
|
|
142
145
|
if (!this.states.started) return this;
|
|
143
146
|
if (this.states.starting) throw new Error('Process is starting, wait for the end of initialization to stop it');
|
|
144
147
|
this.emit(Changes.STOP);
|
|
@@ -150,14 +153,14 @@ class Monitor extends EventEmitter {
|
|
|
150
153
|
* Used by updateMethods to be sure they can call updateXXX methods
|
|
151
154
|
* @property {Boolean} started - true is monitor is started
|
|
152
155
|
*/
|
|
153
|
-
get started() {
|
|
156
|
+
get started () {
|
|
154
157
|
return this.states.started;
|
|
155
158
|
}
|
|
156
159
|
|
|
157
160
|
/**
|
|
158
161
|
* Initialize the updateMethods with this Monitor
|
|
159
162
|
* @callback Monitor~UpdateMethod
|
|
160
|
-
* @param {Monitor} setMonitor
|
|
163
|
+
* @param {Monitor} setMonitor
|
|
161
164
|
*/
|
|
162
165
|
|
|
163
166
|
/**
|
|
@@ -167,14 +170,11 @@ class Monitor extends EventEmitter {
|
|
|
167
170
|
* @param {Monitor~UpdateMethod} updateMethod - the auto-update method
|
|
168
171
|
* @returns {Monitor} this
|
|
169
172
|
*/
|
|
170
|
-
addUpdateMethod(updateMethod) {
|
|
171
|
-
if (false) {
|
|
172
|
-
throw new Error('Argument is not a valid UpdateMethod instance');
|
|
173
|
-
}
|
|
173
|
+
addUpdateMethod (updateMethod) {
|
|
174
174
|
updateMethod.setMonitor(this);
|
|
175
|
-
return this
|
|
175
|
+
return this;
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
Monitor.UpdateMethod = UpdateMethod;
|
|
180
|
-
module.exports = Monitor;
|
|
180
|
+
module.exports = Monitor;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
const UpdateMethod = require('./UpdateMethod');
|
|
2
6
|
|
|
3
7
|
class EventsTimer extends UpdateMethod {
|
|
4
8
|
/**
|
|
5
9
|
* @param {Number} updateRateMS - the refresh rate in milliseconds
|
|
6
10
|
*/
|
|
7
|
-
constructor(updateRateMS) {
|
|
11
|
+
constructor (updateRateMS) {
|
|
8
12
|
super();
|
|
9
13
|
this.timer = null;
|
|
10
14
|
if (!updateRateMS || isNaN(updateRateMS) || updateRateMS < 1) {
|
|
@@ -15,10 +19,10 @@ class EventsTimer extends UpdateMethod {
|
|
|
15
19
|
|
|
16
20
|
async ready () {
|
|
17
21
|
if (this.timer != null) clearTimeout(this.timer);
|
|
18
|
-
this.timer = setTimeout(() => {
|
|
19
|
-
if (this.monitor.started) this.monitor.updateEvents()
|
|
22
|
+
this.timer = setTimeout(() => {
|
|
23
|
+
if (this.monitor.started) this.monitor.updateEvents();
|
|
20
24
|
}, this.updateRateMS);
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
module.exports = EventsTimer;
|
|
28
|
+
module.exports = EventsTimer;
|
|
@@ -1,22 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
|
|
2
6
|
const UpdateMethod = require('./UpdateMethod');
|
|
3
7
|
const Changes = require('../lib/Changes');
|
|
4
8
|
|
|
5
9
|
class Socket extends UpdateMethod {
|
|
6
|
-
|
|
7
|
-
super();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async ready() {
|
|
10
|
+
async ready () {
|
|
11
11
|
if (this.socket) return;
|
|
12
12
|
if (!this.monitor.connection.socket) {
|
|
13
13
|
throw new Error('You should load package @pryv/socket.io to use monitor with websockets');
|
|
14
14
|
}
|
|
15
15
|
this.socket = await this.monitor.connection.socket.open();
|
|
16
|
-
this.socket.on('eventsChanged', () => {
|
|
16
|
+
this.socket.on('eventsChanged', () => { this.monitor.updateEvents(); });
|
|
17
17
|
this.socket.on('streamsChanged', () => { this.monitor.updateStreams(); });
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
/* eslint-disable-next-line node/handle-callback-err */
|
|
19
|
+
this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR.error); });
|
|
20
|
+
}
|
|
20
21
|
|
|
21
22
|
async stop () {
|
|
22
23
|
if (!this.socket) return;
|
|
@@ -24,4 +25,4 @@ class Socket extends UpdateMethod {
|
|
|
24
25
|
this.socket = null;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
|
-
module.exports = Socket;
|
|
28
|
+
module.exports = Socket;
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
const Changes = require('../lib/Changes');
|
|
2
6
|
/**
|
|
3
7
|
* Interface for UpdateMonitor
|
|
4
|
-
* @memberof
|
|
8
|
+
* @memberof pryv.Monitor
|
|
5
9
|
* @constructor {Monitor~UpdateMethod} updateMethod.setMonitor - set only once
|
|
6
10
|
*/
|
|
7
11
|
class UpdateMethod {
|
|
8
12
|
/**
|
|
9
|
-
* Assign a Monitor to this updater.
|
|
13
|
+
* Assign a Monitor to this updater.
|
|
10
14
|
* Usually called by the monitor itself on monitor.addUpdateMethod()
|
|
11
|
-
* @param {Monitor} monitor
|
|
15
|
+
* @param {Monitor} monitor
|
|
12
16
|
*/
|
|
13
|
-
setMonitor(monitor) {
|
|
17
|
+
setMonitor (monitor) {
|
|
14
18
|
if (this.monitor) {
|
|
15
19
|
throw new Error('An update Method can be assigned to one monitor only');
|
|
16
20
|
}
|
|
@@ -27,13 +31,13 @@ class UpdateMethod {
|
|
|
27
31
|
* Called with no params, when all update tasks are done.
|
|
28
32
|
* Also used at "start" call
|
|
29
33
|
*/
|
|
30
|
-
async ready() { }
|
|
34
|
+
async ready () { }
|
|
31
35
|
|
|
32
36
|
/**
|
|
33
37
|
* Should be overwritten by subclases
|
|
34
38
|
* Called with no params, when monitor is stoped: updater should be stoped too.
|
|
35
39
|
*/
|
|
36
|
-
async stop() { }
|
|
40
|
+
async stop () { }
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
module.exports = UpdateMethod;
|
|
43
|
+
module.exports = UpdateMethod;
|