Skip to content

Commit

Permalink
fix(context): add callbacks for async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed May 24, 2023
1 parent 2f41a8b commit f9e0ba6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 4 additions & 13 deletions playground/package-lock.json

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

3 changes: 1 addition & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
"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",
"ts-node": "^10.9.1",
"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",
Expand Down
8 changes: 8 additions & 0 deletions playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ const settings: LocalSettings = {
},
],
},

// logging: {
// console: {
// level: "trace",
// audit: false,
// metrics: false,
// },
// },
};

const app = express();
Expand Down
69 changes: 47 additions & 22 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit f9e0ba6

Please sign in to comment.