/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */ /** */
import { Observable } from '@babylonjs/core/Misc/observable';
/**
 * A growable typed array.
 */
export class LazyResizableTypedArray {
    _factory;
    _uint8;
    /**
     * Direct access to the Typed array. Reference may change if {@link setMinLength} is called.
     * Therefore avoid storing a refernce to this buffer in other places.
     */
    buffer;
    /** Triggered after (and if) buffer has been resized when {@link setMinLength} was called. */
    onResized = new Observable();
    /** Constructor
     * @param _factory Array creation factory. Allows us to create any kind of resizable typed array.
     */
    constructor(_factory) {
        this._factory = _factory;
        this.buffer = _factory(0);
        this._uint8 = new Uint8Array(this.buffer.buffer);
    }
    /**
     * Uint8Array view of {@link buffer}.
     * @returns Uint8Array wrapping {@link buffer}.
     */
    get uint8() {
        return this._uint8;
    }
    /** `true` if {@link buffer} is empty. */
    get isEmpty() {
        return this.buffer.length === 0;
    }
    /**
     * Sets minimum length of array. Array is only resized if `minLength` is larger than
     * current array size.
     * @param minLength New minimal size of array.
     * @returns `true` if array was resized.
     */
    setMinLength(minLength) {
        const isResized = minLength > this.buffer.length;
        if (isResized) {
            const tmp = this.buffer;
            this.buffer = this._factory(minLength);
            // TODO This copy can perhaps be made faster by copying 8 bytes at a time.
            this.buffer.set(tmp);
            this._uint8 = new Uint8Array(this.buffer.buffer);
            this.onResized.notifyObservers(this);
        }
        return isResized;
    }
    /** Empties array. ({@link buffer} is set to zero sized array.) */
    reset() {
        this.buffer = this._factory(0);
        this._uint8 = new Uint8Array(this.buffer.buffer);
    }
}
//# sourceMappingURL=LazyResizableTypedArray.js.map