-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathjavascript-class.js.hbs
More file actions
108 lines (89 loc) · 4.03 KB
/
javascript-class.js.hbs
File metadata and controls
108 lines (89 loc) · 4.03 KB
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
const DBus = require('dbus-next');
const { EventEmitter } = require('events');
/*
* Generated by dbus-next interface generator
* Template: javascript-class.js.hbs
*/
{{#if xmlData}}
// Introspection XML of {{serviceName}} at {{objectPath}}
const XMLObjectData = `{{xmlData}}`;
{{/if}}
{{#each interfaces}}
/**
* Service: {{../serviceName}}
* ObjectPath: {{../objectPath}}
* Interface: {{$.name}}
*/
export class {{className $.name}} extends EventEmitter {
static Connect(bus, objectPath, xml) { {{#if ../xmlData}}
if(!objectPath) { objectPath = "{{../objectPath}}"; }
if(!xml) { xml = XMLObjectData; }{{/if}}
return bus.getProxyObject('{{../serviceName}}', objectPath, xml).then((obj) => new {{className $.name}}(obj));
}
constructor(dbusObject) {
super();
this.dbusInterfaceName = '{{$.name}}';
this.dbusObject = dbusObject;
this.thisDBusInterface = dbusObject.getInterface('{{$.name}}');
this.propertiesDBusInterface = dbusObject.getInterface('org.freedesktop.DBus.Properties');
// forward property change events
const forwardPropertyChange = (iface, changed, invalidated) => {
if(iface === this.dbusInterfaceName) {
this.emit('PropertiesChanged', iface, changed, invalidated);
}
}
// forward all signals
this.on("newListener", (event, listener) => {
if(event === "PropertiesChanged" && this.listenerCount('PropertiesChanged') === 0) {
this.propertiesDBusInterface.on('PropertiesChanged', forwardPropertyChange);
} else {
this.thisDBusInterface.on(event, listener);
}
});
this.on("removeListener", (event, listener) => {
if(event === "PropertiesChanged" && this.listenerCount('PropertiesChanged') === 0) {
this.propertiesDBusInterface.removeListener('PropertiesChanged', forwardPropertyChange);
} else {
this.thisDBusInterface.removeListener(event, listener);
}
});
}
/***** Properties *****/
getProperties() {
return this.propertiesDBusInterface.GetAll(this.dbusInterfaceName);
}
getProperty(name) {
return this.propertiesDBusInterface.Get(this.dbusInterfaceName, name);
}
setProperty(name, value) {
return this.propertiesDBusInterface.Set(this.dbusInterfaceName, name, value);
}
{{#each property}}
//@property({ name: '{{$.name}}', signature: '{{$.type}}', access: {{accessConst $.access}} }){{#ifeq $.access "read"}}
{{$.name}}() {
return this.propertiesDBusInterface.Get(this.dbusInterfaceName, '{{$.name}}'){{#unlesseq $.type "v"}}.then((variant) => variant.value){{/unlesseq}};
}{{/ifeq}}{{#ifeq $.access "write"}}
{{$.name}}(value) {
return this.propertiesDBusInterface.Set(this.dbusInterfaceName, '{{$.name}}', {{#ifeq $.type "v"}}value{{else}}new DBus.Variant("{{$.type}}", value){{/ifeq}});
}{{/ifeq}}{{#ifeq $.access "readwrite"}}
{{$.name}}(value) {
if(value !== undefined) {
return this.propertiesDBusInterface.Set(this.dbusInterfaceName, '{{$.name}}', {{#ifeq $.type "v"}}value{{else}}new DBus.Variant("{{$.type}}", value){{/ifeq}});
} else {
return this.propertiesDBusInterface.Get(this.dbusInterfaceName, '{{$.name}}'){{#unlesseq $.type "v"}}.then((variant) => variant.value){{/unlesseq}};
}
}{{/ifeq}}
{{/each}}
/***** Methods *****/
{{#each method}}
//@method({ name: '{{$.name}}', inSignature: '{{inSignature arg}}', outSignature: '{{outSignature arg}}' })
{{$.name}}({{#each arg}}{{#ifeq $.direction "in"}}{{$.name}}{{#unless @last}}, {{/unless}}{{/ifeq}}{{/each}}) {
return this.thisDBusInterface.{{$.name}}({{#each arg}}{{#ifeq $.direction "in"}}{{$.name}}{{#unless @last}}, {{/unless}}{{/ifeq}}{{/each}});
}
{{/each}}
/***** Signals for {{$.name}} *****/
{{#each signal}}
//@signal({ name: '{{$.name}}', signature: '{{signature arg}}' })
{{/each}}
}
{{/each}}