-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
176 lines (154 loc) · 4.05 KB
/
test.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// @ts-check
/// <reference lib="dom" />
import "./polyfill.js";
/**
* @typedef {import("./index.js").Disposable & {name: string}} NamedDisposable
*/
/**
* @typedef {import("./index.js").AsyncDisposable & {name: string}} NamedAsyncDisposable
*/
/** @param {{name: string}} obj */
function cleanup(obj) {
console.log(`Cleaning ${obj.name}`);
}
/**
*
* @param {string} name
* @returns {NamedDisposable}
*/
const getResource = (name) => {
console.log(`creating ${name}`);
return {
name,
[Symbol.dispose]() {
cleanup(this);
},
};
};
/** @param {Iterable<string>} names */
const getResources = function* (names) {
try {
console.log("generating resources for", names);
for (const name of names) {
yield getResource(name);
}
} finally {
console.log("done generating resources");
}
};
/**
*
* @param {string} name
* @returns {NamedAsyncDisposable}
*/
const getAsyncResource = (name) => {
console.log(`creating async ${name}`);
return {
name,
async [Symbol.asyncDispose]() {
await Promise.resolve();
cleanup(this);
},
};
};
/** @param {Iterable<string>} names */
const getAsyncResources = function* (names) {
try {
console.log("generating async resources for", names);
for (const name of names) {
yield getAsyncResource(name);
}
} finally {
console.log("done generating async resources");
}
};
/** @param {Iterable<string>} names */
const getResourcesAsync = async function* (names) {
try {
console.log("generating resources async for", names);
let i = 0;
for (const name of names) {
await Promise.resolve();
yield ++i % 2 ? getAsyncResource(name) : getResource(name);
}
} finally {
console.log("done generating resources async");
}
};
const testNames = ["hello", "bonjour"];
for (const { using } of Disposable) {
const res1 = using(getResource("ola"));
console.log(`using ${res1.name}`);
const res2 = using(getResource("gracias"));
console.log(`using ${res2.name}`);
using(Disposable.from(getResources(testNames)));
console.log("using aggregate");
using(() => console.log("done"));
using("foo", function () {
console.log("done with", this);
});
}
for (const { using } of Disposable) {
using(() => console.log("cleaning after break"));
break;
}
try {
for (const { using } of Disposable) {
using(() => console.log("cleaning after throw"));
throw new Error();
}
} catch (err) {}
for (const res of Disposable.usingFrom(getResources(testNames))) {
console.log(`using ${res.name}`);
}
for (const res of Disposable.usingFrom(getResources(testNames))) {
console.log(`using ${res.name}`);
break;
}
try {
for (const res of Disposable.usingFrom(getResources(["trouble"]))) {
console.log(`using ${res.name}`);
throw new Error();
}
} catch (err) {}
for (const obj of Disposable.usingFrom(
testNames.map((name) => ({ name })),
(obj) => () => cleanup(obj)
)) {
console.log(`using ${obj.name}`);
}
for await (const { using } of AsyncDisposable) {
const res1 = using(getResource("ola"));
console.log(`async using ${res1.name}`);
const res2 = using(getAsyncResource("gracias"));
console.log(`async using ${res2.name}`);
using(await AsyncDisposable.from(getAsyncResources(testNames)));
console.log("using async aggregate");
using(await AsyncDisposable.from(getResourcesAsync(testNames)));
console.log("using aggregate async");
using(() => console.log("done"));
using("foo", async function () {
await Promise.resolve();
console.log("done with", this);
});
}
for await (const { using } of AsyncDisposable) {
using(() => console.log("cleaning after break async"));
break;
}
try {
for await (const { using } of AsyncDisposable) {
using(() => console.log("cleaning after throw async"));
throw new Error();
}
} catch (err) {}
for await (const res of AsyncDisposable.usingFrom(
getAsyncResources(testNames)
)) {
console.log(`using async ${res.name}`);
}
for await (const res of AsyncDisposable.usingFrom(
getResourcesAsync(testNames)
)) {
console.log(`async using ${res.name}`);
}