@gov.nasa.jpl.honeycomb/model-cache
v0.0.6
Published
Class to cache and clone three.js and javascript objects to save processing time.
Readme
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.
Use
Caching async model loads
Creates a copy of the model when the asynchronous load resolves.
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
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
Functions
hash
hash( args : ...any ) : StringConverts the given objects into a hash.
ModelCache
Cache for three.js models and objects
add
add( hash : String, model : Object | Promise<Object> ) : voidAdds the given object to the cache indexed by hash.
has
has( hash : String ) : BooleanReturns whether or not the given hash exists in the cache.
isPending
isPending( hash : String ) : BooleanReturns whether the object indexed by the given hash is pending a promise that hasn't resolved yet.
delete
delete( hash : String ) : Object | Promise<Object>Removes the object at the given hash and returns the deleted object.
get
get( hash : String ) : Object | Promise<Object>Returns the original object added into the cache.
getClone
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
markUnused( ) : voidIncrements 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 the unused counter is reset.
cullUnused
cullUnused( unusedCount : Number = 1 ) : voidRemoves any cached item that has been marked as unused >= count
number of times. The number of models removed is returned.
clear
clear( ) : voidClears the cache entirely. The number of models removed is returned.
