1+ import mergeWith from 'lodash.mergewith' ;
2+ import cloneDeep from 'lodash.clonedeep' ;
3+ import { patch } from '@n1ru4l/json-patch-plus' ;
4+
5+ type HawkEvent = {
6+ payload : {
7+ [ key : string ] : any
8+ }
9+ }
10+
11+ type HawkEventRepetition = {
12+ payload : {
13+ [ key : string ] : any
14+ }
15+ delta : string ;
16+ }
17+
18+ /**
19+ * One of the features of the events is that their repetition is the difference
20+ * between the original, which greatly optimizes storage. So we need to restore
21+ * the original repetition payload using the very first event and its difference
22+ * between its repetition
23+ *
24+ * @param originalEvent - the very first event we received
25+ * @param repetition - the difference with its repetition, for the repetition we want to display
26+ * @returns fully assembled payload of the current repetition
27+ */
28+ export function repetitionAssembler ( originalEvent : Object , repetition : { [ key : string ] : any } ) : any {
29+ const customizer = ( originalParam : any , repetitionParam : any ) : any => {
30+ if ( repetitionParam === null ) {
31+ return originalParam ;
32+ }
33+
34+
35+ if ( typeof repetitionParam === 'object' && typeof originalParam === 'object' ) {
36+ /**
37+ * If original event has null but repetition has some value, we need to return repetition value
38+ */
39+ if ( originalParam === null ) {
40+ return repetitionParam ;
41+ /**
42+ * Otherwise, we need to recursively merge original and repetition values
43+ */
44+ } else {
45+ return repetitionAssembler ( originalParam , repetitionParam ) ;
46+ }
47+ }
48+
49+ return repetitionParam ;
50+ } ;
51+
52+ return mergeWith ( cloneDeep ( originalEvent ) , cloneDeep ( repetition ) , customizer ) ;
53+ }
54+
55+
56+ /**
57+ * Helps to merge original event and repetition due to delta format,
58+ * in case of old delta format, we need to patch the payload
59+ * in case of new delta format, we need to assemble the payload
60+ *
61+ * @param originalEvent {HawkEvent} - The original event
62+ * @param repetition {HawkEventRepetition} - The repetition to process
63+ * @returns {HawkEvent } Updated event with processed repetition payload
64+ */
65+ export function composeFullRepetitionEvent ( originalEvent : HawkEvent , repetition : HawkEventRepetition | undefined ) : HawkEvent {
66+
67+ console . log ( 'originalEvent' , originalEvent ) ;
68+ console . log ( 'repetition' , repetition ) ;
69+
70+ /**
71+ * Make a deep copy of the original event, because we need to avoid mutating the original event
72+ */
73+ const event = cloneDeep ( originalEvent ) ;
74+
75+ if ( ! repetition ) {
76+ return event ;
77+ }
78+
79+ /**
80+ * New delta format (repetition.delta is not null)
81+ */
82+ if ( repetition . delta ) {
83+ event . payload = patch ( {
84+ left : event . payload ,
85+ delta : JSON . parse ( repetition . delta )
86+ } ) ;
87+
88+ return event ;
89+ }
90+
91+ /**
92+ * New delta format (repetition.payload is null) and repetition.delta is null (there is no delta between original and repetition)
93+ */
94+ if ( ! repetition . payload ) {
95+ return event ;
96+ }
97+
98+ /**
99+ * Old delta format (repetition.payload is not null)
100+ * @todo remove after July 5 2025
101+ */
102+ event . payload = repetitionAssembler ( event . payload , repetition . payload ) ;
103+
104+ return event ;
105+ }
106+
0 commit comments