forked from openhab/openhab-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.js
47 lines (36 loc) · 1.17 KB
/
provider.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
const osgi = require('./osgi');
const log = require('./log')('provider'); // eslint-disable-line no-unused-vars
// const utils = require('./utils');
function getAllFunctionNames (obj) {
let props = [];
let o = obj;
do {
props = props.concat(Object.getOwnPropertyNames(o));
o = Object.getPrototypeOf(o);
} while (o.constructor.name !== 'AbstractProvider');
return props.filter(p => typeof obj[p] === 'function');
}
class AbstractProvider {
constructor (type) {
this.typeName = type.class.getName();
this.JavaType = Java.extend(type);// require('@runtime/osgi').classutil.extend(type);
}
register () {
const javaConfig = {};
const functionNamesToBind = getAllFunctionNames(this)
.filter(f => f !== 'constructor')
.filter(f => f !== 'javaType');
for (const fn of functionNamesToBind) {
javaConfig[fn] = this[fn].bind(this);
}
const hostProvider = this.processHostProvider(new this.JavaType(javaConfig));
this.hostProvider = hostProvider;
osgi.registerService(this.hostProvider, this.typeName);
}
processHostProvider (hostProvider) {
return hostProvider;
}
}
module.exports = {
AbstractProvider
};