Skip to content

Commit

Permalink
chore: keepAliveTimeoutCache changed to timeFrameTimeoutCache (#14)
Browse files Browse the repository at this point in the history
* chore: keepAliveTimeoutCache changed to timeFrameTimeoutCache

Co-authored-by: Carmelo Badalamenti <[email protected]>
  • Loading branch information
Cadienvan and rollsappletree authored Jan 16, 2023
1 parent 9a57d94 commit 821ffe8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"@jointly/cache-candidate-plugin-base": "^1.0.0"
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx}": [
Expand All @@ -64,7 +65,6 @@
"license": "MIT",
"author": "Jointly <[email protected]>",
"dependencies": {
"@jointly/cache-candidate-plugin-base": "^1.0.0",
"hook-fn": "^1.0.3"
}
}
36 changes: 18 additions & 18 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CacheCandidateOptions,
DataCacheRecordNotFound,
Events,
KeepAliveCache,
TimeFrameTimeoutCache,
RunningQueryCache,
RunningQueryRecordNotFound,
TimeFrameCache
Expand Down Expand Up @@ -123,7 +123,7 @@ async function handleResult({
executionStart,
options,
timeframeCache,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
args,
HookPayload
}: {
Expand All @@ -133,7 +133,7 @@ async function handleResult({
executionStart: number;
options: CacheCandidateOptions;
timeframeCache: TimeFrameCache;
keepAliveTimeoutCache: KeepAliveCache;
timeFrameTimeoutCache: TimeFrameTimeoutCache;
args: any[];
HookPayload: PluginPayload;
}): Promise<void> {
Expand Down Expand Up @@ -173,7 +173,7 @@ async function handleResult({
})
.finally(() => {
runningQueryCache.delete(key);
keepAliveTimeoutCache.set(
timeFrameTimeoutCache.set(
key,
setTimeout(() => {
deleteDataCacheRecord({ options, key, HookPayload });
Expand Down Expand Up @@ -245,17 +245,17 @@ function getExceedingAmountFromCandidateFunction(
exceedingAmount = options.requestsThreshold;
return exceedingAmount;
}
function refreshKeepAliveRecord({
keepAliveTimeoutCache,
function refreshTimeframeTimeoutCacheRecord({
timeFrameTimeoutCache,
key,
options
}: {
keepAliveTimeoutCache: KeepAliveCache;
timeFrameTimeoutCache: TimeFrameTimeoutCache;
key: string;
options: CacheCandidateOptions;
}) {
clearTimeout(keepAliveTimeoutCache.get(key));
keepAliveTimeoutCache.set(
clearTimeout(timeFrameTimeoutCache.get(key));
timeFrameTimeoutCache.set(
key,
setTimeout(() => {
options.cache.delete(key);
Expand All @@ -272,15 +272,15 @@ export function uniqid(length = 10) {
export async function letsCandidate({
options,
key,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
runningQueryCache,
timeframeCache,
args,
originalMethod
}: {
options: CacheCandidateOptions;
key: string;
keepAliveTimeoutCache: KeepAliveCache;
timeFrameTimeoutCache: TimeFrameTimeoutCache;
runningQueryCache: RunningQueryCache;
timeframeCache: TimeFrameCache;
args: any[];
Expand All @@ -289,7 +289,7 @@ export async function letsCandidate({
const HookPayload = {
options: { ...options, plugins: undefined },
key,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
runningQueryCache,
timeframeCache,
fnArgs: args
Expand All @@ -299,8 +299,8 @@ export async function letsCandidate({
const cachedData = await getDataCacheRecord({ options, key, HookPayload });
if (cachedData !== DataCacheRecordNotFound) {
if (options.keepAlive) {
refreshKeepAliveRecord({
keepAliveTimeoutCache,
refreshTimeframeTimeoutCacheRecord({
timeFrameTimeoutCache,
key,
options
});
Expand Down Expand Up @@ -351,7 +351,7 @@ export async function letsCandidate({
executionStart,
options,
timeframeCache,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
args,
HookPayload
});
Expand All @@ -367,7 +367,7 @@ export async function letsCandidate({
executionStart,
options,
timeframeCache,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
args,
HookPayload
})
Expand All @@ -386,7 +386,7 @@ export function getInitialState(_options: Partial<CacheCandidateOptions>) {
// Generate a uniqid
const uniqueIdentifier = uniqid();

const keepAliveTimeoutCache: KeepAliveCache = new Map();
const timeFrameTimeoutCache: TimeFrameTimeoutCache = new Map();

const options: CacheCandidateOptions = {
...CacheCandidateOptionsDefault,
Expand All @@ -397,7 +397,7 @@ export function getInitialState(_options: Partial<CacheCandidateOptions>) {
timeframeCache,
runningQueryCache,
uniqueIdentifier,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
options
};
}
8 changes: 4 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function CacheCandidate(_options: Partial<CacheCandidateOptions> = {}) {
timeframeCache,
runningQueryCache,
uniqueIdentifier,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
options
} = getInitialState(_options);

Expand All @@ -32,7 +32,7 @@ export function CacheCandidate(_options: Partial<CacheCandidateOptions> = {}) {
return letsCandidate({
options,
key: dataCacheKey,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
runningQueryCache,
timeframeCache,
args,
Expand All @@ -50,15 +50,15 @@ export function cacheCandidate(
timeframeCache,
runningQueryCache,
uniqueIdentifier,
keepAliveTimeoutCache,
timeFrameTimeoutCache,
options
} = getInitialState(_options);

return async (...args: any[]) =>
letsCandidate({
options,
key: getDataCacheKey([uniqueIdentifier, JSON.stringify(args)]),
keepAliveTimeoutCache,
timeFrameTimeoutCache,
runningQueryCache,
timeframeCache,
args,
Expand Down
2 changes: 1 addition & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface TimeFrameCacheRecord {

export type TimeFrameCache = Map<string, Array<TimeFrameCacheRecord>>;
export type RunningQueryCache = Map<string, Promise<any>>;
export type KeepAliveCache = Map<string, NodeJS.Timeout>;
export type TimeFrameTimeoutCache = Map<string, NodeJS.Timeout>;

export enum Events {
RUNNING_QUERY = 'RUNNING_QUERY',
Expand Down

0 comments on commit 821ffe8

Please sign in to comment.