Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/__tests__/global-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,83 @@ describe("dispose", () => {
expect(baz.disposed).toBeTruthy();
});

it("shares pending and completed disposal across calls", async () => {
const container = globalContainer.createChildContainer();
let finish!: () => void;
const pending = new Promise<void>(resolve => {
finish = resolve;
});
const dispose = jest.fn(() => pending);
class Resource implements Disposable {
dispose(): Promise<void> {
return dispose();
}
}
container.resolve(Resource);

const first = container.dispose();
const second = container.dispose();
expect(second).toBe(first);
expect(dispose).toHaveBeenCalledTimes(1);

finish();
await Promise.all([first, second]);
await container.dispose();
expect(dispose).toHaveBeenCalledTimes(1);
});

it.each([false, true])(
"shares a disposal failure without repeating teardown (async: %s)",
async isAsync => {
const container = globalContainer.createChildContainer();
const error = new Error("teardown failed");
const dispose = jest.fn(() => {
if (isAsync) {
return Promise.reject(error);
}
throw error;
});
class FailingResource implements Disposable {
dispose(): Promise<void> {
return dispose();
}
}
container.resolve(FailingResource);
const other = container.resolve(Foo);

await Promise.all([
expect(container.dispose()).rejects.toBe(error),
expect(container.dispose()).rejects.toBe(error)
]);
await expect(container.dispose()).rejects.toBe(error);
expect(dispose).toHaveBeenCalledTimes(1);
expect(other.disposed).toBeTruthy();
}
);

it.each([false, true])(
"does not wait on itself when teardown calls dispose again (async: %s)",
async isAsync => {
const container = globalContainer.createChildContainer();
const dispose = jest.fn(
isAsync
? async () => {
await container.dispose();
}
: () => container.dispose()
);
class Resource implements Disposable {
dispose(): Promise<void> | void {
return dispose();
}
}
container.resolve(Resource);

await container.dispose();
expect(dispose).toHaveBeenCalledTimes(1);
}
);

it("disposes all instances of the same type", () => {
const container = globalContainer.createChildContainer();

Expand Down
30 changes: 18 additions & 12 deletions src/dependency-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const typeInfo = new Map<constructor<any>, ParamInfo[]>();
class InternalDependencyContainer implements DependencyContainer {
private _registry = new Registry();
private interceptors = new Interceptors();
private disposed = false;
private disposal?: "starting" | Promise<void>;
private disposables = new Set<Disposable>();

public constructor(private parent?: InternalDependencyContainer) {}
Expand Down Expand Up @@ -475,18 +475,24 @@ class InternalDependencyContainer implements DependencyContainer {
});
}

public async dispose(): Promise<void> {
this.disposed = true;

const promises: Promise<unknown>[] = [];
this.disposables.forEach(disposable => {
const maybePromise = disposable.dispose();
public dispose(): Promise<void> {
if (this.disposal === "starting") {
// A callback reentering during startup cannot wait on its own teardown.
return Promise.resolve();
}

if (maybePromise) {
promises.push(maybePromise);
}
});
if (!this.disposal) {
this.disposal = "starting";
this.disposal = this.disposeInstances();
}
return this.disposal;
}

private async disposeInstances(): Promise<void> {
const promises = Array.from(this.disposables, async disposable =>
disposable.dispose()
);
this.disposables.clear();
await Promise.all(promises);
}

Expand Down Expand Up @@ -589,7 +595,7 @@ class InternalDependencyContainer implements DependencyContainer {
}

private ensureNotDisposed(): void {
if (this.disposed) {
if (this.disposal) {
throw new Error(
"This container has been disposed, you cannot interact with a disposed container"
);
Expand Down