From a00292a523939efa835e292ac9abda89ae6d3cb3 Mon Sep 17 00:00:00 2001 From: Thaum Rystra <9525416+ThaumRystra@users.noreply.github.com> Date: Wed, 5 Jun 2024 17:29:33 +0200 Subject: [PATCH] Improved observer reuse for loaded creatures --- app/imports/api/creature/creatures/Creatures.ts | 4 ++-- app/imports/api/engine/loadCreatures.ts | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/imports/api/creature/creatures/Creatures.ts b/app/imports/api/creature/creatures/Creatures.ts index 0c0e766b..010d1407 100644 --- a/app/imports/api/creature/creatures/Creatures.ts +++ b/app/imports/api/creature/creatures/Creatures.ts @@ -13,8 +13,8 @@ export type Creature = Colored & Shared & { avatarPicture?: string, // Libraries - allowedLibraries: string[], - allowedLibraryCollections: string[], + allowedLibraries?: string[], + allowedLibraryCollections?: string[], // Stats that are computed and denormalized outside of recomputation denormalizedStats?: { diff --git a/app/imports/api/engine/loadCreatures.ts b/app/imports/api/engine/loadCreatures.ts index 21339b1a..67832026 100644 --- a/app/imports/api/engine/loadCreatures.ts +++ b/app/imports/api/engine/loadCreatures.ts @@ -7,12 +7,19 @@ import { getFilter } from '/imports/api/parenting/parentingFunctions'; const COMPUTE_DEBOUNCE_TIME = 100; // ms export const loadedCreatures: Map = new Map(); // creatureId => {creature, properties, etc.} -// TODO: migrate to nested sets + +// function logLoadedCreatures() { +// let creatureLoadString = ''; +// for (const [key, value] of loadedCreatures.entries()) { +// creatureLoadString += `${key}: ${value.subs.size}\n`; +// } +// console.log(creatureLoadString); +// } export function loadCreature(creatureId: string, subscription: Tracker.Computation) { if (!creatureId) throw 'creatureId is required'; let creature = loadedCreatures.get(creatureId); - if (!creature || !creature.subs.has(subscription)) { + if (!creature?.subs.has(subscription)) { subscription.onStop(() => { unloadCreature(creatureId, subscription); }); @@ -23,6 +30,7 @@ export function loadCreature(creatureId: string, subscription: Tracker.Computati creature = new LoadedCreature(subscription, creatureId); loadedCreatures.set(creatureId, creature); } + // logLoadedCreatures() } function unloadCreature(creatureId, subscription) { @@ -34,6 +42,7 @@ function unloadCreature(creatureId, subscription) { creature.stop(); loadedCreatures.delete(creatureId); } + // logLoadedCreatures() } export function getSingleProperty(creatureId: string, propertyId: string) { @@ -266,9 +275,6 @@ class LoadedCreature { // Observe all creature properties which are needed for computation self.propertyObserver = CreatureProperties.find({ 'root.id': creatureId, - removed: { $ne: true }, - }, { - sort: { left: 1 }, }).observeChanges({ added(id, fields) { fields._id = id;