Improved observer reuse for loaded creatures
This commit is contained in:
@@ -13,8 +13,8 @@ export type Creature = Colored & Shared & {
|
|||||||
avatarPicture?: string,
|
avatarPicture?: string,
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
allowedLibraries: string[],
|
allowedLibraries?: string[],
|
||||||
allowedLibraryCollections: string[],
|
allowedLibraryCollections?: string[],
|
||||||
|
|
||||||
// Stats that are computed and denormalized outside of recomputation
|
// Stats that are computed and denormalized outside of recomputation
|
||||||
denormalizedStats?: {
|
denormalizedStats?: {
|
||||||
|
|||||||
@@ -7,12 +7,19 @@ import { getFilter } from '/imports/api/parenting/parentingFunctions';
|
|||||||
|
|
||||||
const COMPUTE_DEBOUNCE_TIME = 100; // ms
|
const COMPUTE_DEBOUNCE_TIME = 100; // ms
|
||||||
export const loadedCreatures: Map<string, LoadedCreature> = new Map(); // creatureId => {creature, properties, etc.}
|
export const loadedCreatures: Map<string, LoadedCreature> = 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) {
|
export function loadCreature(creatureId: string, subscription: Tracker.Computation) {
|
||||||
if (!creatureId) throw 'creatureId is required';
|
if (!creatureId) throw 'creatureId is required';
|
||||||
let creature = loadedCreatures.get(creatureId);
|
let creature = loadedCreatures.get(creatureId);
|
||||||
if (!creature || !creature.subs.has(subscription)) {
|
if (!creature?.subs.has(subscription)) {
|
||||||
subscription.onStop(() => {
|
subscription.onStop(() => {
|
||||||
unloadCreature(creatureId, subscription);
|
unloadCreature(creatureId, subscription);
|
||||||
});
|
});
|
||||||
@@ -23,6 +30,7 @@ export function loadCreature(creatureId: string, subscription: Tracker.Computati
|
|||||||
creature = new LoadedCreature(subscription, creatureId);
|
creature = new LoadedCreature(subscription, creatureId);
|
||||||
loadedCreatures.set(creatureId, creature);
|
loadedCreatures.set(creatureId, creature);
|
||||||
}
|
}
|
||||||
|
// logLoadedCreatures()
|
||||||
}
|
}
|
||||||
|
|
||||||
function unloadCreature(creatureId, subscription) {
|
function unloadCreature(creatureId, subscription) {
|
||||||
@@ -34,6 +42,7 @@ function unloadCreature(creatureId, subscription) {
|
|||||||
creature.stop();
|
creature.stop();
|
||||||
loadedCreatures.delete(creatureId);
|
loadedCreatures.delete(creatureId);
|
||||||
}
|
}
|
||||||
|
// logLoadedCreatures()
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSingleProperty(creatureId: string, propertyId: string) {
|
export function getSingleProperty(creatureId: string, propertyId: string) {
|
||||||
@@ -266,9 +275,6 @@ class LoadedCreature {
|
|||||||
// Observe all creature properties which are needed for computation
|
// Observe all creature properties which are needed for computation
|
||||||
self.propertyObserver = CreatureProperties.find({
|
self.propertyObserver = CreatureProperties.find({
|
||||||
'root.id': creatureId,
|
'root.id': creatureId,
|
||||||
removed: { $ne: true },
|
|
||||||
}, {
|
|
||||||
sort: { left: 1 },
|
|
||||||
}).observeChanges({
|
}).observeChanges({
|
||||||
added(id, fields) {
|
added(id, fields) {
|
||||||
fields._id = id;
|
fields._id = id;
|
||||||
|
|||||||
Reference in New Issue
Block a user