Merge branch 'Dev-Enhancement-DivideMore' into PR-Branch.
This commit is contained in:
commit
837f8b626f
|
@ -9,6 +9,7 @@ import { UI as ui } from '../ui/index.js';
|
||||||
import { userAgent } from '../util/index.js';
|
import { userAgent } from '../util/index.js';
|
||||||
import * as config from '../util/config.js';
|
import * as config from '../util/config.js';
|
||||||
import { gnc } from '../gnc/index.js';
|
import { gnc } from '../gnc/index.js';
|
||||||
|
import { Mutex } from '../util/index.js';
|
||||||
|
|
||||||
export async function onload(resetGameTimeout) {
|
export async function onload(resetGameTimeout) {
|
||||||
const libOnload = lib.onload;
|
const libOnload = lib.onload;
|
||||||
|
@ -821,15 +822,17 @@ function createTouchDraggedFilter() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @async
|
* @async
|
||||||
* @param {(() => void | GeneratorFunction)[]} contents
|
* @param {((function(Mutex): void) | GeneratorFunction)[]} contents
|
||||||
*/
|
*/
|
||||||
function runCustomContents(contents) {
|
function runCustomContents(contents) {
|
||||||
if (!Array.isArray(contents)) return
|
if (!Array.isArray(contents)) return
|
||||||
|
|
||||||
|
const mutex = new Mutex();
|
||||||
|
|
||||||
const tasks = contents
|
const tasks = contents
|
||||||
.filter((fn) => typeof fn === "function")
|
.filter((fn) => typeof fn === "function")
|
||||||
.map((fn) => gnc.is.generatorFunc(fn) ? gnc.of(fn) : fn) // 将生成器函数转换成genCoroutin
|
.map((fn) => gnc.is.generatorFunc(fn) ? gnc.of(fn) : fn) // 将生成器函数转换成genCoroutin
|
||||||
.map((fn) => fn())
|
.map((fn) => fn(mutex))
|
||||||
|
|
||||||
|
|
||||||
return Promise
|
return Promise
|
||||||
|
|
|
@ -3,6 +3,7 @@ export const assetURL = typeof nonameInitialized != 'string' || nonameInitialize
|
||||||
export const GeneratorFunction = (function* () { }).constructor;
|
export const GeneratorFunction = (function* () { }).constructor;
|
||||||
export const AsyncFunction = (async function () { }).constructor;
|
export const AsyncFunction = (async function () { }).constructor;
|
||||||
export const userAgent = navigator.userAgent.toLowerCase();
|
export const userAgent = navigator.userAgent.toLowerCase();
|
||||||
|
export { Mutex } from './mutex.js';
|
||||||
export const characterDefaultPicturePath = "image/character/default_silhouette_";
|
export const characterDefaultPicturePath = "image/character/default_silhouette_";
|
||||||
|
|
||||||
// 我靠循环引用问题在这?
|
// 我靠循环引用问题在这?
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue