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