npm package: @gov.nasa.jpl.honeycomb/model-cache
Description: Class to cache and clone three.js and javascript objects to save processing time.
Latest version: 0.0.6

# model-cache

An optionally asynchronous class for cache object instances including three.js objects so they can be retrieved without duplicating the objects.

!> Note that geometry and material instances are not cloned by default so they should be changed before modifying.

<!--{package-dependencies ./package.json}-->

# Use

## Caching async model loads

Creates a copy of the model when the asynchronous load resolves.

```js
import { ModelCache } from '@gov.nasa.jpl.honeycomb/model-cache';

const cache = new ModelCache();

async function getModel(url) {
    if (cache.has(url)) {
        return cache.getClone(url);
    } else {
        const modelPromise = new Promise(resolve => loader.load(res => resolve(res.scene)));
        cache.add(url, modelPromise);
        return modelPromise;
    }
}
```

## Caching created models

```js
import { ModelCache, hash } from '@gov.nasa.jpl.honeycomb/model-cache';

const cache = new ModelCache();

function createArc(length, angle) {
    const str = hash(length, angle);
    if (cache.has(str)) {
        return cache.getClone(str);
    } else {
        const arc = new LineArc(length, angle);
        cache.add(str, arc);
        return arc;
    }
}
```

# API

<!-- START_AUTOGENERATED_DOCS -->
## Functions
### hash<a name="hash"></a>

```js
hash( args : ...any ) : String
```

Converts the given objects into a hash.

## ModelCache

Cache for three.js models and objects

### add<a name="ModelCache#add"></a>

```js
add( hash : String, model : Object | Promise<Object> ) : void
```

Adds the given object to the cache indexed by `hash`.

### has<a name="ModelCache#has"></a>

```js
has( hash : String ) : Boolean
```

Returns whether or not the given hash exists in the cache.

### isPending<a name="ModelCache#isPending"></a>

```js
isPending( hash : String ) : Boolean
```

Returns whether the object indexed by the given hash is pending
a promise that hasn't resolved yet.

### delete<a name="ModelCache#delete"></a>

```js
delete( hash : String ) : Object | Promise<Object>
```

Removes the object at the given hash and returns the deleted object.

### get<a name="ModelCache#get"></a>

```js
get( hash : String ) : Object | Promise<Object>
```

Returns the original object added into the cache.

### getClone<a name="ModelCache#getClone"></a>

```js
getClone( hash : String ) : Object | Promise<Object>
```

Returns a clone of the object indexed by the given hash or a promise
that will resolve with a copy.

### markUnused<a name="ModelCache#markUnused"></a>

```js
markUnused(  ) : void
```

Increments the amount of times a model has been left unused by one.
If a model has never been marked unused then it is set to 1. Every
time a model is retrieved using [getClone](#ModelCache#getClone)
the unused counter is reset.

### cullUnused<a name="ModelCache#cullUnused"></a>

```js
cullUnused( unusedCount : Number = 1 ) : void
```

Removes any cached item that has been marked as unused >= `count`
number of times. The number of models removed is returned.

### clear<a name="ModelCache#clear"></a>

```js
clear(  ) : void
```

Clears the cache entirely. The number of models removed is returned.


<!-- END_AUTOGENERATED_DOCS -->