syndicate-js/packages/broker/src/monitor.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-11-21 14:23:30 +00:00
"use strict";
const UI = activate require("@syndicate-lang/driver-browser-ui");
// @jsx UI.html
// @jsxFrag UI.htmlFragment
2019-05-12 22:26:01 +00:00
const { WSBroker, ToBroker, FromBroker, BrokerConnected } = activate require("./client");
2018-11-21 14:23:30 +00:00
2019-05-12 22:26:01 +00:00
assertion type ConnectionScope(connId, scope);
2018-11-21 14:23:30 +00:00
message type Disconnect(connId);
spawn {
const ui = new UI.Anchor();
assert ui.html('body',
<div id="main">
<h1>Broker monitor</h1>
<div id="scopes"></div>
</div>);
const url = (function () {
const u = new URL(document.location);
u.protocol = u.protocol.replace(/^http/, 'ws');
2019-05-12 22:26:01 +00:00
u.pathname = '/';
2018-11-21 14:23:30 +00:00
return u.toString();
})();
2019-05-12 22:26:01 +00:00
const addr = WSBroker(url, "monitor");
2018-11-21 14:23:30 +00:00
2019-05-12 22:26:01 +00:00
during BrokerConnected(addr) {
during FromBroker(addr, ConnectionScope(_, $scope)) {
2018-11-21 14:23:30 +00:00
const ui = new UI.Anchor();
assert ui.html('#scopes',
<div class={`scope_${scope}`}>
<p>Scope: <tt>{scope}</tt></p>
<ul></ul>
</div>);
2019-05-12 22:26:01 +00:00
during FromBroker(addr, ConnectionScope($id, scope)) {
2018-11-21 14:23:30 +00:00
const ui = new UI.Anchor();
assert ui.html(`#scopes div.scope_${scope} ul`,
<li>{id.toString()} <button class="disconnect">Disconnect</button></li>);
on message UI.UIEvent(ui.fragmentId, 'button.disconnect', 'click', _) {
2019-05-12 22:26:01 +00:00
send ToBroker(addr, Disconnect(id));
2018-11-21 14:23:30 +00:00
}
}
}
}
}