Merge pull request #772 from nofficalfs/PR-Branch

Add Mutex to lib.onload to parallel.
This commit is contained in:
Spmario233 2024-01-08 23:50:46 +08:00 committed by GitHub
commit 106423f529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 933 additions and 871 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +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 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));
}

58
noname/util/mutex.js Normal file
View File

@ -0,0 +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();
}
}
}