From f9e0ba6f0276e45985924cb8d58ffb22910358c6 Mon Sep 17 00:00:00 2001 From: becem-gharbi <99251251+becem-gharbi@users.noreply.github.com> Date: Wed, 24 May 2023 10:18:43 +0100 Subject: [PATCH] fix(context): add callbacks for async operations --- package.json | 1 + playground/package-lock.json | 17 +++------ playground/package.json | 3 +- playground/src/index.ts | 8 +++++ src/context.ts | 69 ++++++++++++++++++++++++------------ 5 files changed, 61 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 90c8088..34ce074 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dist" ], "scripts": { + "dev": "cd playground && npm run dev", "build": "rimraf dist && tsup src/**/*.ts --format cjs,esm --dts", "release": "npm run build && changelogen --release && npm publish && git push --follow-tags" }, diff --git a/playground/package-lock.json b/playground/package-lock.json index 955844b..e87d619 100644 --- a/playground/package-lock.json +++ b/playground/package-lock.json @@ -9,7 +9,6 @@ "version": "0.1.5", "license": "MIT", "dependencies": { - "@bg-dev/node-red-unstorage": "^0.1.5", "dotenv": "^16.0.3", "express": "^4.18.2", "mongodb": "^5.5.0", @@ -18,7 +17,7 @@ }, "devDependencies": { "@types/express": "^4.17.17", - "@types/node": "^20.2.0", + "@types/node": "^20.2.3", "@types/node-red": "^1.3.1", "changelogen": "^0.5.3", "rimraf": "^5.0.1", @@ -1168,14 +1167,6 @@ "node": ">=6.9.0" } }, - "node_modules/@bg-dev/node-red-unstorage": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/@bg-dev/node-red-unstorage/-/node-red-unstorage-0.1.5.tgz", - "integrity": "sha512-G95+oCopbL1twAIfsfe7NKy9PT2WSiXbnmBY7lECVY2A9N0V80VoiQvhJT+HbtUYHBWwTyG+1ms1RZVsf41lrg==", - "dependencies": { - "unstorage": "^1.6.0" - } - }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -2130,9 +2121,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.2.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.2.0.tgz", - "integrity": "sha512-3iD2jaCCziTx04uudpJKwe39QxXgSUnpxXSvRQjRvHPxFQfmfP4NXIm/NURVeNlTCc+ru4WqjYGTmpXrW9uMlw==" + "version": "20.2.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.2.3.tgz", + "integrity": "sha512-pg9d0yC4rVNWQzX8U7xb4olIOFuuVL9za3bzMT2pu2SU0SNEi66i2qrvhE2qt0HvkhuCaWJu7pLNOt/Pj8BIrw==" }, "node_modules/@types/node-red": { "version": "1.3.1", diff --git a/playground/package.json b/playground/package.json index ca74be2..a7dc653 100644 --- a/playground/package.json +++ b/playground/package.json @@ -13,7 +13,7 @@ "license": "MIT", "devDependencies": { "@types/express": "^4.17.17", - "@types/node": "^20.2.0", + "@types/node": "^20.2.3", "@types/node-red": "^1.3.1", "changelogen": "^0.5.3", "rimraf": "^5.0.1", @@ -21,7 +21,6 @@ "typescript": "^5.0.4" }, "dependencies": { - "@bg-dev/node-red-unstorage": "^0.1.5", "dotenv": "^16.0.3", "express": "^4.18.2", "mongodb": "^5.5.0", diff --git a/playground/src/index.ts b/playground/src/index.ts index 0e24f28..6eef7ef 100644 --- a/playground/src/index.ts +++ b/playground/src/index.ts @@ -68,6 +68,14 @@ const settings: LocalSettings = { }, ], }, + + // logging: { + // console: { + // level: "trace", + // audit: false, + // metrics: false, + // }, + // }, }; const app = express(); diff --git a/src/context.ts b/src/context.ts index 81f30ec..69b5712 100644 --- a/src/context.ts +++ b/src/context.ts @@ -10,41 +10,66 @@ var ContextStore = function (args: { this.appStorage = prefixStorage(storage, args.app); }; -ContextStore.prototype.get = function (scope: string, key: string | string[]) { - if (Array.isArray(key)) { - return Promise.all( - key.map((el) => this.appStorage.getItem(scope + ":" + el)) - ); - } +ContextStore.prototype.get = function ( + scope: string, + key: string | string[], + cb: (error: Error, ...values: string[]) => void +) { + try { + if (Array.isArray(key)) { + Promise.all( + key.map((el) => this.appStorage.getItem(scope + ":" + el)) + ).then((result) => cb(undefined, ...result)); + } - return this.appStorage.getItem(scope + ":" + key); + this.appStorage + .getItem(scope + ":" + key) + .then((result) => cb(undefined, result)); + } catch (error) { + cb(error); + } }; ContextStore.prototype.set = function ( scope: string, key: string | string[], - value: string | string[] + value: string | string[], + cb: (error?: Error) => void ) { - if (Array.isArray(key)) { - const values = [...value]; - return Promise.all( - key.map((el, index) => - this.appStorage.setItem(scope + ":" + el, values[index] || null) - ) - ); - } + try { + if (Array.isArray(key)) { + const values = [...value]; - return this.appStorage.setItem(scope + ":" + key, value); + Promise.all( + key.map((el, index) => + this.appStorage.setItem(scope + ":" + el, values[index] || null) + ) + ).then(() => cb()); + } else { + this.appStorage.setItem(scope + ":" + key, value).then(() => cb()); + } + } catch (error) { + cb(error); + } }; -ContextStore.prototype.keys = function (scope: string) { - return this.appStorage - .getKeys(scope) - .then((result) => result.map((el) => el.split(":")[1])); +ContextStore.prototype.keys = function ( + scope: string, + cb: (error: Error, ...values: string[]) => void +) { + try { + this.appStorage.getKeys(scope).then((result) => { + const keys = result.map((el) => el.split(":")[1]); + + cb(undefined, keys); + }); + } catch (error) { + cb(error); + } }; ContextStore.prototype.delete = function (scope: string) { - return this.appStorage + this.appStorage .getKeys(scope) .then((result) => Promise.all(result.map((key) => this.appStorage.removeItem(key)))