Some optimizations

This commit is contained in:
2026-10-10 20:36:00 -03:00
parent bf91450a9d
commit afe6e03d4c
3 changed files with 78 additions and 26 deletions
+37 -7
View File
@@ -94,6 +94,14 @@ export class Signal<T> {
let running_mod = "";
// a mod's script, imported from url. engine_access lets it import bworld/engine
// a component a block lists, with its params from the block's json
export interface BlockComponentOf {
mod: string;
id: string;
component: BlockComponent;
params: unknown;
}
interface ScriptSource {
mod: string;
version: string;
@@ -434,14 +442,34 @@ export class ModRuntime {
}
// components on a block, with their params from its json
components_of(block_id: string): { mod: string; id: string; component: BlockComponent; params: unknown }[] {
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
// components that only have shared rules have nothing here
return Object.entries(block?.components ?? {}).flatMap(([id, params]) => {
const registered = this.block_components.get(id);
return registered ? [{ ...registered, id, params }] : [];
});
// worked out once per block, blocks and components don't change after loading. shared, so callers don't change it
components_of(block_id: string): readonly BlockComponentOf[] {
let components = this.#components_of.get(block_id);
if (!components) {
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
// components that only have shared rules have nothing here
components = Object.entries(block?.components ?? {}).flatMap(([id, params]) => {
const registered = this.block_components.get(id);
return registered ? [{ ...registered, id, params }] : [];
});
this.#components_of.set(block_id, components);
}
return components;
}
#components_of = new Map<string, BlockComponentOf[]>();
// the ones with on_tick or on_second, for ticking blocks every tick. cached like components_of
ticking_components_of(block_id: string): readonly BlockComponentOf[] {
let components = this.#ticking_components_of.get(block_id);
if (!components) {
components = this.components_of(block_id).filter(({ component }) =>
component.on_tick || component.on_second
);
this.#ticking_components_of.set(block_id, components);
}
return components;
}
#ticking_components_of = new Map<string, BlockComponentOf[]>();
// components on an item, with their params from its json
item_components_of(item_id: string): { mod: string; id: string; component: ItemComponent; params: unknown }[] {
@@ -558,6 +586,8 @@ export class ModRuntime {
throw new ModLoadError(mod, `component ${id} is registered twice`);
}
this.block_components.set(id, { mod, component: component as BlockComponent });
this.#components_of.clear();
this.#ticking_components_of.clear();
},
register_item: (id, component) => {
setup_only("components");