mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 09:42:34 +00:00
refactor(server): adjust types
This commit is contained in:
parent
dcb22c8ce3
commit
c17dfd36b8
@ -12,12 +12,17 @@ import { Mutex, tryAcquire, MutexInterface, E_CANCELED } from 'async-mutex';
|
|||||||
|
|
||||||
export type Releaser = () => void | Promise<void>;
|
export type Releaser = () => void | Promise<void>;
|
||||||
|
|
||||||
export abstract class AbstractLockAdapter {
|
export interface ILock {
|
||||||
async connect() {}
|
acquire(ttl: number): Releaser | Promise<Releaser>;
|
||||||
async close() {}
|
runExclusive<T>(fn: () => Promise<T>, ttl: number): Promise<T>;
|
||||||
abstract acquire(key: string, ttl: number): Releaser | Promise<Releaser>;
|
}
|
||||||
abstract runExclusive<T>(key: string, fn: () => Promise<T>, ttl: number): Promise<T>;
|
|
||||||
// abstract tryAcquire(key: string, ttl: number): Releaser | Promise<Releaser>;
|
export interface ILockAdapter {
|
||||||
|
connect(): Promise<void>;
|
||||||
|
close(): Promise<void>;
|
||||||
|
acquire(key: string, ttl: number): Releaser | Promise<Releaser>;
|
||||||
|
runExclusive<T>(key: string, fn: () => Promise<T>, ttl: number): Promise<T>;
|
||||||
|
// tryAcquire(key: string, timeout?: number): Promise<ILock>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LockAbortError extends Error {
|
export class LockAbortError extends Error {
|
||||||
@ -26,9 +31,18 @@ export class LockAbortError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LocalLockAdapter extends AbstractLockAdapter {
|
export class LockAcquireError extends Error {
|
||||||
|
constructor(message, options) {
|
||||||
|
super(message, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LocalLockAdapter implements ILockAdapter {
|
||||||
static locks = new Map<string, MutexInterface>();
|
static locks = new Map<string, MutexInterface>();
|
||||||
|
|
||||||
|
async connect() {}
|
||||||
|
async close() {}
|
||||||
|
|
||||||
private getLock(key: string): MutexInterface {
|
private getLock(key: string): MutexInterface {
|
||||||
let lock = (<typeof LocalLockAdapter>this.constructor).locks.get(key);
|
let lock = (<typeof LocalLockAdapter>this.constructor).locks.get(key);
|
||||||
if (!lock) {
|
if (!lock) {
|
||||||
@ -72,13 +86,26 @@ class LocalLockAdapter extends AbstractLockAdapter {
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// async tryAcquire(key: string, ttl: number) {
|
|
||||||
|
// async tryAcquire(key: string) {
|
||||||
|
// try {
|
||||||
// const lock = this.getLock(key);
|
// const lock = this.getLock(key);
|
||||||
// return lock.tryAcquire(ttl);
|
// await tryAcquire(lock);
|
||||||
|
// return {
|
||||||
|
// async acquire(ttl) {
|
||||||
|
// return this.acquire(key, ttl);
|
||||||
|
// },
|
||||||
|
// async runExclusive(fn: () => Promise<any>, ttl) {
|
||||||
|
// return this.runExclusive(key, fn, ttl);
|
||||||
|
// },
|
||||||
|
// };
|
||||||
|
// } catch (e) {
|
||||||
|
// throw new LockAcquireError('Lock acquire error', { cause: e });
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LockAdapterConfig<C extends AbstractLockAdapter = AbstractLockAdapter> {
|
export interface LockAdapterConfig<C extends ILockAdapter = ILockAdapter> {
|
||||||
Adapter: new (...args: any[]) => C;
|
Adapter: new (...args: any[]) => C;
|
||||||
options?: Record<string, any>;
|
options?: Record<string, any>;
|
||||||
}
|
}
|
||||||
@ -89,7 +116,7 @@ export interface LockManagerOptions {
|
|||||||
|
|
||||||
export class LockManager {
|
export class LockManager {
|
||||||
private registry = new Registry<LockAdapterConfig>();
|
private registry = new Registry<LockAdapterConfig>();
|
||||||
private adapters = new Map<string, AbstractLockAdapter>();
|
private adapters = new Map<string, ILockAdapter>();
|
||||||
|
|
||||||
constructor(private options: LockManagerOptions = {}) {
|
constructor(private options: LockManagerOptions = {}) {
|
||||||
this.registry.register('local', {
|
this.registry.register('local', {
|
||||||
@ -101,7 +128,7 @@ export class LockManager {
|
|||||||
this.registry.register(name, adapterConfig);
|
this.registry.register(name, adapterConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAdapter(): Promise<AbstractLockAdapter> {
|
private async getAdapter(): Promise<ILockAdapter> {
|
||||||
const type = this.options.defaultAdapter || 'local';
|
const type = this.options.defaultAdapter || 'local';
|
||||||
let client = this.adapters.get(type);
|
let client = this.adapters.get(type);
|
||||||
if (!client) {
|
if (!client) {
|
||||||
|
Loading…
Reference in New Issue
Block a user