fix(package): project setup

This commit is contained in:
2018-07-12 23:52:06 +02:00
parent c2740ca25b
commit 073d792497
7 changed files with 705 additions and 46 deletions

View File

@ -20,9 +20,12 @@ export class CacheManager {
}
stillValid(identifierArg: string): boolean {
if (this.cacheExists(identifierArg) && this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
this._cacheMap[identifierArg].timer.timeInMilliseconds
)) {
if (
this.cacheExists(identifierArg) &&
this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
this._cacheMap[identifierArg].timer.timeInMilliseconds
)
) {
return true;
}
return false;
@ -37,21 +40,23 @@ export class CacheManager {
};
this._cacheMap[identifierArg].timer.start();
this._cacheMap[identifierArg].timer.completed.then(() => {
this.deleteCache(identifierArg)
this.deleteCache(identifierArg);
});
}
/**
* waits for the cache to be ready
*/
async waitForCacheReady (identifierArg: string) {
await this._cacheMap[identifierArg].cachedDeferred.promise
async waitForCacheReady(identifierArg: string) {
await this._cacheMap[identifierArg].cachedDeferred.promise;
return true;
}
setCache(identifierArg: string, cachedObjectArg: any, validForArg = 1000) {
if(!this.cacheExists(identifierArg)) {
console.log(new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`));
if (!this.cacheExists(identifierArg)) {
console.log(
new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`)
);
}
this._cacheMap[identifierArg].cachedObject = cachedObjectArg;
this._cacheMap[identifierArg].cachedDeferred.resolve();

View File

@ -7,13 +7,17 @@ export class SmartCache {
async cacheReturn(asyncCachedFuncArg: () => Promise<any>, cacheDuration: number = 5000) {
let callStack: string = new plugins.smarterror.SmartError('').cleanFullStack;
let callHash = plugins.nodehash.sha256FromStringSync(callStack);
console.log(callHash);
if(this._cacheManager.cacheExists(callHash) && await this._cacheManager.waitForCacheReady(callHash) && this._cacheManager.stillValid(callHash)) {
if (
this._cacheManager.cacheExists(callHash) &&
(await this._cacheManager.waitForCacheReady(callHash)) &&
this._cacheManager.stillValid(callHash)
) {
return this._cacheManager.getCache(callHash).cachedObject;
} else {
this._cacheManager.announceCache(callHash, cacheDuration);
let newCachedObject = await asyncCachedFuncArg()
let newCachedObject = await asyncCachedFuncArg();
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
return newCachedObject;
}