feat(nbstore): init (#7639)

TODO

- [x] basic
- [x] storages
- [x] producer/consumer
- [x] operation pattern
- [x] events
- [x] worker
- [x] readme
- [x] peer dependencies
This commit is contained in:
forehalo
2024-11-22 03:13:04 +00:00
parent 76eabf644c
commit 4125038ff8
18 changed files with 882 additions and 3 deletions
@@ -0,0 +1,44 @@
export interface Locker {
lock(domain: string, resource: string): Promise<Lock>;
}
export class SingletonLocker implements Locker {
lockedResource = new Map<string, Lock>();
constructor() {}
async lock(domain: string, resource: string) {
const key = `${domain}:${resource}`;
let lock = this.lockedResource.get(key);
if (!lock) {
lock = new Lock();
this.lockedResource.set(key, lock);
}
await lock.acquire();
return lock;
}
}
export class Lock {
private inner: Promise<void> = Promise.resolve();
private release: () => void = () => {};
async acquire() {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let release: () => void = null!;
const nextLock = new Promise<void>(resolve => {
release = resolve;
});
await this.inner;
this.inner = nextLock;
this.release = release;
}
[Symbol.asyncDispose]() {
this.release();
return Promise.resolve();
}
}