-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
69 lines (56 loc) · 1.78 KB
/
index.ts
File metadata and controls
69 lines (56 loc) · 1.78 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
import defaults from "./defaults";
import parse from "parse-strings-in-object";
import rc from "rc";
import { getLogger } from "log4js";
import { ChannelReceiver, ChannelSender, TetherAgent } from "tether-agent";
const appName = defaults.appName;
const config: typeof defaults = parse(rc(appName, defaults));
const logger = getLogger(appName);
logger.level = config.loglevel;
logger.info("started with config", config);
logger.debug("Debug logging enabled; output could be verbose!");
const main = async () => {
const agent = await TetherAgent.create("brain");
// Note the alternative syntax for doing the same thing, below:
// ...
// const sender = new ChannelSender(agent, "randomValues");
const sender = agent.createSender("randomValues");
sender.send({
value: Math.random(),
timestamp: Date.now(),
something: "one",
});
const genericReceiver = await agent.createReceiver(
"randomValuesStrictlyTyped"
);
genericReceiver.on("message", (payload, topic) => {
logger.info(
"Our generic receiver got:",
payload,
typeof payload,
"on topic",
topic
);
});
const typedReceiver = await agent.createReceiver<number>(
"randomValuesStrictlyTyped"
);
typedReceiver.on("message", (payload) => {
logger.info("Our typed receiver got", payload, typeof payload);
});
const typedSender = agent.createSender<number>("randomValuesStrictlyTyped");
// This will be rejected by TypeScript compiler:
// typedSender.send({
// value: Math.random(),
// timestamp: Date.now(),
// something: "one",
// });
// This will work fine, though
typedSender.send(Math.random());
setTimeout(() => {
agent.disconnect();
}, 2000);
};
// ================================================
// Kick off main process here
main();