在 TypeScript 中实现确定性清理的惯用方法是什么?换句话说,是否存在与 C#using (IDisposable)或 C++ 的 RAII 等效的东西?
还是我应该坚持下去finally?
上下文:在 SPA 应用程序(Aurelia / ASP.NET Core Web API)中,我试图向用户指示当前正在从 Web API 获取数据。获取完成后,即使抛出异常,我也想删除 UI 指示。我正在考虑将其包装到“RAII 类”中,以便重用和更清晰的语法(这样我就不必在代码中添加finally)...
Typescript 中没有任何using声明,无论如何,您始终可以依靠try-finallyC using# 中的语法糖。
另一种方法是using使用函数创建您自己的。
interface IDisposable {
dispose();
}
function using<T extends IDisposable,
T2 extends IDisposable,
T3 extends IDisposable>(disposable: [T, T2, T3], action: (r: T, r2: T2, r3: T3) => void);
function using<T extends IDisposable, T2 extends IDisposable>(disposable: [T, T2], action: (r: T, r2: T2) => void);
function using<T extends IDisposable>(disposable: T, action: (r: T) => void);
function using(disposable: IDisposable[], action: (...r: IDisposable[]) => void)
function using(disposable: IDisposable | IDisposable[], action: (...r: IDisposable[]) => void) {
let disposableArray = disposable instanceof Array ? disposable : [disposable];
try {
action(...disposableArray);
} finally {
disposableArray.forEach(d => d.dispose());
}
}
// Usage
class UserNotify { dispose() { console.log("Done"); } }
class Other { dispose() { console.log("Done Other"); } }
using(new UserNotify(), userNotify => {
console.log("DoStuff");
})
// It will type the arrow function parameter correctly for up to 3 parameters, but you can add more overloads above.
using([new UserNotify(), new Other()], (userNotify, other) => {
console.log("DoStuff");
})
Run Code Online (Sandbox Code Playgroud)
如果你想将它与 Promise 一起使用,你可以创建一个异步版本,其中一次性返回一个 Promise,并且参数action也返回一个 Promise:
interface IDisposableAsync {
dispose(): Promise<void> | void;
}
function usingAsync<T extends IDisposableAsync, T2 extends IDisposableAsync, T3 extends IDisposableAsync>(disposable: [T, T2, T3], action: (r: T, r2: T2, r3: T3) => Promise<void>): Promise<void>;
function usingAsync<T extends IDisposableAsync, T2 extends IDisposableAsync>(disposable: [T, T2], action: (r: T, r2: T2) => Promise<void>): Promise<void>;
function usingAsync<T extends IDisposableAsync>(disposable: T, action: (r: T) => Promise<void>): Promise<void>;
function usingAsync(disposable: IDisposableAsync[], action: (...r: IDisposableAsync[]) => Promise<void>): Promise<void>
async function usingAsync(disposable: IDisposableAsync | IDisposableAsync[], action: (...r: IDisposableAsync[]) => Promise<void>): Promise<void> {
let disposableArray = disposable instanceof Array ? disposable : [disposable];
try {
await action(...disposableArray);
} finally {
for (let d of disposableArray) {
let result = d.dispose();
if (result !== null) {
await result;
}
}
}
}
// Usage
class UserNotify { dispose() { console.log("Done"); } }
class Other { dispose() { console.log("Done Other"); } }
function delay() {
return new Promise((r)=> setTimeout(() => {
r();
}, 100));
}
(async function () {
await usingAsync(new UserNotify(), async userNotify => {
await delay()
console.log("DoStuff");
})
await usingAsync([new UserNotify(), new Other()], async (userNotify, other) => {
await delay()
console.log("DoStuff");
})
})();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3599 次 |
| 最近记录: |