-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdiscovery.js
128 lines (102 loc) · 2.97 KB
/
discovery.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const Thing = require('./thing');
const { BasicDiscovery, addService, removeService, debug } = require('tinkerhub-discovery');
const parent = Symbol('parent');
const mapper = Symbol('mapper');
const available = Symbol('available');
const unavailable = Symbol('unavailable');
const updated = Symbol('updated');
const mapAndAddService = Symbol('mapService');
const mappedId = Symbol('mappedId');
function toMapper(m) {
if(typeof m === 'function') {
return {
create: m
};
} else if(typeof m === 'object') {
if(! m.create) {
throw new Error('create(service) is needed in mapper');
}
return m;
} else {
throw new Error('Function or object (with create-method) needed for discovery');
}
}
module.exports = class ThingDiscovery extends BasicDiscovery {
static get type() {
return 'thing';
}
constructor(parent_, mapper_) {
super();
this[parent] = parent_;
this[mapper] = toMapper(mapper_);
this[available] = this[available].bind(this);
this[unavailable] = this[unavailable].bind(this);
this[updated] = this[updated].bind(this);
if(parent_.active) {
// Make sure we start if we are being filtered on an already started discovery
parent_.on('available', this[available]);
parent_.on('unavailable', this[unavailable]);
parent_.on('updated', this[updated]);
for(const service of parent_.services) {
this[mapAndAddService](service);
}
this.active = true;
}
}
/**
* Start the discovery.
*/
start() {
// Protect against starting twice and registering twice
if(this[parent].active) return;
this[parent].on('available', this[available]);
this[parent].on('unavailable', this[unavailable]);
this[parent].on('updated', this[updated]);
this[parent].start();
super.start();
}
/**
* Stop the discovery.
*/
stop() {
this[parent].stop();
this[parent].off('available', this[available]);
this[parent].off('unavailable', this[unavailable]);
this[parent].off('updated', this[updated]);
super.stop();
}
[mapAndAddService](service) {
Promise.resolve(this[mapper].create(service))
.then(mapped => mapped && mapped.init())
.then(mapped => {
if(mapped) {
if(mapped instanceof Thing) {
// Bind up a listener for removal if the thing is destroyed
const id = mapped.id;
mapped.on('thing:destroy', () => this[removeService](id));
// Cache the id this service mapped to
service[mappedId] = mapped.id;
// Add the mapped service
this[addService](mapped);
} else {
this[debug]('Did not map into a thing:', mapped);
}
}
})
.catch(error => this[debug]('Could not map service', error));
}
[available](service) {
this[mapAndAddService](service);
}
[unavailable](service) {
// Get the id that this service mapped to
const id = service[mappedId];
// Make sure that this service has been mapped to something
if(! id) return;
this[removeService](id);
}
[updated](service) {
// TODO: Fetch the mapped service and call mapper.update()
}
};