cqrs-eventdenormalizer
Advanced tools
Comparing version 1.16.55 to 1.16.56
@@ -401,15 +401,17 @@ var Definition = require('../definitionBase'), | ||
var self = this; | ||
var localFoundVms = {}; | ||
var localFoundVmsDict = {}; // Dictionary to have O(1) lookups | ||
var localFoundVms = []; | ||
if (this.isReplaying) { | ||
var replVms = _.map(this.replayingVms, function (vm) { | ||
// return vm.toJSON(); | ||
// We just read, so this is ok and faster! | ||
return vm.attributes; | ||
}); | ||
_.each(sift(query, replVms), function (vm) { | ||
localFoundVms[vm.id] = self.replayingVms[vm.id]; | ||
}); | ||
localFoundVms = _.reduce(this.replayingVms, function (result, vm) { | ||
// if (sift(query, [vm.toJSON()]).length > 0) { // We just read, so this is ok and faster! | ||
if (sift(query, [vm.attributes]).length > 0) { | ||
var newLen = result.push(vm); | ||
localFoundVmsDict[vm.id] = newLen - 1; | ||
} | ||
return result; | ||
}, []); | ||
} | ||
this.repository.find(query, queryOptions, function (err, vms) { | ||
this.repository.find(query, queryOptions, function (err, serverVms) { | ||
if (err) { | ||
@@ -419,31 +421,50 @@ debug(err); | ||
} | ||
vms.forEach(function (vm) { | ||
if (!localFoundVms[vm.id] && self.replayingVms[vm.id]) { | ||
localFoundVms[vm.id] = self.replayingVms[vm.id]; | ||
} | ||
if (localFoundVms[vm.id]) return; // optimize a bit | ||
var clonedInitValues = _.cloneDeep(self.modelInitValues); | ||
for (var prop in clonedInitValues) { | ||
if (!vm.has(prop)) { | ||
vm.set(prop, clonedInitValues[prop]); | ||
// We will now enhance the local replayingVms whith the results from the server | ||
// while keeping the local vms if it already exists. | ||
if (self.isReplaying) { | ||
// We preffer the local vms but add the server vm if no local available. | ||
var localAndServerVms = localFoundVms.concat(serverVms); // The order of this concat matters since we preffer the local ones. | ||
var resultDict = {}; // Dictionary to have O(1) lookups within the reduce loop. | ||
var uniqueLocalPrefferedVms = _.reduce(localAndServerVms, function (result, vm) { | ||
var isDeleted = !!self.replayingVmsToDelete[vm.id]; | ||
var alreadyInResult = !!resultDict[vm.id]; | ||
var localVm = null; | ||
var resultVm = null; | ||
if (isDeleted || alreadyInResult) return result; | ||
// No result found for query wihtin replayingVm since changes have been applied to the | ||
// replayingVm's already but from the server we retrieve a result. | ||
if (!localFoundVmsDict[vm.id] && self.replayingVms[vm.id]) { | ||
localVm = self.replayingVms[vm.id]; | ||
} | ||
} | ||
}); | ||
if (self.isReplaying) { | ||
var mergedVmsFromRepo = _.map(vms, function (vm) { | ||
return localFoundVms[vm.id] || vm; | ||
}); | ||
mergedVmsFromRepo = _.reject(mergedVmsFromRepo, function (vm) { | ||
return !!self.replayingVmsToDelete[vm.id]; | ||
}); | ||
mergedVmsFromRepo = _.union(mergedVmsFromRepo, _.values(localFoundVms)); | ||
mergedVmsFromRepo.forEach(function (vm) { | ||
// Preffer local vm if available | ||
localVm = localVm || localFoundVms[localFoundVmsDict[vm.id]]; | ||
if (localVm) { | ||
resultVm = localVm; | ||
} else { | ||
// Enhance server vm with initial values if not yet available | ||
var clonedInitValues = _.cloneDeep(self.modelInitValues); | ||
for (var prop in clonedInitValues) { | ||
if (!vm.has(prop)) { | ||
vm.set(prop, clonedInitValues[prop]); | ||
} | ||
} | ||
resultVm = vm; | ||
} | ||
result.push(resultVm); | ||
resultDict[resultVm.id] = true; | ||
if (!self.replayingVms[vm.id]) { | ||
self.replayingVms[vm.id] = vm; | ||
self.replayingVms[vm.id] = resultVm; | ||
} | ||
}); | ||
return callback(null, mergedVmsFromRepo); | ||
return result; | ||
}, []); | ||
return callback(null, uniqueLocalPrefferedVms); | ||
} | ||
callback(null, vms); | ||
callback(null, serverVms); | ||
}); | ||
@@ -450,0 +471,0 @@ }, |
{ | ||
"author": "adrai", | ||
"name": "cqrs-eventdenormalizer", | ||
"version": "1.16.55", | ||
"version": "1.16.56", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -0,1 +1,4 @@ | ||
## [v1.16.56](https://github.com/adrai/node-cqrs-eventdenormalizer/compare/v1.16.55...v1.16.56) | ||
- refactoring of findViewModel - only one loop [#85](https://github.com/adrai/node-cqrs-eventdenormalizer/pull/85) thanks to [Robin Fehr](https://github.com/robinfehr) | ||
## [v1.16.55](https://github.com/adrai/node-cqrs-eventdenormalizer/compare/v1.16.54...v1.16.55) | ||
@@ -2,0 +5,0 @@ - fix edge case during replay whe using query |
209453
4738