pref: divide `game.promises`.
This commit is contained in:
parent
e03a7f1e55
commit
e5eb8894de
|
@ -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<string | false>;
|
||||
* (title: string, forced: true): Promise<string>;
|
||||
* (alertOption: 'alert', title: string): Promise<true>;
|
||||
* }}
|
||||
*
|
||||
* @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<string> }
|
||||
*/
|
||||
// @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<true> }
|
||||
*/
|
||||
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 = {};
|
||||
|
|
|
@ -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<string | false>;
|
||||
* (title: string, forced: true): Promise<string>;
|
||||
* (alertOption: 'alert', title: string): Promise<true>;
|
||||
* }}
|
||||
*
|
||||
* @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<string> }
|
||||
*/
|
||||
// @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<true> }
|
||||
*/
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue