export class AssetHandle {
    id;
    url;
    type;
    valueRef;
    disposer;
    retainCount = 1;
    currentStatus = "ready";
    constructor(options) {
        this.id = options.id;
        this.url = options.url;
        this.type = options.type;
        this.valueRef = options.value;
        this.disposer = options.dispose;
    }
    get status() {
        return this.currentStatus;
    }
    get refCount() {
        return this.retainCount;
    }
    get disposed() {
        return this.currentStatus === "disposed";
    }
    get value() {
        this.assertAlive();
        return this.valueRef;
    }
    retain() {
        this.assertAlive();
        this.retainCount += 1;
        return this;
    }
    async release() {
        this.assertAlive();
        this.retainCount -= 1;
        if (this.retainCount < 0) {
            this.retainCount = 0;
            throw new Error(`Asset handle ${this.id} was released too many times`);
        }
        if (this.retainCount === 0) {
            this.currentStatus = "disposed";
            await this.disposer?.(this.valueRef);
        }
        return this.retainCount;
    }
    assertAlive() {
        if (this.currentStatus === "disposed") {
            throw new Error(`Asset handle ${this.id} for ${this.url} has been disposed`);
        }
    }
}
//# sourceMappingURL=AssetHandle.js.map