fix: might be wrong.

This commit is contained in:
Ansolve 2024-01-08 01:20:11 +08:00
parent 1a49bf5943
commit fb422165cc
No known key found for this signature in database
GPG Key ID: 2F2873AC9538CC2D
3 changed files with 933 additions and 933 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +1,28 @@
export const nonameInitialized = localStorage.getItem('noname_inited');
export const assetURL = typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized;
export const GeneratorFunction = (function* () { }).constructor;
export const AsyncFunction = (async function () { }).constructor;
export const userAgent = navigator.userAgent.toLowerCase();
export { Mutex } from './mutex.js';
export const characterDefaultPicturePath = "image/character/default_silhouette_";
// 我靠循环引用问题在这?
// export * as config from './config.js'
/**
* 不能被new的类
*/
export class Uninstantable {
constructor() {
throw new TypeError(`${new.target.name} is not a constructor`);
}
}
/**
*
* @param { number } ms
* @returns { Promise<void> }
*/
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export const nonameInitialized = localStorage.getItem('noname_inited');
export const assetURL = typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized;
export const GeneratorFunction = (function* () { }).constructor;
export const AsyncFunction = (async function () { }).constructor;
export const userAgent = navigator.userAgent.toLowerCase();
export { Mutex } from './mutex.js';
export const characterDefaultPicturePath = "image/character/default_silhouette_";
// 我靠循环引用问题在这?
// export * as config from './config.js'
/**
* 不能被new的类
*/
export class Uninstantable {
constructor() {
throw new TypeError(`${new.target.name} is not a constructor`);
}
}
/**
*
* @param { number } ms
* @returns { Promise<void> }
*/
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@ -1,58 +1,58 @@
/**
*
*/
export class Mutex {
/**
* @type {'locked' | 'unlocked'}
*/
#status;
/**
* @type {null | Promise<void>}
*/
#promise;
/**
* @type {null | function(): void}
*/
#resolve;
constructor() {
this.#status = 'unlocked';
this.#promise = null;
this.#resolve = null;
}
async lock() {
switch (this.#status) {
case 'locked':
await this.#promise;
case 'unlocked':
this.#status = 'locked';
// @ts-ignore
({ promise: this.#promise, resolve: this.#resolve } = Promise.withResolvers())
break;
}
}
unlock() {
if (this.#status === 'unlocked') throw new Error('This Mutex is not locked.');
this.#status = 'unlocked';
if (this.#resolve) this.#resolve();
}
/**
*
* @param {function(): void | Promise<void>} content
*/
async scoped(content) {
try {
await this.lock();
await content();
} finally {
this.unlock();
}
}
}
/**
*
*/
export class Mutex {
/**
* @type {'locked' | 'unlocked'}
*/
#status;
/**
* @type {null | Promise<void>}
*/
#promise;
/**
* @type {null | function(): void}
*/
#resolve;
constructor() {
this.#status = 'unlocked';
this.#promise = null;
this.#resolve = null;
}
async lock() {
switch (this.#status) {
case 'locked':
await this.#promise;
case 'unlocked':
this.#status = 'locked';
// @ts-ignore
({ promise: this.#promise, resolve: this.#resolve } = Promise.withResolvers())
break;
}
}
unlock() {
if (this.#status === 'unlocked') throw new Error('This Mutex is not locked.');
this.#status = 'unlocked';
if (this.#resolve) this.#resolve();
}
/**
*
* @param {function(): void | Promise<void>} content
*/
async scoped(content) {
try {
await this.lock();
await content();
} finally {
this.unlock();
}
}
}