Fixed massive writes to creature.variables on calc

Now only writes changed variables, preventing oplog from being
polluted with massive updates
This commit is contained in:
Stefan Zermatten
2022-04-25 13:57:39 +02:00
parent 33f60c2c6d
commit ea32c54f57
4 changed files with 28 additions and 6 deletions

View File

@@ -1,10 +1,31 @@
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import { EJSON } from 'meteor/ejson';
export default function writeScope(creatureId, scope){
// Remove large properties that aren't likely to be accessed
export default function writeScope(creatureId, computation) {
const scope = computation.scope;
const variables = computation.creature.variables || {};
let $set;
for (const key in scope){
// Remove large properties that aren't likely to be accessed
delete scope[key].parent;
delete scope[key].ancestors;
// Remove empty keys
for (const subKey in scope[key]) {
if (scope[key][subKey] === undefined) {
delete scope[key][subKey]
}
}
// Only update changed fields
if (!EJSON.equals(variables[key], scope[key])) {
if (!$set) $set = {};
// Set the changed key in the creature variables
$set[`variables.${key}`] = scope[key];
}
}
if ($set) {
Creatures.update(creatureId, {$set});
}
Creatures.update(creatureId, {$set: {variables: scope}});
}