|
| 1 | +import { produceEffects } from "produceEffects"; |
| 2 | +import { aggregate } from "../aggregate"; |
| 3 | +import type { Effect } from "../Effect"; |
| 4 | +import type { |
| 5 | + DomainEvent, |
| 6 | + PoppedEvent, |
| 7 | + PushedEvent, |
| 8 | + ReplacedEvent, |
| 9 | +} from "../event-types"; |
| 10 | +import type { Stack } from "../Stack"; |
| 11 | +import type { Publisher } from "../utils/Publisher/Publisher"; |
| 12 | +import type { Aggregator } from "./Aggregator"; |
| 13 | + |
| 14 | +export class SyncAggregator implements Aggregator { |
| 15 | + private events: DomainEvent[]; |
| 16 | + private changePublisher: Publisher<{ effects: Effect[]; stack: Stack }>; |
| 17 | + private autoUpdateTask: DynamicallyScheduledTask; |
| 18 | + private previousStack: Stack; |
| 19 | + |
| 20 | + constructor( |
| 21 | + events: DomainEvent[], |
| 22 | + changePublisher: Publisher<{ effects: Effect[]; stack: Stack }>, |
| 23 | + ) { |
| 24 | + this.events = events; |
| 25 | + this.changePublisher = changePublisher; |
| 26 | + this.autoUpdateTask = new DynamicallyScheduledTask(async () => { |
| 27 | + this.updateStack(); |
| 28 | + }); |
| 29 | + this.previousStack = this.computeStack(); |
| 30 | + } |
| 31 | + |
| 32 | + getStack(): Stack { |
| 33 | + return this.computeStack(); |
| 34 | + } |
| 35 | + |
| 36 | + dispatchEvent(event: DomainEvent): void { |
| 37 | + this.events.push(event); |
| 38 | + this.updateStack(); |
| 39 | + } |
| 40 | + |
| 41 | + subscribeChanges( |
| 42 | + listener: (effects: Effect[], stack: Stack) => void, |
| 43 | + ): () => void { |
| 44 | + return this.changePublisher.subscribe(({ effects, stack }) => { |
| 45 | + listener(effects, stack); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private computeStack(): Stack { |
| 50 | + return aggregate(this.events, Date.now()); |
| 51 | + } |
| 52 | + |
| 53 | + private predictUpcomingTransitionStateUpdate(): { |
| 54 | + event: DomainEvent; |
| 55 | + timestamp: number; |
| 56 | + } | null { |
| 57 | + const stack = this.computeStack(); |
| 58 | + const activeActivities = stack.activities.filter( |
| 59 | + (activity) => |
| 60 | + activity.transitionState === "enter-active" || |
| 61 | + activity.transitionState === "exit-active", |
| 62 | + ); |
| 63 | + const mostRecentlyActivatedActivity = activeActivities.sort( |
| 64 | + (a, b) => b.estimatedTransitionEnd! - a.estimatedTransitionEnd!, |
| 65 | + )[0]; |
| 66 | + |
| 67 | + return mostRecentlyActivatedActivity |
| 68 | + ? { |
| 69 | + event: |
| 70 | + mostRecentlyActivatedActivity.exitedBy ?? |
| 71 | + mostRecentlyActivatedActivity.enteredBy, |
| 72 | + timestamp: mostRecentlyActivatedActivity.estimatedTransitionEnd!, |
| 73 | + } |
| 74 | + : null; |
| 75 | + } |
| 76 | + |
| 77 | + private updateStack(): void { |
| 78 | + const previousStack = this.previousStack; |
| 79 | + const currentStack = this.computeStack(); |
| 80 | + const effects = produceEffects(previousStack, currentStack); |
| 81 | + |
| 82 | + if (effects.length > 0) { |
| 83 | + this.changePublisher.publish({ effects, stack: currentStack }); |
| 84 | + |
| 85 | + this.previousStack = currentStack; |
| 86 | + |
| 87 | + const upcomingTransitionStateUpdate = |
| 88 | + this.predictUpcomingTransitionStateUpdate(); |
| 89 | + |
| 90 | + if (upcomingTransitionStateUpdate) { |
| 91 | + this.autoUpdateTask.schedule(upcomingTransitionStateUpdate.timestamp); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class DynamicallyScheduledTask { |
| 98 | + private task: () => Promise<void>; |
| 99 | + private scheduleId: number | null; |
| 100 | + |
| 101 | + constructor(task: () => Promise<void>) { |
| 102 | + this.task = task; |
| 103 | + this.scheduleId = null; |
| 104 | + } |
| 105 | + |
| 106 | + schedule(timestamp: number): void { |
| 107 | + if (this.scheduleId !== null) { |
| 108 | + clearTimeout(this.scheduleId); |
| 109 | + this.scheduleId = null; |
| 110 | + } |
| 111 | + |
| 112 | + const timeoutId = setTimeout( |
| 113 | + () => { |
| 114 | + if (this.scheduleId !== timeoutId) return; |
| 115 | + if (Date.now() < timestamp) { |
| 116 | + this.schedule(timestamp); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + this.task(); |
| 121 | + this.scheduleId = null; |
| 122 | + }, |
| 123 | + Math.max(0, timestamp - Date.now()), |
| 124 | + ); |
| 125 | + |
| 126 | + this.scheduleId = timeoutId; |
| 127 | + } |
| 128 | +} |
0 commit comments