readline-pager 0.4.8 → 0.4.9

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.
package/dist/main.cjs CHANGED
@@ -48,8 +48,9 @@ function createRingBuffer(capacity) {
48
48
  }
49
49
  return v;
50
50
  }
51
- async function shift() {
51
+ async function shift(done) {
52
52
  if (count) return shiftSync();
53
+ if (done) return null;
53
54
  await new Promise((r) => {
54
55
  consumerWaiter = r;
55
56
  });
@@ -214,7 +215,7 @@ function createBackwardReader(filepath, options) {
214
215
  }
215
216
  async function next() {
216
217
  if (closed) return null;
217
- return await pageQueue.shift();
218
+ return pageQueue.shift(done);
218
219
  }
219
220
  function nextSync() {
220
221
  if (closed) return null;
@@ -394,7 +395,7 @@ function createForwardReader(filepath, options) {
394
395
  }
395
396
  async function next() {
396
397
  if (closed) return null;
397
- return await pageQueue.shift();
398
+ return pageQueue.shift(done);
398
399
  }
399
400
  function nextSync() {
400
401
  if (closed) return null;
@@ -462,6 +463,7 @@ const workerFile = new URL("./worker.mjs", require("url").pathToFileURL(__filena
462
463
  function createWorkerReader(filepath, options) {
463
464
  const { prefetch } = options;
464
465
  const pageQueue = createRingBuffer(Math.max(2, prefetch + 1));
466
+ let done = false;
465
467
  let closed = false;
466
468
  const worker = new node_worker_threads.Worker(new URL(workerFile, require("url").pathToFileURL(__filename).href), { workerData: {
467
469
  filepath,
@@ -469,17 +471,22 @@ function createWorkerReader(filepath, options) {
469
471
  } });
470
472
  worker.on("message", (msg) => {
471
473
  if (msg.type === "page") pageQueue.push(msg.data);
472
- if (msg.type === "done") pageQueue.wake();
474
+ if (msg.type === "done") {
475
+ done = true;
476
+ pageQueue.wake();
477
+ }
473
478
  });
474
479
  worker.on("error", () => {
480
+ done = true;
475
481
  pageQueue.wake();
476
482
  });
477
483
  worker.on("exit", () => {
484
+ done = true;
478
485
  pageQueue.wake();
479
486
  });
480
487
  async function next() {
481
488
  if (closed) return null;
482
- return await pageQueue.shift();
489
+ return pageQueue.shift(done);
483
490
  }
484
491
  function nextSync() {
485
492
  if (closed) return null;
@@ -487,6 +494,7 @@ function createWorkerReader(filepath, options) {
487
494
  }
488
495
  async function close() {
489
496
  closed = true;
497
+ done = true;
490
498
  pageQueue.clear();
491
499
  await worker.terminate();
492
500
  }
package/dist/main.mjs CHANGED
@@ -48,8 +48,9 @@ function createRingBuffer(capacity) {
48
48
  }
49
49
  return v;
50
50
  }
51
- async function shift() {
51
+ async function shift(done) {
52
52
  if (count) return shiftSync();
53
+ if (done) return null;
53
54
  await new Promise((r) => {
54
55
  consumerWaiter = r;
55
56
  });
@@ -214,7 +215,7 @@ function createBackwardReader(filepath, options) {
214
215
  }
215
216
  async function next() {
216
217
  if (closed) return null;
217
- return await pageQueue.shift();
218
+ return pageQueue.shift(done);
218
219
  }
219
220
  function nextSync() {
220
221
  if (closed) return null;
@@ -394,7 +395,7 @@ function createForwardReader(filepath, options) {
394
395
  }
395
396
  async function next() {
396
397
  if (closed) return null;
397
- return await pageQueue.shift();
398
+ return pageQueue.shift(done);
398
399
  }
399
400
  function nextSync() {
400
401
  if (closed) return null;
@@ -462,6 +463,7 @@ const workerFile = typeof import.meta !== "undefined" ? new URL("./worker.mjs",
462
463
  function createWorkerReader(filepath, options) {
463
464
  const { prefetch } = options;
464
465
  const pageQueue = createRingBuffer(Math.max(2, prefetch + 1));
466
+ let done = false;
465
467
  let closed = false;
466
468
  const worker = new Worker(new URL(workerFile, import.meta.url), { workerData: {
467
469
  filepath,
@@ -469,17 +471,22 @@ function createWorkerReader(filepath, options) {
469
471
  } });
470
472
  worker.on("message", (msg) => {
471
473
  if (msg.type === "page") pageQueue.push(msg.data);
472
- if (msg.type === "done") pageQueue.wake();
474
+ if (msg.type === "done") {
475
+ done = true;
476
+ pageQueue.wake();
477
+ }
473
478
  });
474
479
  worker.on("error", () => {
480
+ done = true;
475
481
  pageQueue.wake();
476
482
  });
477
483
  worker.on("exit", () => {
484
+ done = true;
478
485
  pageQueue.wake();
479
486
  });
480
487
  async function next() {
481
488
  if (closed) return null;
482
- return await pageQueue.shift();
489
+ return pageQueue.shift(done);
483
490
  }
484
491
  function nextSync() {
485
492
  if (closed) return null;
@@ -487,6 +494,7 @@ function createWorkerReader(filepath, options) {
487
494
  }
488
495
  async function close() {
489
496
  closed = true;
497
+ done = true;
490
498
  pageQueue.clear();
491
499
  await worker.terminate();
492
500
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "readline-pager",
3
- "version": "0.4.8",
3
+ "version": "0.4.9",
4
4
  "scripts": {
5
5
  "build:js": "tsdown",
6
6
  "build:native": "node-gyp rebuild",
@@ -24,10 +24,10 @@
24
24
  "typescript": "~6.0.2"
25
25
  },
26
26
  "optionalDependencies": {
27
- "@devmor-j/readline-pager-linux-arm64": "0.4.8",
28
- "@devmor-j/readline-pager-linux-x64": "0.4.8",
29
- "@devmor-j/readline-pager-linux-musl-arm64": "0.4.8",
30
- "@devmor-j/readline-pager-linux-musl-x64": "0.4.8"
27
+ "@devmor-j/readline-pager-linux-arm64": "0.4.9",
28
+ "@devmor-j/readline-pager-linux-x64": "0.4.9",
29
+ "@devmor-j/readline-pager-linux-musl-arm64": "0.4.9",
30
+ "@devmor-j/readline-pager-linux-musl-x64": "0.4.9"
31
31
  },
32
32
  "gypfile": false,
33
33
  "type": "module",