Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Disposable.NULL always returns a new Disposable #11053

Merged
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
30 changes: 30 additions & 0 deletions packages/core/src/common/disposable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// *****************************************************************************
// Copyright (C) 2022 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { expect } from 'chai';
import { DisposableCollection, Disposable } from './disposable';

describe('Disposables', () => {
/* eslint-disable no-unused-expressions */
it('Is safe to use Disposable.NULL', () => {
const collectionA = new DisposableCollection(Disposable.NULL);
const collectionB = new DisposableCollection(Disposable.NULL);
expect(!collectionA.disposed && !collectionB.disposed, 'Neither should be disposed before either is disposed.').to.be.true;
collectionA.dispose();
expect(collectionA.disposed, 'A should be disposed after being disposed.').to.be.true;
expect(collectionB.disposed, 'B should not be disposed because A was disposed.').to.be.false;
});
});
19 changes: 15 additions & 4 deletions packages/core/src/common/disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ export namespace Disposable {
return !!arg && typeof arg === 'object' && 'dispose' in arg && typeof arg['dispose'] === 'function';
}
export function create(func: () => void): Disposable {
return {
dispose: func
};
return { dispose: func };
}
export const NULL = create(() => { });
/** Always provides a reference to a new disposable. */
export declare const NULL: Disposable;
}

/**
* Ensures that every reference to {@link Disposable.NULL} returns a new object,
* as sharing a disposable between multiple {@link DisposableCollection} can have unexpected side effects
*/
Object.defineProperty(Disposable, 'NULL', {
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
configurable: false,
enumerable: true,
get(): Disposable {
return { dispose: () => { } };
}
});

export class DisposableCollection implements Disposable {

protected readonly disposables: Disposable[] = [];
Expand Down