|
| 1 | +/* |
| 2 | + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + You may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { Event, EventPublisher, EventSubscriber } from "./event"; |
| 18 | +import { EventClass } from "../../types"; |
| 19 | + |
| 20 | +const DEFAULT_MESSAGE_INTERVAL_MS = 30_000; // 30 seconds |
| 21 | + |
| 22 | +/** |
| 23 | + * An event publisher that periodically publishes a batch of all unique events |
| 24 | + * encountered during the latest time interval. |
| 25 | + */ |
| 26 | +export class BatchingEventPublisher implements EventPublisher { |
| 27 | + protected readonly subscribersMap = new Map<EventClass, Set<EventSubscriber>>(); |
| 28 | + protected readonly pendingEvents = new Set<Event>(); |
| 29 | + protected publishingInterval?: ReturnType<typeof setInterval>; |
| 30 | + |
| 31 | + constructor(messageIntervalMs: number = DEFAULT_MESSAGE_INTERVAL_MS) { |
| 32 | + this.initPublishingInterval(messageIntervalMs); |
| 33 | + } |
| 34 | + |
| 35 | + protected initPublishingInterval(messageIntervalMs: number): void { |
| 36 | + this.publishingInterval = setInterval(() => this.sendMessages(), messageIntervalMs); |
| 37 | + // Allow the process to exit even if the interval is still running |
| 38 | + this.publishingInterval.unref(); |
| 39 | + } |
| 40 | + |
| 41 | + protected sendMessages(): void { |
| 42 | + for (const event of this.pendingEvents) { |
| 43 | + this.pendingEvents.delete(event); |
| 44 | + this.deliverEvent(event); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected deliverEvent(event: Event): void { |
| 49 | + const subscribers = this.subscribersMap.get(event.constructor as EventClass); |
| 50 | + if (!subscribers) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + for (const subscriber of subscribers) { |
| 55 | + subscriber.processEvent(event); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + subscribe(subscriber: EventSubscriber, eventClasses: Set<EventClass>): void { |
| 60 | + for (const eventClass of eventClasses) { |
| 61 | + let subscribers = this.subscribersMap.get(eventClass); |
| 62 | + if (!subscribers) { |
| 63 | + subscribers = new Set(); |
| 64 | + this.subscribersMap.set(eventClass, subscribers); |
| 65 | + } |
| 66 | + subscribers.add(subscriber); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + unsubscribe(subscriber: EventSubscriber, eventClasses: Set<EventClass>): void { |
| 71 | + for (const eventClass of eventClasses) { |
| 72 | + const subscribers = this.subscribersMap.get(eventClass); |
| 73 | + if (subscribers) { |
| 74 | + subscribers.delete(subscriber); |
| 75 | + if (subscribers.size === 0) { |
| 76 | + this.subscribersMap.delete(eventClass); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + publish(event: Event): void { |
| 83 | + if (event.isImmediateDelivery) { |
| 84 | + this.deliverEvent(event); |
| 85 | + } else { |
| 86 | + this.pendingEvents.add(event); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + releaseResources(): void { |
| 91 | + if (this.publishingInterval) { |
| 92 | + clearInterval(this.publishingInterval); |
| 93 | + this.publishingInterval = undefined; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments