@uupaa/messagepassing
Version:
Implementation of lightweight message passing logic for TypeScript.
54 lines (47 loc) • 1.4 kB
HTML
<html><head>
<title>MessagePassing browser test</title>
</head>
<body>
<script type="module">
import { MessagePassing } from "../lib/MessagePassing.esm.js";
class Sub1 {
constructor(msg) {
msg.register(this, ["Hello"]);
}
onmessage(selector, options) {
switch (selector) {
case "Hello": return "World";
}
}
}
class Sub2 {
constructor(msg) {
msg.register(this, ["Happy"]);
}
onmessage(selector, options) {
switch (selector) {
case "Happy": return "Halloween";
}
}
}
const msg = new MessagePassing();
const sub1 = new Sub1(msg);
const sub2 = new Sub2(msg);
// post is no result
msg.to(sub1, sub2).post("Hello"); // multicast
msg.to().remove(sub1).post("Happy", [1, 2, 3]); // broadcast(exclude sub1)
// send with result
const result1 = msg.to(sub1, sub2).send("Hello"); // multicas
const result2 = msg.to().remove(sub1).send("Happy", [1, 2, 3]); // broadcast(exclude sub1)
if (result1.get(sub1) === "World" &&
result1.get(sub2) === undefined &&
result2.get(sub1) === undefined &&
result2.get(sub2) === "Halloween" &&
result2.list(sub2).join("") === ["Halloween"].join("") ) {
document.body.style.backgroundColor = "lime";
} else {
document.body.style.backgroundColor = "red";
}
</script>
</body>
</html>