minerender 2.1.0-alpha.35 → 2.1.0-alpha.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -584,13 +584,11 @@ declare class BatchedExecutor {
584
584
  readonly interval: number;
585
585
  readonly batch: number;
586
586
  private readonly queue;
587
- private readonly task;
588
587
  constructor(interval?: number, batch?: number);
589
- submit(task: Task): void;
590
- private run;
591
- private runNext;
588
+ submit<T>(task: Task<T>): Promise<T>;
589
+ stop(): void;
592
590
  }
593
- type Task = () => any | Promise<any>;
591
+ type Task<T = unknown> = () => T | Promise<T>;
594
592
 
595
593
  declare function prefix(prefix?: string): string;
596
594
 
@@ -584,13 +584,11 @@ declare class BatchedExecutor {
584
584
  readonly interval: number;
585
585
  readonly batch: number;
586
586
  private readonly queue;
587
- private readonly task;
588
587
  constructor(interval?: number, batch?: number);
589
- submit(task: Task): void;
590
- private run;
591
- private runNext;
588
+ submit<T>(task: Task<T>): Promise<T>;
589
+ stop(): void;
592
590
  }
593
- type Task = () => any | Promise<any>;
591
+ type Task<T = unknown> = () => T | Promise<T>;
594
592
 
595
593
  declare function prefix(prefix?: string): string;
596
594
 
@@ -1655,24 +1655,20 @@ var AssetLoadError = class extends Error {
1655
1655
  };
1656
1656
 
1657
1657
  // src/util/BatchedExecutor.ts
1658
+ var import_jobqu2 = require("jobqu");
1658
1659
  var BatchedExecutor = class {
1659
1660
  constructor(interval = 1, batch = 30) {
1661
+ if (!Number.isFinite(interval) || interval < 0) throw new RangeError("Batch interval must be finite and nonnegative");
1662
+ if (!Number.isInteger(batch) || batch < 1) throw new RangeError("Batch size must be a positive integer");
1660
1663
  this.interval = interval;
1661
1664
  this.batch = batch;
1662
- this.queue = [];
1663
- this.task = setInterval(() => this.run());
1665
+ this.queue = new import_jobqu2.JobQueue(async (task) => task(), { interval, maxPerRun: batch, maxActive: batch });
1664
1666
  }
1665
1667
  submit(task) {
1666
- this.queue.push(task);
1668
+ return this.queue.add(() => task());
1667
1669
  }
1668
- run() {
1669
- for (let i = 0; i < this.batch; i++) {
1670
- this.runNext();
1671
- }
1672
- }
1673
- runNext() {
1674
- let next = this.queue.shift();
1675
- if (next) next();
1670
+ stop() {
1671
+ this.queue.end();
1676
1672
  }
1677
1673
  };
1678
1674
 
@@ -7364,22 +7360,39 @@ var MineRenderWorld = class {
7364
7360
  const chunk = Chunk.isAir(value) ? this.getChunkAt(posOrX) : this.getOrCreateChunkAt(posOrX);
7365
7361
  return chunk?.setBlockAt(posOrX, value);
7366
7362
  }
7367
- async placeMultiBlock(multiblock, useBatches = true, executor = new BatchedExecutor()) {
7368
- const keys = new Set(multiblock.blocks.map((block) => AssetKey.parse("blockstates", block.type)));
7369
- await BlockStates.getAll(keys);
7370
- const place = async (block) => {
7371
- if (useBatches && typeof executor !== "undefined") {
7372
- await new Promise((resolve, reject) => {
7373
- executor.submit(() => {
7374
- this.setBlockAt(block.position, block).then(resolve).catch(reject);
7375
- });
7376
- });
7377
- } else {
7378
- await this.setBlockAt(block.position, block);
7363
+ async placeMultiBlock(multiblock, useBatches = true, executor) {
7364
+ const keys = /* @__PURE__ */ new Map();
7365
+ for (const block of multiblock.blocks) {
7366
+ if (Chunk.isAir(block)) continue;
7367
+ const key = AssetKey.parse("blockstates", block.type);
7368
+ keys.set(key.serialize(), key);
7369
+ }
7370
+ await BlockStates.getAll(keys.values());
7371
+ if (!useBatches) {
7372
+ for (const block of multiblock.blocks) await this.setBlockAt(block.position, block);
7373
+ return;
7374
+ }
7375
+ const positions = /* @__PURE__ */ new Map();
7376
+ for (const block of multiblock.blocks) {
7377
+ const key = block.position.join(",");
7378
+ const group = positions.get(key);
7379
+ if (group) group.push(block);
7380
+ else positions.set(key, [block]);
7381
+ }
7382
+ const groups = [...positions.values()];
7383
+ const queue = executor ?? new BatchedExecutor();
7384
+ try {
7385
+ for (let i = 0; i < groups.length; i += queue.batch) {
7386
+ const results = await Promise.allSettled(groups.slice(i, i + queue.batch).map(
7387
+ (blocks) => queue.submit(async () => {
7388
+ for (const block of blocks) await this.setBlockAt(block.position, block);
7389
+ })
7390
+ ));
7391
+ const failure = results.find((result) => result.status === "rejected");
7392
+ if (failure?.status === "rejected") throw failure.reason;
7379
7393
  }
7380
- };
7381
- for (let block of multiblock.blocks) {
7382
- await place(block);
7394
+ } finally {
7395
+ if (!executor) queue.stop();
7383
7396
  }
7384
7397
  }
7385
7398
  async clear() {