@@ -6,7 +6,7 @@ const _merge = require("../../helper/merge");
6
6
7
7
const COMMON = require ( "./class.common.js" ) ;
8
8
9
- // const PENDING_CHANGE_EVENTS = new Set();
9
+ const PENDING_CHANGE_EVENTS = new Set ( ) ;
10
10
11
11
/**
12
12
* @description
@@ -130,6 +130,10 @@ module.exports = class COMPONENT extends COMMON {
130
130
} ) ;
131
131
132
132
changeStream . on ( "change" , ( event ) => {
133
+
134
+ // feedback
135
+ this . logger . trace ( "Change event triggerd" , event ) ;
136
+
133
137
// replace is used when updated via mongodb compass
134
138
// updated is used when called via api/`.update` method
135
139
if ( event . operationType === "replace" ) {
@@ -145,6 +149,19 @@ module.exports = class COMPONENT extends COMMON {
145
149
return ;
146
150
}
147
151
152
+ // when a change was initialized local, ignore changes from mongodb
153
+ if ( PENDING_CHANGE_EVENTS . has ( target . _id ) ) {
154
+
155
+ // feedback
156
+ this . logger . trace ( "Local change detected, ignore event from change stream" ) ;
157
+
158
+ // cleanup
159
+ PENDING_CHANGE_EVENTS . delete ( target . _id ) ;
160
+
161
+ return ;
162
+
163
+ }
164
+
148
165
// get original property descriptor
149
166
//let descriptor = Object.getOwnPropertyDescriptors(target);
150
167
//console.log("replace event", descriptor.config)
@@ -186,18 +203,17 @@ module.exports = class COMPONENT extends COMMON {
186
203
}
187
204
188
205
// when a change was initialized local, ignore changes from mongodb
189
- /*
190
206
if ( PENDING_CHANGE_EVENTS . has ( target . _id ) ) {
191
207
192
208
// feedback
193
- this.logger.verbose ("Local change detected, ignore event from change stream");
209
+ this . logger . trace ( "Local change detected, ignore event from change stream" ) ;
194
210
195
211
// cleanup
196
212
PENDING_CHANGE_EVENTS . delete ( target . _id ) ;
197
213
198
214
return ;
215
+
199
216
}
200
- */
201
217
202
218
// event now contain dot notation partial update
203
219
// to make things simpler, just fetch the doc and merge it
@@ -226,6 +242,7 @@ module.exports = class COMPONENT extends COMMON {
226
242
this . logger . verbose ( `$watch operation (${ event . operationType } ) not implemented!` ) ;
227
243
228
244
}
245
+
229
246
} ) ;
230
247
231
248
} catch ( err ) {
@@ -274,6 +291,9 @@ module.exports = class COMPONENT extends COMMON {
274
291
// override string with ObjectId, see #175
275
292
result . value . _id = new mongodb . ObjectId ( result . value . _id ) ;
276
293
294
+ // add id to pending change events
295
+ PENDING_CHANGE_EVENTS . add ( result . value . _id ) ;
296
+
277
297
this . collection . insertOne ( result . value , ( err , result ) => {
278
298
if ( err ) {
279
299
if ( err . code === 11000 && options . returnDuplicate ) {
@@ -296,8 +316,8 @@ module.exports = class COMPONENT extends COMMON {
296
316
*/
297
317
} ) ;
298
318
299
- // add id to pending change events
300
- // PENDING_CHANGE_EVENTS.add(item ._id);
319
+ // remove id when error occurs
320
+ PENDING_CHANGE_EVENTS . delete ( result . value . _id ) ;
301
321
302
322
if ( item ) {
303
323
resolve ( [ item ] ) ;
@@ -398,21 +418,24 @@ module.exports = class COMPONENT extends COMMON {
398
418
return obj . _id === _id ;
399
419
} ) ;
400
420
421
+ // add id to pending change events
422
+ PENDING_CHANGE_EVENTS . add ( target . _id ) ;
423
+
401
424
this . collection . deleteOne ( {
402
425
_id : new mongodb . ObjectId ( _id )
403
426
} , ( err , result ) => {
404
427
if ( err ) {
405
428
429
+ // remove id when error occurs
430
+ PENDING_CHANGE_EVENTS . delete ( result . value . _id ) ;
431
+
406
432
reject ( err ) ;
407
433
408
434
} else {
409
435
410
436
//if (result.n === 1 && result.ok === 1 && target) {
411
437
if ( result . acknowledged && result . deletedCount > 0 ) {
412
438
413
- // add id to pending change events
414
- //PENDING_CHANGE_EVENTS.add(target._id);
415
-
416
439
resolve ( [ target , result , _id ] ) ;
417
440
418
441
} else {
@@ -472,6 +495,9 @@ module.exports = class COMPONENT extends COMMON {
472
495
// _id is immutable. remove it
473
496
delete validation . value . _id ;
474
497
498
+ // add id to pending change events
499
+ PENDING_CHANGE_EVENTS . add ( target . _id ) ;
500
+
475
501
this . collection . findOneAndUpdate ( {
476
502
// casting problem, see #175
477
503
_id : new mongodb . ObjectId ( _id )
@@ -485,6 +511,9 @@ module.exports = class COMPONENT extends COMMON {
485
511
} , ( err ) => {
486
512
if ( err ) {
487
513
514
+ // remove id when error occurs
515
+ PENDING_CHANGE_EVENTS . delete ( target . _id ) ;
516
+
488
517
//console.log("4tpoiwrejtkwienrut", err)
489
518
reject ( err ) ;
490
519
@@ -502,9 +531,6 @@ module.exports = class COMPONENT extends COMMON {
502
531
// Umwandlung von object/string zu/von object/string
503
532
// muss in middlware erflogen!!!!!!!!!!!!!!
504
533
505
- // add id to pending change events
506
- //PENDING_CHANGE_EVENTS.add(target._id);
507
-
508
534
// TODO CHECK RESUTL!
509
535
// extend exisiting object in items array
510
536
//_extend(target, validation.value);
0 commit comments