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:
@@ -2,7 +2,7 @@ import { EJSON } from 'meteor/ejson';
|
|||||||
import createGraph from 'ngraph.graph';
|
import createGraph from 'ngraph.graph';
|
||||||
|
|
||||||
export default class CreatureComputation {
|
export default class CreatureComputation {
|
||||||
constructor(properties){
|
constructor(properties, creature){
|
||||||
// Set up fields
|
// Set up fields
|
||||||
this.originalPropsById = {};
|
this.originalPropsById = {};
|
||||||
this.propsById = {};
|
this.propsById = {};
|
||||||
@@ -11,6 +11,7 @@ export default class CreatureComputation {
|
|||||||
this.props = properties;
|
this.props = properties;
|
||||||
this.dependencyGraph = createGraph();
|
this.dependencyGraph = createGraph();
|
||||||
this.errors = [];
|
this.errors = [];
|
||||||
|
this.creature = creature;
|
||||||
|
|
||||||
// Store properties for easy access later
|
// Store properties for easy access later
|
||||||
properties.forEach(prop => {
|
properties.forEach(prop => {
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ function getCreature(creatureId){
|
|||||||
|
|
||||||
export function buildComputationFromProps(properties, creature){
|
export function buildComputationFromProps(properties, creature){
|
||||||
|
|
||||||
const computation = new CreatureComputation(properties);
|
const computation = new CreatureComputation(properties, creature);
|
||||||
// Dependency graph where edge(a, b) means a depends on b
|
// Dependency graph where edge(a, b) means a depends on b
|
||||||
// The graph includes all dependencies even of inactive properties
|
// The graph includes all dependencies even of inactive properties
|
||||||
// such that any properties changing without changing their dependencies
|
// such that any properties changing without changing their dependencies
|
||||||
|
|||||||
@@ -1,10 +1,31 @@
|
|||||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||||
|
import { EJSON } from 'meteor/ejson';
|
||||||
|
|
||||||
export default function writeScope(creatureId, scope){
|
export default function writeScope(creatureId, computation) {
|
||||||
// Remove large properties that aren't likely to be accessed
|
const scope = computation.scope;
|
||||||
|
const variables = computation.creature.variables || {};
|
||||||
|
let $set;
|
||||||
for (const key in scope){
|
for (const key in scope){
|
||||||
|
// Remove large properties that aren't likely to be accessed
|
||||||
delete scope[key].parent;
|
delete scope[key].parent;
|
||||||
delete scope[key].ancestors;
|
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}});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export default function computeCreature(creatureId){
|
|||||||
try {
|
try {
|
||||||
computeCreatureComputation(computation);
|
computeCreatureComputation(computation);
|
||||||
writeAlteredProperties(computation);
|
writeAlteredProperties(computation);
|
||||||
writeScope(creatureId, computation.scope);
|
writeScope(creatureId, computation);
|
||||||
} catch (e){
|
} catch (e){
|
||||||
const errorText = e.reason || e.message || e.toString();
|
const errorText = e.reason || e.message || e.toString();
|
||||||
computation.errors.push({
|
computation.errors.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user