mirror of
https://github.com/bitwarden/browser
synced 2026-02-10 13:40:06 +00:00
fix: remove the ability to use Rc with using
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { UsingRequired } from "../using-required";
|
||||
|
||||
export type Freeable = { free: () => void };
|
||||
|
||||
type UseReturnValue<T> = T extends (value: any) => Promise<unknown> ? Promise<void> : void;
|
||||
@@ -51,33 +49,6 @@ export class Rc<T extends Freeable> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<SomeValue>) {
|
||||
* using reference = rc.take();
|
||||
* reference.value.doSomething();
|
||||
* // reference is automatically disposed here
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @returns The value.
|
||||
*/
|
||||
take(): Ref<T> {
|
||||
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<T extends Freeable> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Ref<T extends Freeable> implements UsingRequired {
|
||||
constructor(
|
||||
private readonly release: () => void,
|
||||
readonly value: T,
|
||||
) {}
|
||||
|
||||
[Symbol.dispose]() {
|
||||
this.release();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user