-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
DisposableHandle
and AsyncDisposableHandle
- Loading branch information
Showing
6 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* An object that provides an async `.dispose()` method that can called only once. | ||
* | ||
* Calling `.dispose()` will call the provided `onDispose` function only once. | ||
* Any subsequent calls to `.dispose()` will do nothing. | ||
*/ | ||
export class AsyncDisposableHandle { | ||
/** @internal */ private _onDispose: (() => Promise<void>) | undefined; | ||
|
||
public constructor(onDispose: () => Promise<void>) { | ||
this._onDispose = onDispose; | ||
|
||
this.dispose = this.dispose.bind(this); | ||
this[Symbol.asyncDispose] = this[Symbol.asyncDispose].bind(this); | ||
} | ||
|
||
public get disposed() { | ||
return this._onDispose == null; | ||
} | ||
|
||
public async [Symbol.asyncDispose]() { | ||
await this.dispose(); | ||
} | ||
|
||
public async dispose() { | ||
if (this._onDispose != null) { | ||
const onDispose = this._onDispose; | ||
delete this._onDispose; | ||
|
||
await onDispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* An object that provides a `.dispose()` method that can called only once. | ||
* | ||
* Calling `.dispose()` will call the provided `onDispose` function only once. | ||
* Any subsequent calls to `.dispose()` will do nothing. | ||
*/ | ||
export class DisposableHandle { | ||
/** @internal */ private _onDispose: (() => void) | undefined; | ||
|
||
public constructor(onDispose: () => void) { | ||
this._onDispose = onDispose; | ||
|
||
this.dispose = this.dispose.bind(this); | ||
this[Symbol.dispose] = this[Symbol.dispose].bind(this); | ||
} | ||
|
||
public get disposed() { | ||
return this._onDispose == null; | ||
} | ||
|
||
public [Symbol.dispose]() { | ||
this.dispose(); | ||
} | ||
|
||
public dispose() { | ||
if (this._onDispose != null) { | ||
const onDispose = this._onDispose; | ||
delete this._onDispose; | ||
|
||
onDispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {describe, expect, test} from "vitest"; | ||
import {AsyncDisposableHandle} from "../src/index.js"; | ||
|
||
describe("AsyncDisposableHandle", () => { | ||
test("disposing only happens once", async () => { | ||
let disposeTimes = 0; | ||
const handle = new AsyncDisposableHandle(async () => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
await handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
await handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
|
||
test("marked as disposed before the callback finishes", async () => { | ||
let disposeTimes = 0; | ||
const handle = new AsyncDisposableHandle(async () => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
void handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
void handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
|
||
test("Symbol.dispose works", async () => { | ||
let disposeTimes = 0; | ||
const handle = new AsyncDisposableHandle(async () => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
await handle[Symbol.asyncDispose](); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
await handle[Symbol.asyncDispose](); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
|
||
test("Storing dispose function in a variable", () => { | ||
let disposeTimes = 0; | ||
const handle = new AsyncDisposableHandle(async () => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
const dispose = handle.dispose; | ||
dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {describe, expect, test} from "vitest"; | ||
import {DisposableHandle} from "../src/index.js"; | ||
|
||
describe("DisposableHandle", () => { | ||
test("disposing only happens once", () => { | ||
let disposeTimes = 0; | ||
const handle = new DisposableHandle(() => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
handle.dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
|
||
test("Symbol.dispose works", () => { | ||
let disposeTimes = 0; | ||
const handle = new DisposableHandle(() => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
handle[Symbol.dispose](); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
handle[Symbol.dispose](); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
|
||
test("Storing dispose function in a variable", () => { | ||
let disposeTimes = 0; | ||
const handle = new DisposableHandle(() => { | ||
disposeTimes++; | ||
}); | ||
|
||
expect(disposeTimes).toBe(0); | ||
expect(handle.disposed).toBe(false); | ||
|
||
const dispose = handle.dispose; | ||
dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
|
||
dispose(); | ||
expect(disposeTimes).toBe(1); | ||
expect(handle.disposed).toBe(true); | ||
}); | ||
}); |