index.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TaskInfo = exports.FixedQueue = exports.ArrayTaskQueue = void 0;
  4. exports.isTaskQueue = isTaskQueue;
  5. const node_perf_hooks_1 = require("node:perf_hooks");
  6. const node_async_hooks_1 = require("node:async_hooks");
  7. const common_1 = require("../common");
  8. const symbols_1 = require("../symbols");
  9. var array_queue_1 = require("./array_queue");
  10. Object.defineProperty(exports, "ArrayTaskQueue", { enumerable: true, get: function () { return array_queue_1.ArrayTaskQueue; } });
  11. var fixed_queue_1 = require("./fixed_queue");
  12. Object.defineProperty(exports, "FixedQueue", { enumerable: true, get: function () { return fixed_queue_1.FixedQueue; } });
  13. /**
  14. * Verifies if a given TaskQueue is valid
  15. *
  16. * @export
  17. * @param {*} value
  18. * @return {*} {boolean}
  19. */
  20. function isTaskQueue(value) {
  21. return (typeof value === 'object' &&
  22. value !== null &&
  23. 'size' in value &&
  24. typeof value.shift === 'function' &&
  25. typeof value.remove === 'function' &&
  26. typeof value.push === 'function');
  27. }
  28. let taskIdCounter = 0;
  29. // Extend AsyncResource so that async relations between posting a task and
  30. // receiving its result are visible to diagnostic tools.
  31. class TaskInfo extends node_async_hooks_1.AsyncResource {
  32. constructor(task, transferList, filename, name, callback, abortSignal, triggerAsyncId) {
  33. super('Piscina.Task', { requireManualDestroy: true, triggerAsyncId });
  34. this.abortListener = null;
  35. this.workerInfo = null;
  36. this.callback = callback;
  37. this.task = task;
  38. this.transferList = transferList;
  39. // If the task is a Transferable returned by
  40. // Piscina.move(), then add it to the transferList
  41. // automatically
  42. if ((0, common_1.isMovable)(task)) {
  43. // This condition should never be hit but typescript
  44. // complains if we dont do the check.
  45. /* istanbul ignore if */
  46. if (this.transferList == null) {
  47. this.transferList = [];
  48. }
  49. this.transferList =
  50. this.transferList.concat(task[symbols_1.kTransferable]);
  51. this.task = task[symbols_1.kValue];
  52. }
  53. this.filename = filename;
  54. this.name = name;
  55. this.taskId = taskIdCounter++;
  56. this.abortSignal = abortSignal;
  57. this.created = node_perf_hooks_1.performance.now();
  58. this.started = 0;
  59. }
  60. releaseTask() {
  61. const ret = this.task;
  62. this.task = null;
  63. return ret;
  64. }
  65. done(err, result) {
  66. this.runInAsyncScope(this.callback, null, err, result);
  67. this.emitDestroy(); // `TaskInfo`s are used only once.
  68. // If an abort signal was used, remove the listener from it when
  69. // done to make sure we do not accidentally leak.
  70. if (this.abortSignal && this.abortListener) {
  71. if ('removeEventListener' in this.abortSignal && this.abortListener) {
  72. this.abortSignal.removeEventListener('abort', this.abortListener);
  73. }
  74. else {
  75. this.abortSignal.off('abort', this.abortListener);
  76. }
  77. }
  78. }
  79. get [symbols_1.kQueueOptions]() {
  80. return symbols_1.kQueueOptions in this.task ? this.task[symbols_1.kQueueOptions] : null;
  81. }
  82. get interface() {
  83. return {
  84. taskId: this.taskId,
  85. filename: this.filename,
  86. name: this.name,
  87. created: this.created,
  88. isAbortable: this.abortSignal !== null,
  89. [symbols_1.kQueueOptions]: this[symbols_1.kQueueOptions]
  90. };
  91. }
  92. }
  93. exports.TaskInfo = TaskInfo;
  94. //# sourceMappingURL=index.js.map