State and Workflow Patterns
Event Queue Case
An event queue can be summarized by topic while preserving typed event fields.
queue summary
Counting selected events shows how a typed queue can feed a dashboard or worker.
Event Queue Case
events.ts
Replay: real traced execution (multi-file project)
type QueueEvent = {
topic: "build" | "deploy";
priority: number;
};
function topicCount(events: QueueEvent[], topic: QueueEvent["topic"]): number {
let count = 0;
for (const event of events) {
if (event.topic === topic) {
count += 1;
}
}
return count;
}
const selectedTopic: QueueEvent["topic"] = "build";
const events: QueueEvent[] = [
{ topic: "build", priority: 2 },
{ topic: "deploy", priority: 1 },
{ topic: "build", priority: 3 }
];
const count: number = topicCount(events, selectedTopic);
console.log(`topic=${selectedTopic};count=${count}`);
type QueueEvent = {
topic: "build" | "deploy";
priority: number;
};
function topicCount(events: QueueEvent[], topic: QueueEvent["topic"]): number {
let count = 0;
for (const event of events) {
if (event.topic === topic) {
count += 1;
}
}
return count;
}
const selectedTopic: QueueEvent["topic"] = "deploy";
const events: QueueEvent[] = [
{ topic: "build", priority: 2 },
{ topic: "deploy", priority: 1 },
{ topic: "build", priority: 3 }
];
const count: number = topicCount(events, selectedTopic);
console.log(`topic=${selectedTopic};count=${count}`);
selectedTopic ← build, events ← [object Object],[object Object],[object Object]
15 return count;16}1718const selectedTopic→ build: QueueEvent["topic"] = "build"; //@selectedTopic="deploy"19const events→ [object Object],[object Object],[object Object]: QueueEvent[] = [20 { topic: "build", priority: 2 },21 { topic: "deploy", priority: 1 },22 { topic: "build", priority: 3 }23];2425const count: number = topicCount(events[object Object],[object Object],[object Object], selectedTopicbuild);count ← 0
3 priority: number;4};56function topicCount(events[object Object],[object Object],[object Object]: QueueEvent[], topicbuild: QueueEvent["topic"]): number {7 let count→ 0 = 0;if (event.topic === topic)
pass 1 of 29for (const event of events) {10 if (event.topicbuild === topicbuild) {11 count += 1;12 }if (event.topic === topic)
pass 2 of 29for (const event of events) {10 if (event.topicbuild === topicbuild) {11 count += 1;12 }return count;
12 }13 }1415 return count2;16}count ← 2
22 { topic: "build", priority: 3 }23];2425const count→ 2: number = topicCount(events[object Object],[object Object],[object Object], selectedTopicbuild);2627console.log(`topic=${selectedTopicbuild};count=${count2}`);outputtopic=build;count=2
selectedTopic ← deploy, events ← [object Object],[object Object],[object Object]
15 return count;16}1718const selectedTopic→ deploy: QueueEvent["topic"] = "deploy";19const events→ [object Object],[object Object],[object Object]: QueueEvent[] = [20 { topic: "build", priority: 2 },21 { topic: "deploy", priority: 1 },22 { topic: "build", priority: 3 }23];2425const count: number = topicCount(events[object Object],[object Object],[object Object], selectedTopicdeploy);count ← 0
3 priority: number;4};56function topicCount(events[object Object],[object Object],[object Object]: QueueEvent[], topicdeploy: QueueEvent["topic"]): number {7 let count→ 0 = 0;if (event.topic === topic)
9for (const event of events) {10 if (event.topicdeploy === topicdeploy) {11 count += 1;12 }return count;
12 }13 }1415 return count1;16}count ← 1
22 { topic: "build", priority: 3 }23];2425const count→ 1: number = topicCount(events[object Object],[object Object],[object Object], selectedTopicdeploy);2627console.log(`topic=${selectedTopicdeploy};count=${count1}`);outputtopic=deploy;count=1