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.
@@ -585,13 +585,11 @@ declare class BatchedExecutor {
585
585
  readonly interval: number;
586
586
  readonly batch: number;
587
587
  private readonly queue;
588
- private readonly task;
589
588
  constructor(interval?: number, batch?: number);
590
- submit(task: Task): void;
591
- private run;
592
- private runNext;
589
+ submit<T>(task: Task<T>): Promise<T>;
590
+ stop(): void;
593
591
  }
594
- type Task = () => any | Promise<any>;
592
+ type Task<T = unknown> = () => T | Promise<T>;
595
593
 
596
594
  declare function prefix(prefix?: string): string;
597
595
 
@@ -585,13 +585,11 @@ declare class BatchedExecutor {
585
585
  readonly interval: number;
586
586
  readonly batch: number;
587
587
  private readonly queue;
588
- private readonly task;
589
588
  constructor(interval?: number, batch?: number);
590
- submit(task: Task): void;
591
- private run;
592
- private runNext;
589
+ submit<T>(task: Task<T>): Promise<T>;
590
+ stop(): void;
593
591
  }
594
- type Task = () => any | Promise<any>;
592
+ type Task<T = unknown> = () => T | Promise<T>;
595
593
 
596
594
  declare function prefix(prefix?: string): string;
597
595
 
@@ -1661,24 +1661,20 @@ var AssetLoadError = class extends Error {
1661
1661
  };
1662
1662
 
1663
1663
  // src/util/BatchedExecutor.ts
1664
+ var import_jobqu2 = require("jobqu");
1664
1665
  var BatchedExecutor = class {
1665
1666
  constructor(interval = 1, batch = 30) {
1667
+ if (!Number.isFinite(interval) || interval < 0) throw new RangeError("Batch interval must be finite and nonnegative");
1668
+ if (!Number.isInteger(batch) || batch < 1) throw new RangeError("Batch size must be a positive integer");
1666
1669
  this.interval = interval;
1667
1670
  this.batch = batch;
1668
- this.queue = [];
1669
- this.task = setInterval(() => this.run());
1671
+ this.queue = new import_jobqu2.JobQueue(async (task) => task(), { interval, maxPerRun: batch, maxActive: batch });
1670
1672
  }
1671
1673
  submit(task) {
1672
- this.queue.push(task);
1674
+ return this.queue.add(() => task());
1673
1675
  }
1674
- run() {
1675
- for (let i = 0; i < this.batch; i++) {
1676
- this.runNext();
1677
- }
1678
- }
1679
- runNext() {
1680
- let next = this.queue.shift();
1681
- if (next) next();
1676
+ stop() {
1677
+ this.queue.end();
1682
1678
  }
1683
1679
  };
1684
1680
 
@@ -7413,22 +7409,39 @@ var MineRenderWorld = class {
7413
7409
  const chunk = Chunk.isAir(value) ? this.getChunkAt(posOrX) : this.getOrCreateChunkAt(posOrX);
7414
7410
  return chunk?.setBlockAt(posOrX, value);
7415
7411
  }
7416
- async placeMultiBlock(multiblock, useBatches = true, executor = new BatchedExecutor()) {
7417
- const keys = new Set(multiblock.blocks.map((block) => AssetKey.parse("blockstates", block.type)));
7418
- await BlockStates.getAll(keys);
7419
- const place = async (block) => {
7420
- if (useBatches && typeof executor !== "undefined") {
7421
- await new Promise((resolve, reject) => {
7422
- executor.submit(() => {
7423
- this.setBlockAt(block.position, block).then(resolve).catch(reject);
7424
- });
7425
- });
7426
- } else {
7427
- await this.setBlockAt(block.position, block);
7412
+ async placeMultiBlock(multiblock, useBatches = true, executor) {
7413
+ const keys = /* @__PURE__ */ new Map();
7414
+ for (const block of multiblock.blocks) {
7415
+ if (Chunk.isAir(block)) continue;
7416
+ const key = AssetKey.parse("blockstates", block.type);
7417
+ keys.set(key.serialize(), key);
7418
+ }
7419
+ await BlockStates.getAll(keys.values());
7420
+ if (!useBatches) {
7421
+ for (const block of multiblock.blocks) await this.setBlockAt(block.position, block);
7422
+ return;
7423
+ }
7424
+ const positions = /* @__PURE__ */ new Map();
7425
+ for (const block of multiblock.blocks) {
7426
+ const key = block.position.join(",");
7427
+ const group = positions.get(key);
7428
+ if (group) group.push(block);
7429
+ else positions.set(key, [block]);
7430
+ }
7431
+ const groups = [...positions.values()];
7432
+ const queue = executor ?? new BatchedExecutor();
7433
+ try {
7434
+ for (let i = 0; i < groups.length; i += queue.batch) {
7435
+ const results = await Promise.allSettled(groups.slice(i, i + queue.batch).map(
7436
+ (blocks) => queue.submit(async () => {
7437
+ for (const block of blocks) await this.setBlockAt(block.position, block);
7438
+ })
7439
+ ));
7440
+ const failure = results.find((result) => result.status === "rejected");
7441
+ if (failure?.status === "rejected") throw failure.reason;
7428
7442
  }
7429
- };
7430
- for (let block of multiblock.blocks) {
7431
- await place(block);
7443
+ } finally {
7444
+ if (!executor) queue.stop();
7432
7445
  }
7433
7446
  }
7434
7447
  async clear() {