-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertx-eventbus.html
103 lines (96 loc) · 3.3 KB
/
vertx-eventbus.html
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
<polymer-element name="vertx-eventbus"
attributes="url auto pingIntervalSeconds reconnectIntervalSeconds">
<template>
<content id="_content" select="register-handler"></content>
</template>
<script>
Polymer('vertx-eventbus', {
url: null,
auto: false,
pingIntervalSeconds: null,
reconnectIntervalSeconds: null,
registerHandlers: [],
eventBus: null,
isConnected: false,
connect: function() {
var __self = this;
try {
var options = this.pingIntervalSeconds ? {vertxbus_ping_interval: (1000 * this.pingIntervalSeconds)} : null;
this.eventBus = new vertx.EventBus(this.url, options);
this.eventBus.onopen = function() {
__self.isConnected = true;
__self.fire('open');
if (__self.registerHandlers) {
__self.registerHandlers.forEach(function(regHandler) {
__self.eventBus.registerHandler(regHandler.address, regHandler.handler);
});
}
};
this.eventBus.onclose = function() {
__self.isConnected = false;
__self.fire('close');
if (__self.reconnectIntervalSeconds) {
setTimeout(__self.connect(), (1000 * __self.reconnectIntervalSeconds));
}
};
} catch (e) {
console.warn(e);
this.fire('error', e);
}
},
close: function() {
try {
if (this.isConnected) {
this.registerHandlers.forEach(function(regHandler) {
this.eventBus.unregisterHandler(regHandler.address, regHandler.handler);
});
this.eventBus.close();
}
} catch (e) {
console.warn(e);
this.fire('error', e);
}
},
send: function(address, message, replyHandler) {
try {
if (this.isConnected) {
this.eventBus.send(address, message, replyHandler);
}
} catch (e) {
console.warn(e);
this.fire('error', e);
}
},
publish: function(address, message) {
try {
if (this.isConnected) {
this.eventBus.publish(address, message);
}
} catch (e) {
console.warn(e);
this.fire('error', e);
}
},
domReady: function() {
var __self = this;
var handlerElements = this.$._content.getDistributedNodes().array();
var handlers = [];
handlerElements.forEach(function(regHandler) {
var address = regHandler.getAttribute('address');
var handlerFn = eval('(' + regHandler.getAttribute('handler') + ')');
if (typeof handlerFn !== 'function') {
var message = 'Registered handler is not a function.';
console.warn(message);
__self.fire('error', message);
return;
}
handlers.push({address: regHandler.getAttribute('address'), handler: handlerFn});
});
this.registerHandlers = handlers;
if (this.auto) {
this.connect();
}
}
});
</script>
</polymer-element>