UNPKG

@bitbybit-dev/jscad-worker

Version:

Bit By Bit Developers JSCAD Based CAD Library to Program Geometry Via WebWorker

92 lines (91 loc) 3.14 kB
export class CacheHelper { constructor() { this.hashesFromPreviousRun = {}; this.usedHashes = {}; this.argCache = {}; } cleanAllCache() { const usedHashKeys = Object.keys(this.usedHashes); usedHashKeys.forEach(hash => { if (this.argCache[hash]) { try { this.argCache[hash].delete(); } // eslint-disable-next-line no-empty catch (_a) { } } }); this.argCache = {}; this.usedHashes = {}; this.hashesFromPreviousRun = {}; } cacheOp(args, cacheMiss) { let toReturn = null; const curHash = this.computeHash(args); this.usedHashes[curHash] = curHash; this.hashesFromPreviousRun[curHash] = curHash; const check = this.checkCache(curHash); if (check) { // TODO I need to check if and why cloning is required. // toReturn = new this.occ.TopoDS_Shape(check); toReturn = check; toReturn.hash = check.hash; } else { toReturn = cacheMiss(); toReturn.hash = curHash; this.addToCache(curHash, toReturn); } return toReturn; } /** Returns the cached object if it exists, or null otherwise. */ checkCache(hash) { return this.argCache[hash] || null; } /** Adds this `shape` to the cache, indexable by `hash`. */ addToCache(hash, shape) { const cacheShape = shape; cacheShape.hash = hash; this.argCache[hash] = cacheShape; return hash; } /** This function computes a 32-bit integer hash given a set of `arguments`. * If `raw` is true, the raw set of sanitized arguments will be returned instead. */ computeHash(args, raw) { let argsString = JSON.stringify(args); argsString = argsString.replace(/(\"ptr\"\:(-?[0-9]*?)\,)/g, ""); argsString = argsString.replace(/(\"ptr\"\:(-?[0-9]*))/g, ""); if (argsString.includes("ptr")) { console.error("YOU DONE MESSED UP YOUR REGEX."); } const hashString = Math.random.toString() + argsString; if (raw) { return hashString; } return this.stringToHash(hashString); } /** This function converts a string to a 32bit integer. */ stringToHash(str) { let hash = 0; if (str.length === 0) { return hash; } for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); // tslint:disable-next-line: no-bitwise hash = ((hash << 5) - hash) + char; // tslint:disable-next-line: no-bitwise hash = hash & hash; } return hash; } /** This function returns a version of the `inputArray` without the `objectToRemove`. */ remove(inputArray, objectToRemove) { return inputArray.filter((el) => { return el.hash !== objectToRemove.hash || el.ptr !== objectToRemove.ptr; }); } }