@jolly-pixel/asset
v4.0.0
Published
Platform-agnostic asset references and loading orchestration for browser and Node.js runtimes
Readme
💃 Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @jolly-pixel/asset
# or
$ yarn add @jolly-pixel/asset💡 About
An asset keeps the same identity when its file, URL, or revision changes.
Scenes and components store an AssetReference. An AssetCatalog resolves
that reference to an AssetRecord, which describes the current path and
revision.
flowchart TB
Scene["Scene / component"]
Reference["AssetReference<br/>id + expected kind"]
Coordinator["AssetCoordinator"]
Catalog["AssetCatalog"]
Record["AssetRecord<br/>path + revision"]
Loader["AssetLoader"]
Handle["AssetHandle<br/>asset value"]
Scene --> Reference
Reference --> Coordinator
Coordinator -->|"resolve"| Catalog
Catalog --> Record
Record -->|"path"| Loader
Loader -->|"runtime value"| Coordinator
Coordinator --> Handle
Handle -->|"synchronous access"| SceneChoose the path that matches your work:
- Game developers using
@jolly-pixel/runtimeshould start with using assets in a game. - Runtime, editor, and server maintainers should read runtime loading architecture and custom runtime integration.
The asset glossary defines the terms shared by these APIs.
👀 Usage example
Create a reference with one of the engine's shared asset types, then declare it as a scene dependency:
import { AssetReference } from "@jolly-pixel/asset";
import {
AssetTypes,
ModelRenderer,
Systems
} from "@jolly-pixel/engine";
const heroModel = new AssetReference(
"hero-model",
AssetTypes.model
);
class GameScene extends Systems.Scene {
constructor() {
super("game", {
assets: [heroModel]
});
}
override awake(): void {
this.world.createActor("hero")
.addComponent(ModelRenderer, {
asset: heroModel
});
}
}The engine's SceneManager loads scene.assets before activating the scene. The
catalog supplies the path for "hero-model"; gameplay code keeps the stable
reference. The game developer guide
shows the matching catalog and runtime setup.
📚 API
Core model
Start here. These types define persistent asset identity and catalog lookup.
AssetReference: the stable, typed value stored by scenes and components.AssetCatalog: the authoritative records for one project or session.
Supporting domain types
AssetId: validated stable identity.AssetKind: the validated persistent category of an asset.AssetType: the shared token connecting a persistent kind to its loaded value type.AssetRecord: path and revision metadata held by a catalog.AssetPath: a workspace-relative path split into directory, name, and extension.AssetRoom: the${kind}:${assetId}network room name of one asset.
Runtime orchestration
These APIs are primarily for runtime integrations, editor tools, and explicit dynamic loading.
AssetCoordinator: resolves references, starts loads, and owns loaded values.AssetLoaderandAssetLoaderRegistry: produce runtime values for registered asset types.AssetHandle: exposes one asset's runtime state.AssetLoadBatch: tracks one fixed loading operation.
The reference also covers package errors and the room-name and URL constants shared with server and collaboration packages.
✨ Contributors guide
If you are a developer looking to contribute to the project, you must first read the CONTRIBUTING guide.
Once you have finished your development, check that the tests (and linter) are still good by running the following script:
pnpm --filter @jolly-pixel/asset test
pnpm run lint[!CAUTION] In case you introduce a new feature or fix a bug, make sure to include tests for it as well.
📃 License
MIT
