6
6
StreamReferences ,
7
7
} from "@models/stream" ;
8
8
import { LIST_STATUS } from "@models/list" ;
9
- import { isEqual , isObject } from "@lib/utils" ;
9
+ import { isObject } from "@lib/utils" ;
10
10
import { Emitter , EventEmitter , StreamEvent } from "./emitter" ;
11
11
12
12
function outputDiffChunk < T extends Record < string , unknown > > (
@@ -31,6 +31,8 @@ function outputDiffChunk<T extends Record<string, unknown>>(
31
31
const output = chunks ;
32
32
chunks = [ ] ;
33
33
return emitter . emit ( StreamEvent . Data , output ) ;
34
+ } else {
35
+ return ;
34
36
}
35
37
}
36
38
return emitter . emit ( StreamEvent . Data , [ chunk ] ) ;
@@ -73,56 +75,53 @@ function isValidChunkSize(
73
75
chunksSize : ListStreamOptions [ "chunksSize" ] ,
74
76
) : boolean {
75
77
if ( ! chunksSize ) return true ;
76
- const x = String ( Math . sign ( chunksSize ) ) ;
77
- return x !== "-1" && x !== "NaN" ;
78
+ const sign = String ( Math . sign ( chunksSize ) ) ;
79
+ return sign !== "-1" && sign !== "NaN" ;
78
80
}
79
81
80
82
function isDataValid < T extends Record < string , unknown > > (
81
83
data : T ,
82
84
referenceProperty : ReferenceProperty < T > ,
83
- emitter : Emitter < T > ,
84
85
listType : "prevList" | "nextList" ,
85
- ) : boolean {
86
+ ) : { isValid : boolean ; message ?: string } {
86
87
if ( ! isObject ( data ) ) {
87
- emitter . emit (
88
- StreamEvent . Error ,
89
- new Error (
90
- `Your ${ listType } must only contain valid objects. Found ${ data } ` ,
91
- ) ,
92
- ) ;
93
- return false ;
88
+ return {
89
+ isValid : false ,
90
+ message : `Your ${ listType } must only contain valid objects. Found '${ data } '` ,
91
+ } ;
94
92
}
95
93
if ( ! Object . hasOwn ( data , referenceProperty ) ) {
96
- emitter . emit (
97
- StreamEvent . Error ,
98
- new Error (
99
- `The reference property ${ String ( referenceProperty ) } is not available in all the objects of your ${ listType } .` ,
100
- ) ,
101
- ) ;
102
- return false ;
94
+ return {
95
+ isValid : false ,
96
+ message : `The reference property '${ String ( referenceProperty ) } ' is not available in all the objects of your ${ listType } .` ,
97
+ } ;
103
98
}
104
- return true ;
99
+ return {
100
+ isValid : true ,
101
+ message : "" ,
102
+ } ;
105
103
}
106
104
107
105
function getDiffChunks < T extends Record < string , unknown > > (
108
- prevList : T [ ] ,
109
- nextList : T [ ] ,
106
+ prevList : T [ ] = [ ] ,
107
+ nextList : T [ ] = [ ] ,
110
108
referenceProperty : ReferenceProperty < T > ,
111
109
emitter : Emitter < T > ,
112
110
options : ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS ,
113
- ) {
111
+ ) : void {
114
112
if ( ! isValidChunkSize ( options ?. chunksSize ) ) {
115
113
return emitter . emit (
116
114
StreamEvent . Error ,
117
115
new Error (
118
- `The chunk size can't be negative. You entered the value ${ options . chunksSize } ` ,
116
+ `The chunk size can't be negative. You entered the value ' ${ options . chunksSize } ' ` ,
119
117
) ,
120
118
) ;
121
119
}
122
- if ( ! prevList && ! nextList ) {
123
- return [ ] ;
120
+ if ( prevList . length === 0 && nextList . length === 0 ) {
121
+ return emitter . emit ( StreamEvent . Finish ) ;
124
122
}
125
- if ( ! prevList ) {
123
+ const handleDiffChunk = outputDiffChunk < T > ( emitter ) ;
124
+ if ( prevList . length === 0 ) {
126
125
const nextDiff = formatSingleListStreamDiff (
127
126
nextList as T [ ] ,
128
127
false ,
@@ -136,11 +135,12 @@ function getDiffChunks<T extends Record<string, unknown>>(
136
135
) ;
137
136
emitter . emit ( StreamEvent . Finish ) ;
138
137
}
139
- return nextDiff ?. forEach ( ( data , i ) =>
138
+ nextDiff ?. forEach ( ( data , i ) =>
140
139
handleDiffChunk ( data , i === nextDiff . length - 1 , options ) ,
141
140
) ;
141
+ return emitter . emit ( StreamEvent . Finish ) ;
142
142
}
143
- if ( ! nextList ) {
143
+ if ( nextList . length === 0 ) {
144
144
const prevDiff = formatSingleListStreamDiff (
145
145
prevList as T [ ] ,
146
146
true ,
@@ -154,17 +154,22 @@ function getDiffChunks<T extends Record<string, unknown>>(
154
154
) ;
155
155
emitter . emit ( StreamEvent . Finish ) ;
156
156
}
157
- return prevDiff ?. forEach ( ( data , i ) =>
157
+ prevDiff ?. forEach ( ( data , i ) =>
158
158
handleDiffChunk ( data , i === prevDiff . length - 1 , options ) ,
159
159
) ;
160
+ return emitter . emit ( StreamEvent . Finish ) ;
160
161
}
161
162
const listsReferences : StreamReferences < T > = new Map ( ) ;
162
- const handleDiffChunk = outputDiffChunk < T > ( emitter ) ;
163
163
for ( let i = 0 ; i < prevList . length ; i ++ ) {
164
164
const data = prevList [ i ] ;
165
165
if ( data ) {
166
- const isValid = isDataValid ( data , referenceProperty , emitter , "prevList" ) ;
166
+ const { isValid, message } = isDataValid (
167
+ data ,
168
+ referenceProperty ,
169
+ "prevList" ,
170
+ ) ;
167
171
if ( ! isValid ) {
172
+ emitter . emit ( StreamEvent . Error , new Error ( message ) ) ;
168
173
emitter . emit ( StreamEvent . Finish ) ;
169
174
break ;
170
175
}
@@ -176,10 +181,15 @@ function getDiffChunks<T extends Record<string, unknown>>(
176
181
}
177
182
178
183
for ( let i = 0 ; i < nextList . length ; i ++ ) {
179
- const data = prevList [ i ] ;
184
+ const data = nextList [ i ] ;
180
185
if ( data ) {
181
- const isValid = isDataValid ( data , referenceProperty , emitter , "nextList" ) ;
186
+ const { isValid, message } = isDataValid (
187
+ data ,
188
+ referenceProperty ,
189
+ "nextList" ,
190
+ ) ;
182
191
if ( ! isValid ) {
192
+ emitter . emit ( StreamEvent . Error , new Error ( message ) ) ;
183
193
emitter . emit ( StreamEvent . Finish ) ;
184
194
break ;
185
195
}
@@ -207,10 +217,11 @@ function getDiffChunks<T extends Record<string, unknown>>(
207
217
208
218
let streamedChunks = 0 ;
209
219
const totalChunks = listsReferences . size ;
220
+
210
221
for ( const data of listsReferences . values ( ) ) {
211
222
streamedChunks ++ ;
212
223
const isLastChunk = totalChunks === streamedChunks ;
213
- if ( ! data . nextIndex ) {
224
+ if ( typeof data . nextIndex === "undefined" ) {
214
225
handleDiffChunk (
215
226
{
216
227
previousValue : prevList [ data . prevIndex ] ,
@@ -226,17 +237,17 @@ function getDiffChunks<T extends Record<string, unknown>>(
226
237
} else {
227
238
const prevData = prevList [ data . prevIndex ] ;
228
239
const nextData = nextList [ data . nextIndex ] ;
229
- const isDataEqual = isEqual ( prevData , nextData ) ;
230
- const indexDiff = data . prevIndex - data . nextIndex ;
240
+ const isDataEqual = JSON . stringify ( prevData ) === JSON . stringify ( nextData ) ;
241
+ const indexDiff = data . nextIndex - data . prevIndex ;
231
242
if ( isDataEqual ) {
232
243
if ( indexDiff === 0 ) {
233
244
handleDiffChunk (
234
245
{
235
246
previousValue : prevList [ data . prevIndex ] ,
236
247
currentValue : nextList [ data . nextIndex ] ,
237
- prevIndex : null ,
248
+ prevIndex : data . prevIndex ,
238
249
newIndex : data . nextIndex ,
239
- indexDiff : null ,
250
+ indexDiff : 0 ,
240
251
status : LIST_STATUS . EQUAL ,
241
252
} ,
242
253
isLastChunk ,
@@ -274,6 +285,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
274
285
}
275
286
}
276
287
}
288
+
277
289
return emitter . emit ( StreamEvent . Finish ) ;
278
290
}
279
291
0 commit comments