Skip to content

Commit 6b6c71c

Browse files
authored
Merge pull request #22 from KBLLR/feature/dynamic-preset-loading
feat: Implement dynamic preset loading for improved efficiency
2 parents 5ae7553 + 2143ba9 commit 6b6c71c

File tree

8 files changed

+788
-146
lines changed

8 files changed

+788
-146
lines changed

package-lock.json

Lines changed: 533 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"format": "prettier --write .",
1313
"clean": "rimraf dist",
1414
"test:scene": "vite src/tests",
15-
"test": "npm run test:scene"
15+
"test": "npm run test:scene",
16+
"test:playwright": "start-server-and-test \"npm run test:scene\" http://localhost:5173 \"node src/tests/playwright.test.cjs\""
1617
},
1718
"dependencies": {
1819
"@lumaai/luma-web": "^0.2.2",
@@ -25,8 +26,10 @@
2526
"devDependencies": {
2627
"@types/three": "0.157.2",
2728
"eslint": "^8.51.0",
29+
"playwright": "^1.55.1",
2830
"prettier": "^3.0.0",
2931
"rimraf": "^5.0.0",
32+
"start-server-and-test": "^2.1.2",
3033
"vite": "^7.1.7",
3134
"vite-plugin-compression": "^0.5.1",
3235
"vite-plugin-env-compatible": "^2.0.1",
@@ -46,4 +49,3 @@
4649
},
4750
"prettier": {}
4851
}
49-

src/core/PresetManager.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @file PresetManager.js
3+
* @description This file contains the PresetManager class, which is responsible for dynamically loading presets.
4+
*/
5+
6+
import { PRESET_REGISTRY } from "@presets/index";
7+
8+
/**
9+
* @class PresetManager
10+
* @description Manages loading presets on demand to reduce initial memory footprint.
11+
*/
12+
export class PresetManager {
13+
/**
14+
* @property {Object.<string, {loader: Function, exportName: string}>} manifest
15+
* @description A mapping of preset types to their dynamic import loaders and export names.
16+
* @private
17+
*/
18+
#manifest;
19+
20+
/**
21+
* @property {Map<string, any>} cache
22+
* @description A cache to store loaded presets and avoid redundant fetches.
23+
* @private
24+
*/
25+
#cache;
26+
27+
/**
28+
* @constructor
29+
* @description Initializes the PresetManager with a manifest of available presets.
30+
*/
31+
constructor() {
32+
this.#cache = new Map();
33+
this.#manifest = {
34+
[PRESET_REGISTRY.CAMERA]: {
35+
loader: () => import("@presets/cameraPresets"),
36+
exportName: "CAMERA_PRESETS",
37+
},
38+
[PRESET_REGISTRY.CAMERA_MOVEMENT]: {
39+
loader: () => import("@presets/cameraPresets"),
40+
exportName: "CAMERA_MOVEMENT_PRESETS",
41+
},
42+
[PRESET_REGISTRY.FOG]: {
43+
loader: () => import("@presets/fogPresets"),
44+
exportName: "FOG_PRESETS",
45+
},
46+
};
47+
}
48+
49+
/**
50+
* @method getPresetNames
51+
* @description Retrieves the names of all available presets for a given type.
52+
* @param {string} type - The type of preset to retrieve (e.g., 'camera', 'fog').
53+
* @returns {Promise<string[]>} A promise that resolves to an array of preset names.
54+
*/
55+
async getPresetNames(type) {
56+
const manifestEntry = this.#manifest[type];
57+
if (!manifestEntry) {
58+
throw new Error(`Unknown preset type: ${type}`);
59+
}
60+
61+
const module = await manifestEntry.loader();
62+
const presetData = module[manifestEntry.exportName];
63+
return Object.keys(presetData);
64+
}
65+
66+
/**
67+
* @method loadPreset
68+
* @description Loads a specific preset by type and name.
69+
* @param {string} type - The type of preset to load.
70+
* @param {string} name - The name of the preset to load.
71+
* @returns {Promise<any>} A promise that resolves to the loaded preset data.
72+
*/
73+
async loadPreset(type, name) {
74+
const cacheKey = `${type}-${name}`;
75+
if (this.#cache.has(cacheKey)) {
76+
return this.#cache.get(cacheKey);
77+
}
78+
79+
const manifestEntry = this.#manifest[type];
80+
if (!manifestEntry) {
81+
throw new Error(`Unknown preset type: ${type}`);
82+
}
83+
84+
const module = await manifestEntry.loader();
85+
const presetData = module[manifestEntry.exportName];
86+
const preset = presetData[name];
87+
88+
if (!preset) {
89+
throw new Error(`Preset "${name}" not found for type "${type}"`);
90+
}
91+
92+
this.#cache.set(cacheKey, preset);
93+
return preset;
94+
}
95+
}

0 commit comments

Comments
 (0)