Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event subscription and improve Counter example #1008

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/browser/counter.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@
// JSONEditor.defaults.theme = 'foundation5';
JSONEditor.defaults.iconlib = "fontawesome4";
</script>
<style>
@keyframes blink {
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.blink {
animation: blink 0.5s step-start 0s 1;
}

.hidden {
display: none;
}
</style>
</head>
<body>
<div id="topbar" class="row">
Expand Down Expand Up @@ -48,6 +65,10 @@ <h1>Counter Client Example in the Browser</h1>
<figure>
<figcaption><h4>Events</h4></figcaption>
<ul id="events" class="side-nav"></ul>
<div id="event-display" class="hidden">
<p>Event count: <span id="event-count"></span></p>
<p>Last payload: <span id="event-payload"></span></p>
</div>
</figure>
</div>
</div>
Expand Down
13 changes: 12 additions & 1 deletion examples/browser/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ function showInteractions(thing) {

eventSubscriptions[evnt] = false;
let subscription;
let eventCounter = 0;
const eventDisplay = document.getElementById("event-display");
checkbox.onclick = (click) => {
if (
document.getElementById(evnt).checked &&
Expand All @@ -124,12 +126,17 @@ function showInteractions(thing) {
eventSubscriptions[evnt] = true;
thing
.subscribeEvent(evnt, async function (data) {
console.log('Event "' + evnt + '"');
const value = await data.value();
console.log('Event "' + evnt + '"', value);
eventCounter++;
updateProperties();
danielpeintner marked this conversation as resolved.
Show resolved Hide resolved
document.getElementById("event-count").innerHTML = eventCounter;
document.getElementById("event-payload").innerHTML = value;
})
.then((sub) => {
subscription = sub;
console.log("Subscribed for event: " + evnt);
eventDisplay.style.display = "block";
})
.catch((error) => {
window.alert("Event " + evnt + " error\nMessage: " + error);
Expand All @@ -142,6 +149,10 @@ function showInteractions(thing) {
.stop()
.then(() => {
console.log("Unsubscribed for event: " + evnt);
eventDisplay.style.display = "none";
eventCounter = 0;
document.getElementById("event-count").innerHTML = eventCounter;
document.getElementById("event-payload").innerHTML = "";
})
.catch((error) => {
window.alert("Event " + evnt + " error\nMessage: " + error);
Expand Down
120 changes: 80 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@
"pretty-quick": "^3.1.1",
"sinon": "11.1.1",
"source-map-support": "^0.5.20",
"ts-loader": "8",
"ts-node": "10.1.0",
"ts-node": "^10.9.1",
"tslint": "5.12.1",
"typescript": "4.4.3",
"typescript": "^4.7.4",
"typescript-standard": "^0.3.36"
}
}
4 changes: 2 additions & 2 deletions packages/binding-coap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"eslint-plugin-unused-imports": "^1.1.4",
"mocha": "^9.2.2",
"prettier": "^2.3.2",
"ts-node": "10.1.0",
"typescript": "4.4.3",
"ts-node": "^10.9.1",
"typescript": "^4.7.4",
"typescript-standard": "^0.3.36"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-unused-imports": "^1.1.4",
"prettier": "^2.3.2",
"typescript": "4.4.3",
"typescript": "^4.7.4",
"typescript-standard": "^0.3.36",
"wot-typescript-definitions": "0.8.0-SNAPSHOT.26"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/binding-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"prettier": "^2.3.2",
"ssestream": "^1.1.0",
"timekeeper": "^2.2.0",
"ts-node": "10.1.0",
"typescript": "4.4.3",
"ts-node": "^10.9.1",
"typescript": "^4.7.4",
"typescript-standard": "^0.3.36",
"wot-typescript-definitions": "0.8.0-SNAPSHOT.26"
},
Expand Down
Loading