-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
436 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
|
||
import { type DisposableLike, disposeOf, isDisposableHybrid, isDisposed } from './disposable.js'; | ||
import { createDisposableList, DisposableList, InheritableDisposable } from './DisposableList.js'; | ||
|
||
describe('disposable', () => { | ||
test('InheritableDisposable', () => { | ||
let count = 0; | ||
class MyDisposable extends InheritableDisposable { | ||
constructor(disposables: DisposableLike[]) { | ||
super(disposables); | ||
} | ||
} | ||
|
||
function use() { | ||
using _d = new MyDisposable([() => (count += 10)]); | ||
} | ||
|
||
expect(count).toBe(0); | ||
|
||
use(); | ||
|
||
expect(count).toBe(10); | ||
}); | ||
|
||
test('DisposableList', () => { | ||
let count = 0; | ||
|
||
function use() { | ||
using list = new DisposableList([() => (count += 10)]); | ||
list.push(() => (count += 100)); | ||
} | ||
|
||
expect(count).toBe(0); | ||
|
||
use(); | ||
|
||
expect(count).toBe(110); | ||
}); | ||
|
||
test('createDisposableList', () => { | ||
const list = createDisposableList(); | ||
let count = 0; | ||
list.push(() => (count += 1)); | ||
disposeOf(list); | ||
expect(count).toBe(1); | ||
expect(isDisposed(list)).toBe(true); | ||
expect(list.isDisposed()).toBe(true); | ||
}); | ||
|
||
test('double dispose', () => { | ||
let count = 0; | ||
const list = createDisposableList([() => (count += 10)]); | ||
function use() { | ||
using aliasList = list; | ||
aliasList.push(() => (count += 100)); | ||
} | ||
|
||
expect(list.length).toBe(1); | ||
use(); | ||
|
||
expect(count).toBe(110); | ||
expect(list.length).toBe(0); | ||
|
||
expect(() => list.push(() => (count += 1000))).toThrowError('Already disposed, cannot add items.'); | ||
|
||
expect(list.length).toBe(0); | ||
list.dispose(); | ||
expect(list.length).toBe(0); | ||
|
||
expect(count).toBe(110); | ||
}); | ||
|
||
test('', () => { | ||
const list = createDisposableList(); | ||
expect(isDisposableHybrid(list)).toBe(true); | ||
expect(list.isDisposed()); | ||
}); | ||
}); |
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,60 @@ | ||
import type { DisposableHybrid, DisposableLike } from './disposable.js'; | ||
import { createDisposeMethodFromList, symbolIsDisposed } from './disposable.js'; | ||
|
||
/** This is a class that can be inherited to provide Disposable support. */ | ||
|
||
export const noop = () => undefined; | ||
|
||
export class InheritableDisposable implements DisposableHybrid { | ||
public dispose: () => void; | ||
public [Symbol.dispose]: () => void = noop; | ||
public [symbolIsDisposed]: boolean = false; | ||
|
||
/** the inherited class can safely add disposables to _disposables */ | ||
protected readonly _disposables: DisposableLike[]; | ||
constructor(disposables?: DisposableLike[], name = 'InheritableDisposable') { | ||
this._disposables = disposables ?? []; | ||
const _dispose = createDisposeMethodFromList(this._disposables, name); | ||
const dispose = () => { | ||
if (this.isDisposed()) return; | ||
this[symbolIsDisposed] = true; | ||
_dispose(); | ||
// Prevent new disposables from being added. | ||
Object.freeze(this._disposables); | ||
}; | ||
this.dispose = dispose; | ||
this[Symbol.dispose] = dispose; | ||
} | ||
|
||
protected isDisposed(): boolean { | ||
return this[symbolIsDisposed]; | ||
} | ||
} | ||
|
||
export class DisposableList extends InheritableDisposable { | ||
constructor( | ||
public readonly disposables: DisposableLike[] = [], | ||
readonly name = 'DisposableList', | ||
) { | ||
super(disposables); | ||
} | ||
|
||
public push(disposable: DisposableLike) { | ||
if (this.isDisposed()) { | ||
throw new Error('Already disposed, cannot add items.'); | ||
} | ||
this.disposables.push(disposable); | ||
} | ||
|
||
get length() { | ||
return this.disposables.length; | ||
} | ||
|
||
public isDisposed(): boolean { | ||
return super.isDisposed(); | ||
} | ||
} | ||
|
||
export function createDisposableList(disposables?: DisposableLike[], name?: string): DisposableList { | ||
return new DisposableList(disposables, name); | ||
} |
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
Oops, something went wrong.