# ResourceHandler

Class · category: Other

Source: https://github.com/playcanvas/engine/blob/b5b983982a9860d21e0c1dafb2f85f72e2c01afb/src/framework/handlers/handler.js#L57

Base class for ResourceHandlers used by [ResourceLoader](https://api.playcanvas.com/engine/classes/ResourceLoader.md). A handler is a collection of
[ResourceParser](https://api.playcanvas.com/engine/interfaces/ResourceParser.md)s for a single asset type; register parsers with [ResourceHandler#addParser](https://api.playcanvas.com/engine/classes/ResourceHandler.md#addparser)
and the base implementation selects the matching one to load and open the resource. A handler with a
single parser is the common (single-format) case.

## Constructors

### constructor

```ts
new ResourceHandler(app: AppBase, handlerType: string)
```

**Parameters**

- `app` ([`AppBase`](https://api.playcanvas.com/engine/classes/AppBase.md)): The running [AppBase](https://api.playcanvas.com/engine/classes/AppBase.md).
- `handlerType` (`string`): The type of the resource the handler handles.

## Properties

### _app

```ts
protected _app: AppBase
```

The running app instance.

### handlerType

```ts
handlerType: string = ''
```

Type of the resource the handler handles.

## Accessors

### app

```ts
get app(): AppBase
```

Gets the running [AppBase](https://api.playcanvas.com/engine/classes/AppBase.md) instance.

### maxRetries

```ts
get maxRetries(): number
set maxRetries(value: number)
```

Gets the number of times to retry a failed request for the resource.

### parsers

```ts
get parsers(): ResourceParser[]
```

Gets a read-only copy of the registered parsers.

## Methods

### addParser

```ts
addParser(parser: ResourceParser, decider?: any): void
```

Registers a [ResourceParser](https://api.playcanvas.com/engine/interfaces/ResourceParser.md) for this handler. Parsers are consulted newest-first: the most
recently added parser whose [ResourceParser#canParse](https://api.playcanvas.com/engine/interfaces/ResourceParser.md#canparse) returns true is selected. This lets a
later registration override a built-in parser for the same format.

Register parsers before starting loads for this handler's type - selection runs for both the
load and open phases, so changing the registry while loads are in flight can route them
inconsistently. Note that handlers that implement their own loading without consulting
registered parsers (for example cubemap or font) ignore registered parsers.

**Parameters**

- `parser` ([`ResourceParser`](https://api.playcanvas.com/engine/interfaces/ResourceParser.md)): The parser to register. Must implement `canParse(context)`.
- `decider` (`any`, optional): Removed. Previously a `(url, data) => boolean` selector; implement
  `canParse(context)` on the parser instead. If passed, it is ignored and logs a warning.

**Example**

```ts
app.loader.getHandler('model').addParser(new ObjModelParser(app.graphicsDevice));
```

### fetch

```ts
fetch(url: string | { load: string; original: string }, responseType: string, callback: ResourceHandlerCallback, asset?: Asset): void
```

Fetches a resource's raw data using this handler's retry settings, reusing pre-fetched
`asset.file.contents` when available. A convenience for a [ResourceParser](https://api.playcanvas.com/engine/interfaces/ResourceParser.md)'s `load` method,
so parsers don't reimplement the fetch boilerplate.

**Parameters**

- `url` (`string | { load: string; original: string }`): The resource URL, or a load/original
  structure.
- `responseType` (`string`): The Http.ResponseType to fetch as (for example
  `Http.ResponseType.ARRAY_BUFFER` for a binary format, or `Http.ResponseType.TEXT`).
- `callback` ([`ResourceHandlerCallback`](https://api.playcanvas.com/engine/types/ResourceHandlerCallback.md)): Called with `(err, data)` when the fetch completes.
- `asset` ([`Asset`](https://api.playcanvas.com/engine/classes/Asset.md), optional): The asset being loaded, used to reuse already-fetched contents.

### load

```ts
load(url: string | { load: string; original: string }, callback: ResourceHandlerCallback, asset?: Asset): void
```

Load a resource from a remote URL. When parsers are registered, the matching parser's `load` is
used; otherwise the base implementation does nothing (subclasses may override).

**Parameters**

- `url` (`string | { load: string; original: string }`): Either the URL of the resource to
  load or a structure containing the load URL (used for loading the resource) and the original
  URL (used for identifying the resource format; necessary when loading, for example, from
  a blob URL).
- `callback` ([`ResourceHandlerCallback`](https://api.playcanvas.com/engine/types/ResourceHandlerCallback.md)): The callback used when the resource is loaded or
  an error occurs.
- `asset` ([`Asset`](https://api.playcanvas.com/engine/classes/Asset.md), optional): Optional asset that is passed by ResourceLoader.

### open

```ts
open(url: string, data: any, asset?: Asset): any
```

The open function is passed the raw resource data. The handler can then process the data
into a format that can be used at runtime. When parsers are registered, the matching parser's
`open` is used (if it implements one); otherwise the base implementation simply returns the data.

**Parameters**

- `url` (`string`): The URL of the resource to open.
- `data` (`any`): The raw resource data passed by callback from [load](https://api.playcanvas.com/engine/classes/ResourceHandler.md#load).
- `asset` ([`Asset`](https://api.playcanvas.com/engine/classes/Asset.md), optional): Optional asset that is passed by ResourceLoader.

**Returns** `any`: The parsed resource data.

### patch

```ts
patch(asset: Asset, assets: AssetRegistry): void
```

The patch function performs any operations on a resource that requires a dependency on its
asset data or any other asset data. The base implementation does nothing.

**Parameters**

- `asset` ([`Asset`](https://api.playcanvas.com/engine/classes/Asset.md)): The asset to patch.
- `assets` ([`AssetRegistry`](https://api.playcanvas.com/engine/classes/AssetRegistry.md)): The asset registry.

### removeParser

```ts
removeParser(parser: ResourceParser): void
```

Removes a previously registered [ResourceParser](https://api.playcanvas.com/engine/interfaces/ResourceParser.md).

**Parameters**

- `parser` ([`ResourceParser`](https://api.playcanvas.com/engine/interfaces/ResourceParser.md)): The parser to remove.
