avg-core-ts
Version:
Develop Adventure Game (or ADV, Visual Novel, Galgame, etc.) on your own!
50 lines (42 loc) • 1.09 kB
JavaScript
/**
* @file Flow control commands
* @author MicroMatrix <yangpan.1985@bytedance.com>
* @copyright 2012-2020 bytedance
* @link git@code.byted.org:yangpan.1985/avg.git
*/
import core from 'core/core';
class Flow {
constructor() {
this.initialed = false;
core.use('flow-init', this.init.bind(this));
}
async init() {
if (!this.initialed) {
core.use('script-exec', this.exec.bind(this));
this.initialed = true;
}
}
/**
* Supply flow-control commands:
*
* `wait`: Prevents script execution for some time. ex.`[flow wait time=1000]`
*
* @method exec
* @param {object} ctx middleware context
* @param {Function} next execute next middleware
*/
async exec(ctx, next) {
const { command, flags, params } = ctx;
if (command === 'flow') {
if (flags.includes('wait') && !flags.includes('_skip_')) {
await new Promise(resolve => {
setTimeout(resolve, params.time || 0);
});
}
} else {
await next();
}
}
}
const flow = new Flow();
export default flow;