Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
69 lines (62 loc) · 2.86 KB

File metadata and controls

69 lines (62 loc) · 2.86 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// The following is a translation of the TypeScript async awaiter which uses generators and yields.
// For Lua we use coroutines instead.
//
// Source:
//
// var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
// function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
// return new (P || (P = Promise))(function (resolve, reject) {
// function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
// function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
// function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
// step((generator = generator.apply(thisArg, _arguments || [])).next());
// });
// };
//
import { __TS__Promise } from "./Promise";
const coroutine = _G.coroutine ?? {};
const cocreate = coroutine.create;
const coresume = coroutine.resume;
const costatus = coroutine.status;
const coyield = coroutine.yield;
// Be extremely careful editing this function. A single non-tail function call may ruin chained awaits performance
// eslint-disable-next-line @typescript-eslint/promise-function-async
export function __TS__AsyncAwaiter(this: void, generator: (this: void) => void) {
return new Promise((resolve, reject) => {
let resolved = false;
const asyncCoroutine = cocreate(generator);
function fulfilled(value: unknown): void {
const [success, resultOrError] = coresume(asyncCoroutine, value);
if (success) {
// `step` never throws. Tail call return is important!
return step(resultOrError);
}
// `reject` should never throw. Tail call return is important!
return reject(resultOrError);
}
function step(this: void, result: unknown): void {
if (resolved) {
return;
}
if (costatus(asyncCoroutine) === "dead") {
// `resolve` never throws. Tail call return is important!
return resolve(result);
}
// We cannot use `then` because we need to avoid calling `coroutine.resume` from inside `pcall`
// `fulfilled` and `reject` should never throw. Tail call return is important!
return __TS__Promise.resolve(result).addCallbacks(fulfilled, reject);
}
const [success, resultOrError] = coresume(asyncCoroutine, (v: unknown) => {
resolved = true;
return __TS__Promise.resolve(v).addCallbacks(resolve, reject);
});
if (success) {
return step(resultOrError);
} else {
return reject(resultOrError);
}
});
}
export function __TS__Await(this: void, thing: unknown) {
return coyield(thing);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.