From 7cd4c1b5c4b6a894385955b2ffabde16ebda159e Mon Sep 17 00:00:00 2001 From: Michael Di Prisco Date: Thu, 12 Jan 2023 10:11:07 +0100 Subject: [PATCH 1/4] feat: added doctor + test script --- .gitignore | 4 +++- .npmignore | 3 ++- doctor-patient.js | 20 ++++++++++++++++++++ package.json | 1 + src/internal.ts | 14 ++++++++------ 5 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 doctor-patient.js diff --git a/.gitignore b/.gitignore index 0034ac9..8c728b4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ dist *.sln *.sw? -coverage \ No newline at end of file +coverage + +.clinic \ No newline at end of file diff --git a/.npmignore b/.npmignore index ffc1146..849f43c 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,5 @@ .git .gitignore demo -coverage \ No newline at end of file +coverage +.clinic \ No newline at end of file diff --git a/doctor-patient.js b/doctor-patient.js new file mode 100644 index 0000000..b9ad8a9 --- /dev/null +++ b/doctor-patient.js @@ -0,0 +1,20 @@ +const { cacheCandidate } = require('./dist/index.js'); +let counter = 0; +const mockFn = (step) => + new Promise((resolve) => { + counter += step; + resolve(counter); + }); +const wrappedMockFn = cacheCandidate(mockFn, { + requestsThreshold: 1, + ttl: 800, + keepAlive: false, +}); +(async () => { + let result = await wrappedMockFn(1); + console.log(result); // 1 + result = await wrappedMockFn(1); + console.log(result); // 1 + result = await wrappedMockFn(1); + console.log(result); // 1 +})(); \ No newline at end of file diff --git a/package.json b/package.json index 936a930..682bd31 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "ts-types": " tsc --emitDeclarationOnly --outDir dist", "check": "npm run prettier && npm run lint && npm test", "build": "npm run check && rimraf dist && NODE_ENV=production node esbuild.js && npm run ts-types", + "build-doctor-bubbleproof": "rimraf dist && NODE_ENV=development node esbuild.js && clinic bubbleproof -- node ./doctor-patient.js", "demo-test": "for file in demo/*.js; do node $file; done", "prettier": "prettier --write ./src", "lint": "eslint ./src --ext .ts", diff --git a/src/internal.ts b/src/internal.ts index 9948313..6043173 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -159,12 +159,14 @@ function handleResult({ }) .finally(() => { runningQueryCache.delete(key); - keepAliveTimeoutCache.set( - key, - setTimeout(() => { - deleteDataCacheRecord({ options, key }); - }, options.ttl) - ); + if (options.keepAlive) { + keepAliveTimeoutCache.set( + key, + setTimeout(() => { + deleteDataCacheRecord({ options, key }); + }, options.ttl) + ); + } }); } } From 34f02dc08a4028cce7cc80cfef70e059a9ce47fd Mon Sep 17 00:00:00 2001 From: Michael Di Prisco Date: Thu, 12 Jan 2023 10:37:25 +0100 Subject: [PATCH 2/4] chore: added npx to doctor so we don't have to install it before using it --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 682bd31..3c2e221 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "ts-types": " tsc --emitDeclarationOnly --outDir dist", "check": "npm run prettier && npm run lint && npm test", "build": "npm run check && rimraf dist && NODE_ENV=production node esbuild.js && npm run ts-types", - "build-doctor-bubbleproof": "rimraf dist && NODE_ENV=development node esbuild.js && clinic bubbleproof -- node ./doctor-patient.js", + "build-doctor-bubbleproof": "rimraf dist && NODE_ENV=development node esbuild.js && npx clinic bubbleproof -- node ./doctor-patient.js", "demo-test": "for file in demo/*.js; do node $file; done", "prettier": "prettier --write ./src", "lint": "eslint ./src --ext .ts", From 5ab092d69c98ffa4f358f3b9de921e5ea0ff3490 Mon Sep 17 00:00:00 2001 From: Michael Di Prisco Date: Thu, 12 Jan 2023 16:23:44 +0100 Subject: [PATCH 3/4] chore: added doctor-patient to npmignore --- .npmignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index 849f43c..6b1d3e7 100644 --- a/.npmignore +++ b/.npmignore @@ -2,4 +2,5 @@ .gitignore demo coverage -.clinic \ No newline at end of file +.clinic +doctor-patient.js \ No newline at end of file From 60c998a8a743f593c8aca859d4c63f63dde9cb71 Mon Sep 17 00:00:00 2001 From: Michael Di Prisco Date: Thu, 12 Jan 2023 17:35:39 +0100 Subject: [PATCH 4/4] chore: revert fix --- src/internal.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/internal.ts b/src/internal.ts index 6043173..9948313 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -159,14 +159,12 @@ function handleResult({ }) .finally(() => { runningQueryCache.delete(key); - if (options.keepAlive) { - keepAliveTimeoutCache.set( - key, - setTimeout(() => { - deleteDataCacheRecord({ options, key }); - }, options.ttl) - ); - } + keepAliveTimeoutCache.set( + key, + setTimeout(() => { + deleteDataCacheRecord({ options, key }); + }, options.ttl) + ); }); } }