UNPKG

ecspresso

Version:

A minimal Entity-Component-System library for typescript and javascript.

62 lines (61 loc) 2.61 kB
export default class ResourceManager<ResourceTypes extends Record<string, any> = Record<string, any>> { private resources; private resourceFactories; private initializedResourceKeys; /** * Add a resource to the manager * @param label The resource key * @param resource The resource value or a factory function that returns the resource * @returns The resource manager instance for chaining */ add<K extends keyof ResourceTypes>(label: K, resource: ResourceTypes[K] | ((context?: any) => ResourceTypes[K] | Promise<ResourceTypes[K]>)): this; /** * Get a resource from the manager * @param label The resource key * @param context Optional context to pass to factory functions (usually the ECSpresso instance) * @returns The resource value * @throws Error if resource not found */ get<K extends keyof ResourceTypes>(label: K, context?: any): ResourceTypes[K]; /** * Check if a resource exists * @param label The resource key * @returns True if the resource exists */ has<K extends keyof ResourceTypes>(label: K): boolean; /** * Remove a resource * @param label The resource key * @returns True if the resource was removed */ remove<K extends keyof ResourceTypes>(label: K): boolean; /** * Get all resource keys * @returns Array of resource keys */ getKeys(): Array<string>; /** * Check if a resource needs to be initialized * @param label The resource key * @returns True if the resource needs initialization */ needsInitialization<K extends keyof ResourceTypes>(label: K): boolean; /** * Get all resource keys that need to be initialized * @returns Array of resource keys that need initialization */ getPendingInitializationKeys(): Array<string>; /** * Initialize a specific resource if it's a factory function * @param label The resource key * @param context Optional context to pass to factory functions * @returns Promise that resolves when the resource is initialized */ initializeResource<K extends keyof ResourceTypes>(label: K, context?: any): Promise<void>; /** * Initialize specific resources or all resources that haven't been initialized yet * @param keys Optional array of resource keys to initialize or optional context to pass to factory functions * @returns Promise that resolves when the specified resources are initialized */ initializeResources<K extends keyof ResourceTypes>(context?: any, ...keys: K[]): Promise<void>; }