From e5eb8894ded009349b5bbf51796fa239caacc545 Mon Sep 17 00:00:00 2001 From: Ansolve Date: Thu, 4 Jan 2024 18:35:14 +0800 Subject: [PATCH] pref: divide `game.promises`. --- noname/game/index.js | 95 ++--------------------------------------- noname/game/promises.js | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 noname/game/promises.js diff --git a/noname/game/index.js b/noname/game/index.js index dbb650b04..a6c9f6d78 100644 --- a/noname/game/index.js +++ b/noname/game/index.js @@ -17,7 +17,9 @@ import { status as _status } from '../status/index.js'; import { UI as ui } from '../ui/index.js'; import { GNC as gnc } from '../gnc/index.js'; import { userAgent, Uninstantable, GeneratorFunction, AsyncFunction, delay } from "../util/index.js"; + import { DynamicStyle } from "./dynamic-style/index.js"; +import { GamePromises } from "./promises.js"; export class Game extends Uninstantable { static online = false; @@ -39,98 +41,7 @@ export class Game extends Uninstantable { static phaseNumber = 0; static roundNumber = 0; static shuffleNumber = 0; - static promises = { - /** - * 模仿h5的prompt,用于显示可提示用户进行输入的对话框 - * - * 注: 由于参数列表是随意的,在这里我准备限制一下这个函数的参数顺序 - * - * @type {{ - * (title: string): Promise; - * (title: string, forced: true): Promise; - * (alertOption: 'alert', title: string): Promise; - * }} - * - * @param { string } title 设置prompt标题与input内容 - * @param { boolean } [forced] 为true的话将没有"取消按钮" - * @param { string } alertOption 设置prompt是否模拟alert - * @example - * ```js - * // 只设置标题(但是input的初始值就变成了undefined) - * game.promises.prompt('###prompt标题').then(value => console.log(value)); - * // 设置标题和input初始内容 - * game.promises.prompt('###prompt标题###input初始内容').then(value => console.log(value)); - * ``` - * @returns { Promise } - */ - // @ts-ignore - prompt(alertOption, title, forced) { - return new Promise((resolve, reject) => { - if (alertOption != 'alert') { - // @ts-ignore - forced = title || false; - title = alertOption; - game.prompt(title, forced, resolve); - } else { - game.prompt(title, alertOption, resolve); - } - }); - }, - /** - * 模仿h5的alert,用于显示信息的对话框 - * - * @param { string } title - * @example - * ```js - * await game.promises.alert('弹窗内容'); - * ``` - * @returns { Promise } - */ - alert(title) { - return new Promise((resolve, reject) => { - game.prompt(title, 'alert', resolve); - }); - }, - // 读写函数promises化(不用考虑其对应函数是否存在) - download(url, folder, dev, onprogress) { - return new Promise((resolve, reject) => { - game.download(url, folder, resolve, reject, dev, onprogress); - }); - }, - readFile(filename) { - return new Promise((resolve, reject) => { - game.readFile(filename, resolve, reject); - }); - }, - readFileAsText(filename) { - return new Promise((resolve, reject) => { - game.readFileAsText(filename, resolve, reject); - }); - }, - writeFile(data, path, name) { - return (new Promise((resolve, reject) => { - game.writeFile(data, path, name, resolve); - })).then(result => { - return new Promise((resolve, reject) => { - if (result instanceof Error) { - reject(result); - } else { - resolve(result); - } - }); - }); - }, - ensureDirectory(list, callback, file) { - return new Promise((resolve, reject) => { - game.ensureDirectory(list, callback, file).then(resolve).catch(reject); - }); - }, - createDir(directory) { - return new Promise((resolve, reject) => { - game.createDir(directory, resolve, reject); - }); - }, - } + static promises = GamePromises; static globalEventHandlers = new class { constructor() { this._handlers = {}; diff --git a/noname/game/promises.js b/noname/game/promises.js new file mode 100644 index 000000000..61a452fa4 --- /dev/null +++ b/noname/game/promises.js @@ -0,0 +1,95 @@ +import { Uninstantable } from "../util/index.js"; +import { game, Game } from "./index.js"; + +export class GamePromises extends Uninstantable { + /** + * 模仿h5的prompt,用于显示可提示用户进行输入的对话框 + * + * 注: 由于参数列表是随意的,在这里我准备限制一下这个函数的参数顺序 + * + * @type {{ + * (title: string): Promise; + * (title: string, forced: true): Promise; + * (alertOption: 'alert', title: string): Promise; + * }} + * + * @param { string } title 设置prompt标题与input内容 + * @param { boolean } [forced] 为true的话将没有"取消按钮" + * @param { string } alertOption 设置prompt是否模拟alert + * @example + * ```js + * // 只设置标题(但是input的初始值就变成了undefined) + * game.promises.prompt('###prompt标题').then(value => console.log(value)); + * // 设置标题和input初始内容 + * game.promises.prompt('###prompt标题###input初始内容').then(value => console.log(value)); + * ``` + * @returns { Promise } + */ + // @ts-ignore + static prompt(alertOption, title, forced) { + return new Promise((resolve, reject) => { + if (alertOption !== 'alert') { + // @ts-ignore + forced = title || false; + title = alertOption; + game.prompt(title, forced, resolve); + } else { + game.prompt(title, alertOption, resolve); + } + }); + } + /** + * 模仿h5的alert,用于显示信息的对话框 + * + * @param { string } title + * @example + * ```js + * await game.promises.alert('弹窗内容'); + * ``` + * @returns { Promise } + */ + static alert(title) { + return new Promise((resolve, reject) => { + game.prompt(title, 'alert', resolve); + }); + } + // 读写函数promises化(不用考虑其对应函数是否存在) + static download(url, folder, dev, onprogress) { + return new Promise((resolve, reject) => { + game.download(url, folder, resolve, reject, dev, onprogress); + }); + } + static readFile(filename) { + return new Promise((resolve, reject) => { + game.readFile(filename, resolve, reject); + }); + } + static readFileAsText(filename) { + return new Promise((resolve, reject) => { + game.readFileAsText(filename, resolve, reject); + }); + } + static writeFile(data, path, name) { + return (new Promise((resolve, reject) => { + game.writeFile(data, path, name, resolve); + })).then(result => { + return new Promise((resolve, reject) => { + if (result instanceof Error) { + reject(result); + } else { + resolve(result); + } + }); + }); + } + static ensureDirectory(list, callback, file) { + return new Promise((resolve, reject) => { + game.ensureDirectory(list, callback, file).then(resolve).catch(reject); + }); + } + static createDir(directory) { + return new Promise((resolve, reject) => { + game.createDir(directory, resolve, reject); + }); + } +}