Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed May 22, 2024
1 parent f4ede69 commit d964098
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .changeset/clean-pigs-guess.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"storage-box": minor
"storage-box": patch
---

fix: added `getall` to extract entry storage as a record/key-value object
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storage-box",
"version": "1.0.0-canary.3",
"version": "1.0.0-canary.4",
"description": "A memory-based key–value storage for javascript.",
"type": "module",
"main": "dist/index.cjs",
Expand Down
9 changes: 7 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class Client<Driver extends StorageDriver = MemoryDriver> implements StorageOper
private readonly _drive: StorageDriver;

private readonly _ttl: Map<HashField, TTL> = new Map();
private _state: StorageState = 'pending';
private _state: StorageState = 'stale';

constructor(storage?: Driver) {
this._drive = storage || new MemoryDriver();
this._prepare().finally();
}

private async _prepare() {
this._state = 'pending';

if (this._drive.prepare) {
await this._drive.prepare();
}
Expand All @@ -43,7 +45,10 @@ class Client<Driver extends StorageDriver = MemoryDriver> implements StorageOper
// Wait til driver is ready
return new Promise<void>((resolve) => {
const intervalId = setInterval(() => {
if (this._state === 'ready') {
if (this._state === 'stale') {
this._state = 'pending';
this._prepare().finally();
} else if (this._state === 'ready') {
clearInterval(intervalId);
resolve();
}
Expand Down
11 changes: 11 additions & 0 deletions src/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ describe('List', () => {
expect(await list.toArray()).to.have.members([1, 2, 3]);
});

it('Async iteration', async () => {
const list = c.createList();
await list.push(1);
await list.push(2);
await list.push(3);

for await (const item of list) {
expect(item).to.be.oneOf([1, 2, 3]);
}
});

describe('Lucky Numbers', () => {
const list = c.createList(LuckyNumberList);

Expand Down
9 changes: 9 additions & 0 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ export class List<Value extends HashValue = HashValue> {
private readonly _key: HashKey
) {}

[Symbol.asyncIterator]() {
const pList = this.list();
return (async function* (): AsyncGenerator<Value> {
for (const item of await pList) {
yield item;
}
})();
}

set(index: number, value: Value | null): Promise<void> {
return this._client.lset(this._key, index, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export interface IStorageParser {
parse(value: any): Map<string, Serializable>;
}

export type StorageState = 'pending' | 'ready';
export type StorageState = 'stale' | 'pending' | 'ready';

// ---------------------

Expand Down

0 comments on commit d964098

Please sign in to comment.