Replaced manual recompute calls with dirty flag settings
This commit is contained in:
@@ -8,7 +8,6 @@ import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/
|
||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
||||
import applyProperty from './applyProperty.js';
|
||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
||||
|
||||
const doAction = new ValidatedMethod({
|
||||
name: 'creatureProperties.doAction',
|
||||
@@ -77,9 +76,10 @@ const doAction = new ValidatedMethod({
|
||||
doActionWork({creature, targets, properties, ancestors, method: this, methodScope: scope});
|
||||
|
||||
// Recompute all involved creatures
|
||||
computeCreature(creature._id);
|
||||
targets.forEach(target => {
|
||||
computeCreature(target._id);
|
||||
Creatures.update({
|
||||
_id: { $in: [creature._id, ...targetIds] }
|
||||
}, {
|
||||
dirty: true
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -7,7 +7,6 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
|
||||
import { doActionWork } from '/imports/api/engine/actions/doAction.js';
|
||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
||||
import { CreatureLogSchema } from '/imports/api/creature/log/CreatureLogs.js';
|
||||
|
||||
const doAction = new ValidatedMethod({
|
||||
@@ -129,12 +128,12 @@ const doAction = new ValidatedMethod({
|
||||
}
|
||||
|
||||
// Do the action
|
||||
doActionWork({creature, targets, properties, ancestors, method: this, methodScope: scope, log});
|
||||
|
||||
// Recompute all involved creatures
|
||||
computeCreature(creature._id);
|
||||
targets.forEach(target => {
|
||||
computeCreature(target._id);
|
||||
doActionWork({ creature, targets, properties, ancestors, method: this, methodScope: scope, log });
|
||||
|
||||
Creatures.update({
|
||||
_id: { $in: [creature._id, ...targetIds] }
|
||||
}, {
|
||||
dirty: true
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -5,7 +5,6 @@ import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/ge
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import { CreatureLogSchema, insertCreatureLogWork } from '/imports/api/creature/log/CreatureLogs.js';
|
||||
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
|
||||
import computeCreature from '/imports/api/engine/computeCreature.js';
|
||||
import rollDice from '/imports/parser/rollDice.js';
|
||||
import numberToSignedString from '/imports/ui/utility/numberToSignedString.js';
|
||||
|
||||
@@ -32,9 +31,6 @@ const doCheck = new ValidatedMethod({
|
||||
|
||||
// Do the check
|
||||
doCheckWork({creature, prop, method: this, methodScope: scope});
|
||||
|
||||
// Recompute all involved creatures
|
||||
computeCreature(creature._id);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -42,7 +42,8 @@ function getProperties(creatureId) {
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
const creature = loadedCreatures.get(creatureId);
|
||||
const props = Array.from(creature.properties.values());
|
||||
return props;
|
||||
const cloneProps = EJSON.clone(props);
|
||||
return cloneProps
|
||||
}
|
||||
console.time(`Cache miss fetching from db: ${creatureId}`)
|
||||
const props = CreatureProperties.find({
|
||||
@@ -56,10 +57,19 @@ function getProperties(creatureId) {
|
||||
return props;
|
||||
}
|
||||
|
||||
function getCreature(creatureId){
|
||||
return Creatures.findOne(creatureId, {
|
||||
function getCreature(creatureId) {
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
const loadedCreature = loadedCreatures.get(creatureId);
|
||||
const creature = loadedCreature.creatures.get(creatureId);
|
||||
if (creature) return creature;
|
||||
}
|
||||
console.time(`Cache miss on Creature: ${creatureId}`);
|
||||
const creature = Creatures.findOne(creatureId, {
|
||||
denormalizedStats: 1,
|
||||
variables: 1,
|
||||
});
|
||||
console.timeEnd(`Cache miss on Creature: ${creatureId}`);
|
||||
return creature;
|
||||
}
|
||||
|
||||
export function buildComputationFromProps(properties, creature){
|
||||
@@ -91,6 +101,8 @@ export function buildComputationFromProps(properties, creature){
|
||||
|
||||
// Process the properties one by one
|
||||
properties.forEach(prop => {
|
||||
// The prop has been processed, it's no longer dirty
|
||||
delete prop.dirty;
|
||||
|
||||
const computedSchema = computedOnlySchemas[prop.type];
|
||||
removeSchemaFields([computedSchema, denormSchema], prop);
|
||||
|
||||
@@ -21,6 +21,7 @@ export default function writeAlteredProperties(computation){
|
||||
'deactivatedByAncestor',
|
||||
'deactivatedByToggle',
|
||||
'damage',
|
||||
'dirty',
|
||||
...schema.objectKeys(),
|
||||
];
|
||||
op = addChangedKeysToOp(op, keys, original, changed);
|
||||
|
||||
@@ -20,7 +20,6 @@ export default function writeScope(creatureId, computation) {
|
||||
// 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];
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import writeErrors from './computation/writeComputation/writeErrors.js';
|
||||
|
||||
export default function computeCreature(creatureId){
|
||||
if (Meteor.isClient) return;
|
||||
console.log('compute')
|
||||
const computation = buildCreatureComputation(creatureId);
|
||||
computeComputation(computation, creatureId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { debounce } from 'lodash';
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
|
||||
import computeCreature from './computeCreature';
|
||||
export const loadedCreatures = new Map(); // creatureId => {creature, properties, etc.}
|
||||
|
||||
export function loadCreature(creatureId, subscription) {
|
||||
@@ -35,27 +36,33 @@ class LoadedCreature {
|
||||
// the required documents
|
||||
const self = this;
|
||||
Tracker.nonreactive(() => {
|
||||
|
||||
self.subs = new Set([sub]);
|
||||
|
||||
const compute = debounce(Meteor.bindEnvironment(() => {
|
||||
computeCreature(creatureId);
|
||||
}), 100);
|
||||
|
||||
self.properties = new Map();
|
||||
// Observe all creature properties which are needed for computation
|
||||
self.propertyObserver = CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
removed: { $ne: true },
|
||||
}, {
|
||||
// sort: { order: 1 },
|
||||
sort: { order: 1 },
|
||||
fields: { icon: 0 },
|
||||
}).observeChanges({
|
||||
added(id, fields) {
|
||||
fields._id = id;
|
||||
return self.addProperty(fields)
|
||||
self.addProperty(fields);
|
||||
if (fields.dirty) compute();
|
||||
},
|
||||
changed(id, fields) {
|
||||
return self.changeProperty(id, fields);
|
||||
self.changeProperty(id, fields);
|
||||
if (fields.dirty) compute();
|
||||
},
|
||||
removed(id) {
|
||||
return self.removeProperty(id);
|
||||
self.removeProperty(id);
|
||||
compute();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -67,12 +74,14 @@ class LoadedCreature {
|
||||
added(id, fields) {
|
||||
fields._id = id;
|
||||
self.addCreature(fields)
|
||||
if (fields.dirty) compute();
|
||||
},
|
||||
changed(id, fields) {
|
||||
return self.changeCreature(id, fields);
|
||||
self.changeCreature(id, fields);
|
||||
if (fields.dirty) compute();
|
||||
},
|
||||
removed(id) {
|
||||
return self.removeCreature(id);
|
||||
self.removeCreature(id);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user