Skip to content

Commit

Permalink
refactor(context): move appStorage to the function scope
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed May 23, 2023
1 parent a9b9e1b commit 30bf6f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
44 changes: 22 additions & 22 deletions src/storage/context.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
import { prefixStorage, Storage, createStorage } from "unstorage";
import { prefixStorage, createStorage } from "unstorage";
import type { CreateStorageOptions } from "unstorage";

var appStorage: Storage;

var ContextStore = function (args: {
app: string;
options: CreateStorageOptions;
}) {
let appStorage: Storage;

const storage = createStorage(args.options);

appStorage = prefixStorage(storage, args.app);
this.appStorage = prefixStorage(storage, args.app);
};

ContextStore.prototype.open = () => {};

ContextStore.prototype.close = () => {};

ContextStore.prototype.get = (scope: string, key: string | string[]) => {
ContextStore.prototype.get = function (scope: string, key: string | string[]) {
if (Array.isArray(key)) {
return Promise.all(key.map((el) => appStorage.getItem(scope + ":" + el)));
return Promise.all(
key.map((el) => this.appStorage.getItem(scope + ":" + el))
);
}

return appStorage.getItem(scope + ":" + key);
return this.appStorage.getItem(scope + ":" + key);
};

ContextStore.prototype.set = (
ContextStore.prototype.set = function (
scope: string,
key: string | string[],
value: string | string[]
) => {
) {
if (Array.isArray(key)) {
const values = [...value];
return Promise.all(
key.map((el, index) =>
appStorage.setItem(scope + ":" + el, values[index] || null)
this.appStorage.setItem(scope + ":" + el, values[index] || null)
)
);
}

return appStorage.setItem(scope + ":" + key, value);
return this.appStorage.setItem(scope + ":" + key, value);
};

ContextStore.prototype.keys = (scope: string) =>
appStorage
ContextStore.prototype.keys = function (scope: string) {
return this.appStorage
.getKeys(scope)
.then((result) => result.map((el) => el.split(":")[1]));
};

ContextStore.prototype.delete = (scope: string) =>
appStorage
ContextStore.prototype.delete = function (scope: string) {
return this.appStorage
.getKeys(scope)
.then((result) =>
Promise.all(result.map((key) => appStorage.removeItem(key)))
Promise.all(result.map((key) => this.appStorage.removeItem(key)))
);
};

ContextStore.prototype.open = () => {};

ContextStore.prototype.close = () => {};

ContextStore.prototype.clean = (activeNodes) => {};

Expand Down
4 changes: 2 additions & 2 deletions src/storage/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
import { prefixStorage, Storage, createStorage } from "unstorage";
import type { CreateStorageOptions } from "unstorage";
import { prefixStorage, createStorage } from "unstorage";
import type { CreateStorageOptions, Storage } from "unstorage";

var appStorage: Storage;
var libraryStorage: Storage;
Expand Down

0 comments on commit 30bf6f9

Please sign in to comment.