-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from medishen/dev/v2
feat(events): Two new methods were added, called request and channel,…
- Loading branch information
Showing
16 changed files
with
524 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { CorrelationId } from '../types'; | ||
|
||
export class CorrelationIdFactory { | ||
private readonly namespace: string; | ||
private sequence: number = 0; | ||
private lastTimestamp: number = 0; | ||
|
||
constructor(namespace: string = 'gland') { | ||
this.namespace = namespace; | ||
} | ||
|
||
create(): CorrelationId { | ||
const timestamp = Date.now(); | ||
const random = Math.random().toString(36).substring(2, 15); | ||
const sequence = this.getSequenceNumber(timestamp); | ||
|
||
return this.formatId({ | ||
timestamp, | ||
sequence, | ||
random, | ||
node: this.namespace, | ||
}) as CorrelationId; | ||
} | ||
|
||
private getSequenceNumber(timestamp: number): number { | ||
if (timestamp === this.lastTimestamp) { | ||
this.sequence = (this.sequence + 1) & 0xfff; // 12-bit sequence | ||
} else { | ||
this.sequence = 0; | ||
this.lastTimestamp = timestamp; | ||
} | ||
return this.sequence; | ||
} | ||
|
||
private formatId(parts: { timestamp: number; sequence: number; random: string; node: string }): string { | ||
return [parts.timestamp.toString(16).padStart(10, '0'), parts.sequence.toString(16).padStart(3, '0'), parts.random, parts.node].join('-'); | ||
} | ||
|
||
static validate(id: string): id is CorrelationId { | ||
return /^[0-9a-f]{10}-[0-9a-f]{3}-[a-z0-9]{13}-[a-z]+$/.test(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { EventType } from '@gland/common'; | ||
import { Event } from '../types'; | ||
|
||
interface QueueNode<T extends EventType, D> { | ||
event: Event<T, D>; | ||
next?: QueueNode<T, D>; | ||
prev?: QueueNode<T, D>; | ||
} | ||
|
||
export class EventQueue<T extends EventType = EventType, D = unknown> { | ||
private readonly maxSize: number; | ||
private readonly map = new Map<string, QueueNode<T, D>>(); | ||
private head?: QueueNode<T, D>; | ||
private tail?: QueueNode<T, D>; | ||
private size = 0; | ||
|
||
constructor(maxSize: number = 1000) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
enqueue(event: Event<T, D>): void { | ||
const node: QueueNode<T, D> = { event }; | ||
const key = this.getEventKey(event); | ||
|
||
if (this.map.has(key)) { | ||
this.moveToFront(key); | ||
return; | ||
} | ||
|
||
this.map.set(key, node); | ||
this.addToFront(node); | ||
this.ensureSize(); | ||
} | ||
|
||
dequeue(): Event<T, D> | undefined { | ||
if (!this.tail) return undefined; | ||
|
||
const node = this.tail; | ||
this.removeNode(node); | ||
return node.event; | ||
} | ||
|
||
process(callback: (event: Event<T, D>) => void | Promise<void>): void { | ||
let current = this.head; | ||
while (current) { | ||
callback(current.event); | ||
current = current.next; | ||
} | ||
this.clear(); | ||
} | ||
|
||
clear(): void { | ||
this.map.clear(); | ||
this.head = undefined; | ||
this.tail = undefined; | ||
this.size = 0; | ||
} | ||
|
||
private getEventKey(event: Event<T, D>): string { | ||
return `${event.type}:${event.correlationId || event.timestamp}`; | ||
} | ||
|
||
private addToFront(node: QueueNode<T, D>): void { | ||
if (!this.head) { | ||
this.head = this.tail = node; | ||
} else { | ||
node.next = this.head; | ||
this.head.prev = node; | ||
this.head = node; | ||
} | ||
this.size++; | ||
} | ||
|
||
private moveToFront(key: string): void { | ||
const node = this.map.get(key); | ||
if (!node || node === this.head) return; | ||
|
||
// Remove from current position | ||
if (node.prev) node.prev.next = node.next; | ||
if (node.next) node.next.prev = node.prev; | ||
if (node === this.tail) this.tail = node.prev; | ||
|
||
// Add to head | ||
node.next = this.head; | ||
node.prev = undefined; | ||
if (this.head) this.head.prev = node; | ||
this.head = node; | ||
} | ||
|
||
private removeNode(node: QueueNode<T, D>): void { | ||
if (node.prev) node.prev.next = node.next; | ||
if (node.next) node.next.prev = node.prev; | ||
if (node === this.head) this.head = node.next; | ||
if (node === this.tail) this.tail = node.prev; | ||
|
||
this.map.delete(this.getEventKey(node.event)); | ||
this.size--; | ||
} | ||
|
||
private ensureSize(): void { | ||
while (this.size > this.maxSize && this.tail) { | ||
this.removeNode(this.tail); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.