|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/15/2026 |
| 5 | +ClientReplicationApplicationLayer.js |
| 6 | +*/ |
| 7 | +import ClientReconciliationStrategy, { |
| 8 | + REPLICATION_IGNORE_REASONS, |
| 9 | +} from './ClientReconciliationStrategy.js'; |
| 10 | +import ReplicationMessageContract from './ReplicationMessageContract.js'; |
| 11 | +import StateReplication from './StateReplication.js'; |
| 12 | + |
| 13 | +function clone(value) { |
| 14 | + return JSON.parse(JSON.stringify(value)); |
| 15 | +} |
| 16 | + |
| 17 | +export default class ClientReplicationApplicationLayer { |
| 18 | + constructor({ |
| 19 | + sessionId = 'session', |
| 20 | + contract = null, |
| 21 | + reconciliationStrategy = null, |
| 22 | + stateReplication = null, |
| 23 | + } = {}) { |
| 24 | + this.sessionId = sessionId; |
| 25 | + this.contract = contract || new ReplicationMessageContract({ sessionId }); |
| 26 | + this.reconciliationStrategy = reconciliationStrategy || new ClientReconciliationStrategy(); |
| 27 | + this.stateReplication = stateReplication || new StateReplication(); |
| 28 | + |
| 29 | + this.pendingEnvelopes = []; |
| 30 | + this.ignoredEnvelopes = []; |
| 31 | + this.replicatedState = []; |
| 32 | + this.lastAppliedTick = -1; |
| 33 | + this.lastAppliedSequence = -1; |
| 34 | + this.appliedCount = 0; |
| 35 | + this.ignoredCount = 0; |
| 36 | + this.nextReceivedOrder = 0; |
| 37 | + } |
| 38 | + |
| 39 | + ingestReplicationEnvelope(envelope, { receivedAtMs = 0 } = {}) { |
| 40 | + const validation = this.contract.validate(envelope, { sessionId: this.sessionId }); |
| 41 | + if (!validation.ok) { |
| 42 | + this.ignoredEnvelopes.push({ |
| 43 | + reason: REPLICATION_IGNORE_REASONS.INVALID_ENVELOPE, |
| 44 | + code: validation.code, |
| 45 | + envelope: envelope ? clone(envelope) : null, |
| 46 | + }); |
| 47 | + this.ignoredCount += 1; |
| 48 | + return { |
| 49 | + ok: false, |
| 50 | + code: validation.code, |
| 51 | + message: validation.message, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + const normalized = this.contract.normalize(envelope, { |
| 56 | + receivedAtMs, |
| 57 | + receivedOrder: this.nextReceivedOrder, |
| 58 | + }); |
| 59 | + this.nextReceivedOrder += 1; |
| 60 | + this.pendingEnvelopes.push(normalized); |
| 61 | + return { |
| 62 | + ok: true, |
| 63 | + pending: this.pendingEnvelopes.length, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + applyPendingReplication() { |
| 68 | + const orderedEnvelopes = this.reconciliationStrategy.sort(this.pendingEnvelopes); |
| 69 | + this.pendingEnvelopes.length = 0; |
| 70 | + |
| 71 | + let applied = 0; |
| 72 | + let ignored = 0; |
| 73 | + orderedEnvelopes.forEach((envelope) => { |
| 74 | + const decision = this.reconciliationStrategy.shouldApply(envelope, { |
| 75 | + lastAppliedTick: this.lastAppliedTick, |
| 76 | + lastAppliedSequence: this.lastAppliedSequence, |
| 77 | + }); |
| 78 | + |
| 79 | + if (!decision.apply) { |
| 80 | + this.ignoredEnvelopes.push({ |
| 81 | + reason: decision.reason, |
| 82 | + envelope: clone(envelope), |
| 83 | + }); |
| 84 | + this.ignoredCount += 1; |
| 85 | + ignored += 1; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const baseState = envelope.snapshotType === 'full' ? [] : this.replicatedState; |
| 90 | + this.replicatedState = this.stateReplication.applySnapshot(envelope.snapshot, baseState); |
| 91 | + this.lastAppliedTick = envelope.authoritativeTick; |
| 92 | + this.lastAppliedSequence = envelope.replicationSequence; |
| 93 | + this.appliedCount += 1; |
| 94 | + applied += 1; |
| 95 | + }); |
| 96 | + |
| 97 | + return { |
| 98 | + applied, |
| 99 | + ignored, |
| 100 | + snapshot: this.getReplicationStatus(), |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + getReplicatedStateSnapshot() { |
| 105 | + return clone(this.replicatedState); |
| 106 | + } |
| 107 | + |
| 108 | + getIgnoredEnvelopes() { |
| 109 | + return this.ignoredEnvelopes.map((entry) => clone(entry)); |
| 110 | + } |
| 111 | + |
| 112 | + getReplicationStatus() { |
| 113 | + return { |
| 114 | + sessionId: this.sessionId, |
| 115 | + lastAppliedTick: this.lastAppliedTick, |
| 116 | + lastAppliedSequence: this.lastAppliedSequence, |
| 117 | + appliedCount: this.appliedCount, |
| 118 | + ignoredCount: this.ignoredCount, |
| 119 | + pendingEnvelopes: this.pendingEnvelopes.length, |
| 120 | + stateEntities: this.replicatedState.length, |
| 121 | + contract: this.contract.getStats(), |
| 122 | + }; |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments