| 23 | * the function returned false. |
| 24 | */ |
| 25 | export function assertAsync (engine: VisualNovelEngine, callable: (...args: unknown[]) => unknown, self: unknown = null, args: unknown[] | null = null): Promise<void> { |
| 26 | const originalBlockValue = engine.global ('block'); |
| 27 | |
| 28 | engine.global ('block', true); |
| 29 | |
| 30 | return new Promise<void> ((resolve, reject) => { |
| 31 | const result = callable.apply(self, args || []); |
| 32 | // Check if the function returned a simple boolean |
| 33 | // if the return value is true, the game will continue |
| 34 | if (typeof result === 'boolean') { |
| 35 | if (result) { |
| 36 | resolve (); |
| 37 | } else { |
| 38 | reject (); |
| 39 | } |
| 40 | } else if (result !== null && typeof result === 'object') { |
| 41 | // Check if the result was a promise |
| 42 | if ('then' in result && typeof result.then === 'function') { |
| 43 | (result as Promise<unknown>).then((value: unknown) => { |
| 44 | if (typeof value === 'boolean') { |
| 45 | if (value) { |
| 46 | resolve (); |
| 47 | } else { |
| 48 | reject (); |
| 49 | } |
| 50 | } else { |
| 51 | resolve (); |
| 52 | } |
| 53 | }).catch(reject); |
| 54 | } else { |
| 55 | resolve (); |
| 56 | } |
| 57 | } else { |
| 58 | reject (); |
| 59 | } |
| 60 | }).finally (() => { |
| 61 | engine.global ('block', originalBlockValue); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | // ============================================================================ |
| 66 | // Step Navigation |