@needle-tools/gltf-progressive 3.0.1 → 3.1.0

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.
@@ -4,3 +4,23 @@ export declare function resolveUrl(source: string | undefined, uri: string): str
4
4
  /** @returns `true` if it's a phone or tablet */
5
5
  export declare function isMobileDevice(): boolean;
6
6
  export declare function isDevelopmentServer(): boolean;
7
+ type SlotReturnValue = {
8
+ use?: ((promise: Promise<any>) => void);
9
+ };
10
+ export declare class PromiseQueue {
11
+ readonly maxConcurrent: number;
12
+ private readonly _running;
13
+ private readonly _queue;
14
+ debug: boolean;
15
+ constructor(maxConcurrent?: number, opts?: {
16
+ debug?: boolean;
17
+ });
18
+ private tick;
19
+ /**
20
+ * Request a slot for a promise with a specific key. This function returns a promise with a `use` method that can be called to add the promise to the queue.
21
+ */
22
+ slot(key: string): Promise<SlotReturnValue>;
23
+ private add;
24
+ private internalUpdate;
25
+ }
26
+ export {};
@@ -58,3 +58,52 @@ export function isDevelopmentServer() {
58
58
  const isDevelopment = url.hostname === "127.0.0.1" || isLocalhostOrIpAddress;
59
59
  return isDevelopment;
60
60
  }
61
+ export class PromiseQueue {
62
+ maxConcurrent;
63
+ _running = new Map();
64
+ _queue = [];
65
+ debug = false;
66
+ constructor(maxConcurrent = 100, opts = {}) {
67
+ this.maxConcurrent = maxConcurrent;
68
+ this.debug = opts.debug ?? false;
69
+ window.requestAnimationFrame(this.tick);
70
+ }
71
+ tick = () => {
72
+ this.internalUpdate();
73
+ setTimeout(this.tick, 10);
74
+ };
75
+ /**
76
+ * Request a slot for a promise with a specific key. This function returns a promise with a `use` method that can be called to add the promise to the queue.
77
+ */
78
+ slot(key) {
79
+ if (this.debug)
80
+ console.debug(`[PromiseQueue]: Requesting slot for key ${key}, running: ${this._running.size}, waiting: ${this._queue.length}`);
81
+ return new Promise((resolve) => {
82
+ this._queue.push({ key, resolve });
83
+ });
84
+ }
85
+ add(key, promise) {
86
+ if (this._running.has(key))
87
+ return;
88
+ this._running.set(key, promise);
89
+ promise.finally(() => {
90
+ this._running.delete(key);
91
+ if (this.debug)
92
+ console.debug(`[PromiseQueue]: Promise for key ${key} finished, running: ${this._running.size}, waiting: ${this._queue.length}`);
93
+ });
94
+ if (this.debug)
95
+ console.debug(`[PromiseQueue]: Adding promise for key ${key}, running: ${this._running.size}, waiting: ${this._queue.length}`);
96
+ }
97
+ internalUpdate() {
98
+ // Run for as many free slots as we can
99
+ const diff = this.maxConcurrent - this._running.size;
100
+ for (let i = 0; i < diff && this._queue.length > 0; i++) {
101
+ if (this.debug)
102
+ console.debug(`[PromiseQueue]: Running ${this._running.size} promises, waiting for ${this._queue.length} more.`);
103
+ const { key, resolve } = this._queue.shift();
104
+ resolve({
105
+ use: (promise) => this.add(key, promise)
106
+ });
107
+ }
108
+ }
109
+ }
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // replaced at build time
2
- export const version = "3.0.1";
2
+ export const version = "3.1.0";
3
3
  globalThis["GLTF_PROGRESSIVE_VERSION"] = version;
4
4
  console.debug(`[gltf-progressive] version ${version || "-"}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/gltf-progressive",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "three.js support for loading glTF or GLB files that contain progressive loading data",
5
5
  "homepage": "https://needle.tools",
6
6
  "author": {