import '../../../assert/js/assert.js';
import '../../../globals.js';
import { alpenglow } from '../imports.js';
export default class ParallelStorageArray {
    constructor(data, indeterminateValue) {
        this.data = data;
        this.indeterminateValue = indeterminateValue;
        this.writeLocalIDs = data.map(() => []);
    }
    async get(context, index) {
        let value;
        if (!isFinite(index) || index < 0 || index >= this.data.length) {
            value = this.indeterminateValue;
        }
        else {
            // Ensure that we're the only ones who have written to this
            assert && assert(this.writeLocalIDs[index].every(id => id.local.equals(context.localId) && id.workgroup.equals(context.workgroupId)));
            value = this.data[index];
        }
        await context.afterGet();
        return value;
    }
    async set(context, index, value) {
        if (isFinite(index) && index >= 0 && index < this.data.length) {
            // Ensure that we're the only ones who have written to this
            assert && assert(this.writeLocalIDs[index].every(id => id.local.equals(context.localId) && id.workgroup.equals(context.workgroupId)));
            this.writeLocalIDs[index].push({ local: context.localId, workgroup: context.workgroupId });
            this.data[index] = value;
        }
        await context.afterSet();
    }
    synchronize(workgroupId) {
        // Clear IDs that were written from that workgroup
        for (let i = this.writeLocalIDs.length - 1; i >= 0; i--) {
            const ids = this.writeLocalIDs[i];
            for (let j = ids.length - 1; j >= 0; j--) {
                const id = ids[j];
                if (id.workgroup.equals(workgroupId)) {
                    ids.splice(j, 1);
                }
            }
        }
    }
    synchronizeFull() {
        // Clear IDs that were written from any workgroup
        this.writeLocalIDs.forEach(ids => {
            ids.length = 0;
        });
    }
}
alpenglow.register('ParallelStorageArray', ParallelStorageArray);
//# sourceMappingURL=ParallelStorageArray.js.map