-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
69 lines (60 loc) · 1.89 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use strict"
const NodeHelper = require("node_helper");
const Log = require("logger");
const Fetcher = require("./fetcher.js");
/**
* Node helper module component.
*
* This module is responsible for managing the creation and reuse
* of fetcher objects in order to efficiently retrieve data.
*/
module.exports = NodeHelper.create({
/**
* List of fetchers objects currently in use.
*/
fetchers: [],
/**
* Receive and validates messages from the module(s).
*
* @param {string} notification message type
* @param {object} payload message data
*/
socketNotificationReceived: function(notification, payload) {
if (notification === "SUBSCRIBE") this.subscribe(payload);
},
/**
* Subscribe a module to a (new) fetcher.
*
* @param {object} payload message data
*/
subscribe(payload) {
let fetcher = this.findFetcher(payload.config)
if (!fetcher) {
fetcher = this.createFetcher(payload.identifier, payload.config);
}
Log.info(`[${this.name}][${payload.identifier}] Subscribing to fetcher.`);
fetcher.addListener(payload.identifier);
},
/**
* Find a fetcher compatible with the module config.
*
* @param {object} config module config parameters
* @returns
*/
findFetcher(config) {
return this.fetchers.find(f => f.isListenerCompatible(config));
},
/**
* Create a new fetcher object for the module config.
*
* @param {string} identifier module identifier
* @param {object} config module config parameters
* @returns Fetcher
*/
createFetcher(identifier, config) {
Log.info(`[${this.name}][${identifier}] Creating new fetcher.`);
let fetcher = new Fetcher(config, (n, p) => this.sendSocketNotification(n, p))
this.fetchers.push(fetcher)
return fetcher;
},
});