@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
198 lines • 7.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dynamicCachedFunctionWithCache = exports.dynamicCachedFunction = exports.cachePlusRetrieve = exports.cachePlusRemove = exports.cachePlusUpsert = void 0;
const __1 = require("../../");
const version_contract_db_json_1 = require("../../../version-contract-db.json");
const EXPIRATION_FOR_STRATEGY = 3600 * 24 * 100; // 100 days
// structure of cache
const cachePlusUpsert = async (preCacheId, key, data, expiration) => {
__1.PreCacheModule.getSlots(preCacheId).add(key, data);
try {
await __1.CacheStorageModule.addData(key, data, expiration);
}
catch (err) {
await __1.CacheStorageModule.updateData(key, data, expiration);
}
};
exports.cachePlusUpsert = cachePlusUpsert;
const cachePlusRemove = async (key) => {
await __1.CacheStorageModule.removeData(key);
};
exports.cachePlusRemove = cachePlusRemove;
const cachePlusRetrieve = async (preCacheId, key) => {
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
const preCached = __1.PreCacheModule.getSlots(preCacheId).get(key);
// console.log('preCached:')
// console.log(preCached)
if (preCached !== undefined) {
return preCached;
}
const data = await __1.CacheStorageModule.getData(key);
// console.log('data:')
// console.log(data)
return data;
// return (await CacheStorageModule.removeData(key)) as unknown as string
};
exports.cachePlusRetrieve = cachePlusRetrieve;
const getCacheKeyFromParameters = (parameters) => {
const serialized = (0, __1.serializeDataTool)([version_contract_db_json_1.contractDbVersion, ...parameters]);
return serialized;
};
const cacheIn = (cachedFunctionName, argsToIndex) => {
return async (cache) => {
const cacheKey = getCacheKeyFromParameters([
cachedFunctionName,
argsToIndex,
]);
// console.log('cache:')
// console.log(cache)
const cacheValue = (0, __1.serializeDataTool)(cache);
// console.log('cacheValue:')
// console.log(cacheValue)
await (0, exports.cachePlusUpsert)('strategyChain', cacheKey, cacheValue, EXPIRATION_FOR_STRATEGY);
};
};
const cacheOut = (cachedFunctionName, argsToIndex) => {
return async () => {
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
// console.log('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
const cacheKey = getCacheKeyFromParameters([
cachedFunctionName,
argsToIndex,
]);
// console.log('cacheKey:')
// console.log(cacheKey)
const cacheValueStr = await (0, exports.cachePlusRetrieve)('strategyChain', cacheKey);
// console.log('cacheValueStr:')
// console.log(cacheValueStr)
if (cacheValueStr === undefined) {
return undefined;
}
const cacheValue = (0, __1.deserializeDataTool)(cacheValueStr);
return cacheValue;
};
};
// A => Arguments
// U => Cache or Result
const cacheVia = (cachedFunctionName, argsToIndex) => {
return {
in: cacheIn(cachedFunctionName, argsToIndex),
out: cacheOut(cachedFunctionName, argsToIndex),
};
};
const recover = async (via) => {
return via.out();
};
// R => Result
// C => Cache
// U => Cache or Result
const resultAfterStore = async (via, resultCache, prevCached) => {
// console.log('---------------TO CACHE----------------')
// console.log('resultCache:')
// console.log((resultCache as unknown as any).cache.cachedStrategy)
// throw new Error('check')
const resultAsCache = resultCache;
if (resultAsCache.result !== undefined &&
resultAsCache.cache !== undefined) {
// console.log('resultAsCache.cache:')
// console.log(resultAsCache.cache)
const toGoCache = resultAsCache.cache;
if ((0, __1.serializeDataTool)(toGoCache) !== (0, __1.serializeDataTool)(prevCached)) {
await via.in(toGoCache);
}
return resultAsCache.result;
}
else {
const resultAsResult = resultCache;
const toGoCache = resultAsResult;
if ((0, __1.serializeDataTool)(toGoCache) !== (0, __1.serializeDataTool)(prevCached)) {
await via.in(toGoCache);
}
return resultAsResult;
}
};
// R => Result
// C => Cache
// U => Cache or Result
const resultAfterStoreWithCache = async (via, resultCache, prevCached) => {
// console.log('---------------TO CACHE----------------')
// console.log('resultCache.cache:')
// console.log(resultCache.cache)
const toGoCache = resultCache.cache;
if ((0, __1.serializeDataTool)(toGoCache) !== (0, __1.serializeDataTool)(prevCached)) {
await via.in(toGoCache);
}
return resultCache;
};
// R => Result
// C => Cache
// A => Arguments
const dynamicCached = async (functionCachedIdentifier, functionToBeCalled, argumentsForGettingData, argsToIndex) => {
const via = cacheVia(functionCachedIdentifier, argsToIndex);
const cached = await recover(via);
// console.log('---------------FROM CACHE----------------')
// console.log('cached:')
// console.log(cached)
// if (cached !== undefined) {
// throw new Error('check')
// }
const argumentsForGettingDataPlusCache = [
...argumentsForGettingData,
cached,
];
const resultCache = await functionToBeCalled(...argumentsForGettingDataPlusCache);
return resultAfterStore(via, resultCache, cached);
};
// R => Result
// C => Cache
// A => Arguments
const dynamicCachedWithCache = async (functionCachedIdentifier, functionToBeCalled, argumentsForGettingData, argsToIndex) => {
const via = cacheVia(functionCachedIdentifier, argsToIndex);
const cached = await recover(via);
// console.log('---------------FROM CACHE----------------')
// console.log('cached:')
// console.log(cached)
// if (cached !== undefined) {
// throw new Error('check')
// }
const argumentsForGettingDataPlusCache = [
...argumentsForGettingData,
cached,
];
const resultCache = await functionToBeCalled(...argumentsForGettingDataPlusCache);
return resultAfterStoreWithCache(via, resultCache, cached);
};
/*
const STRIP_COMMENTS =
/(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/gm
const ARGUMENT_NAMES = /([^\s,]+)/g
type FunctionType = (...args: any[]) => Promise<unknown>
function getParamNames<F extends FunctionType>(func: F): string[] {
const fnStr = func.toString().replace(STRIP_COMMENTS, '')
let result: string[] | null = fnStr
.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')'))
.match(ARGUMENT_NAMES)
if (result === null) result = [] as string[]
return result as string[]
}
*/
const dynamicCachedFunction = (functionToBeCached, argsToIndex) => {
const fName = functionToBeCached.name;
return async (...args) => {
return dynamicCached(fName, functionToBeCached, args, argsToIndex);
};
};
exports.dynamicCachedFunction = dynamicCachedFunction;
const dynamicCachedFunctionWithCache = (functionToBeCached, argsToIndex) => {
const fName = functionToBeCached.name;
return async (...args) => {
return dynamicCachedWithCache(fName, functionToBeCached, args, argsToIndex);
};
};
exports.dynamicCachedFunctionWithCache = dynamicCachedFunctionWithCache;
//# sourceMappingURL=cache-plus-helpers.tool.js.map