From ca76b5a17c55533fd1dd0fd7e352596f734b0e57 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Thu, 20 Feb 2025 13:54:29 +0100 Subject: [PATCH] fix: remove the ability to use Rc with `using` --- .../misc/reference-counting/rc.spec.ts | 74 ------------------- .../platform/misc/reference-counting/rc.ts | 40 ---------- 2 files changed, 114 deletions(-) diff --git a/libs/common/src/platform/misc/reference-counting/rc.spec.ts b/libs/common/src/platform/misc/reference-counting/rc.spec.ts index 5fe44841285..ff0eed92ee1 100644 --- a/libs/common/src/platform/misc/reference-counting/rc.spec.ts +++ b/libs/common/src/platform/misc/reference-counting/rc.spec.ts @@ -1,11 +1,3 @@ -// Temporary workaround for Symbol.dispose -// remove when https://github.com/jestjs/jest/issues/14874 is resolved and *released* -const disposeSymbol: unique symbol = Symbol("Symbol.dispose"); -const asyncDisposeSymbol: unique symbol = Symbol("Symbol.asyncDispose"); -(Symbol as any).asyncDispose ??= asyncDisposeSymbol as unknown as SymbolConstructor["asyncDispose"]; -(Symbol as any).dispose ??= disposeSymbol as unknown as SymbolConstructor["dispose"]; - -// Import needs to be after the workaround import { Rc } from "./rc"; export class FreeableTestValue { @@ -168,70 +160,4 @@ describe("Rc", () => { }); }); }); - - it("should increase refCount when taken", () => { - rc.take(); - - expect(rc["refCount"]).toBe(1); - }); - - it("should return value on take", () => { - // eslint-disable-next-line @bitwarden/platform/required-using - const reference = rc.take(); - - expect(reference.value).toBe(value); - }); - - it("should decrease refCount when disposing reference", () => { - // eslint-disable-next-line @bitwarden/platform/required-using - const reference = rc.take(); - - reference[Symbol.dispose](); - - expect(rc["refCount"]).toBe(0); - }); - - it("should automatically decrease refCount when reference goes out of scope", () => { - { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using reference = rc.take(); - } - - expect(rc["refCount"]).toBe(0); - }); - - it("should not free value when refCount reaches 0 if not marked for disposal", () => { - // eslint-disable-next-line @bitwarden/platform/required-using - const reference = rc.take(); - - reference[Symbol.dispose](); - - expect(value.isFreed).toBe(false); - }); - - it("should free value when refCount reaches 0 and rc is marked for disposal", () => { - // eslint-disable-next-line @bitwarden/platform/required-using - const reference = rc.take(); - rc.markForDisposal(); - - reference[Symbol.dispose](); - - expect(value.isFreed).toBe(true); - }); - - it("should free value when marked for disposal if refCount is 0", () => { - // eslint-disable-next-line @bitwarden/platform/required-using - const reference = rc.take(); - reference[Symbol.dispose](); - - rc.markForDisposal(); - - expect(value.isFreed).toBe(true); - }); - - it("should throw error when trying to take a disposed reference", () => { - rc.markForDisposal(); - - expect(() => rc.take()).toThrow(); - }); }); diff --git a/libs/common/src/platform/misc/reference-counting/rc.ts b/libs/common/src/platform/misc/reference-counting/rc.ts index 3ce77d19ed6..d64b505d541 100644 --- a/libs/common/src/platform/misc/reference-counting/rc.ts +++ b/libs/common/src/platform/misc/reference-counting/rc.ts @@ -1,5 +1,3 @@ -import { UsingRequired } from "../using-required"; - export type Freeable = { free: () => void }; type UseReturnValue = T extends (value: any) => Promise ? Promise : void; @@ -51,33 +49,6 @@ export class Rc { } } - /** - * Use this function when you want to use the underlying object. - * This will guarantee that you have a reference to the object - * and that it won't be freed until your reference goes out of scope. - * - * This function must be used with the `using` keyword. - * - * @example - * ```typescript - * function someFunction(rc: Rc) { - * using reference = rc.take(); - * reference.value.doSomething(); - * // reference is automatically disposed here - * } - * ``` - * - * @returns The value. - */ - take(): Ref { - if (this.markedForDisposal) { - throw new Error("Cannot take a reference to a value marked for disposal"); - } - - this.refCount++; - return new Ref(() => this.release(), this.value); - } - /** * Mark this Rc for disposal. When the refCount reaches 0, the value * will be freed. @@ -98,14 +69,3 @@ export class Rc { } } } - -export class Ref implements UsingRequired { - constructor( - private readonly release: () => void, - readonly value: T, - ) {} - - [Symbol.dispose]() { - this.release(); - } -}