Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

async.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const events_1 = require("events");
  4. const fsScandir = require("@nodelib/fs.scandir");
  5. const fastq = require("fastq");
  6. const common = require("./common");
  7. const reader_1 = require("./reader");
  8. class AsyncReader extends reader_1.default {
  9. constructor(_root, _settings) {
  10. super(_root, _settings);
  11. this._settings = _settings;
  12. this._scandir = fsScandir.scandir;
  13. this._emitter = new events_1.EventEmitter();
  14. this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
  15. this._isFatalError = false;
  16. this._isDestroyed = false;
  17. this._queue.drain = () => {
  18. if (!this._isFatalError) {
  19. this._emitter.emit('end');
  20. }
  21. };
  22. }
  23. read() {
  24. this._isFatalError = false;
  25. this._isDestroyed = false;
  26. setImmediate(() => {
  27. this._pushToQueue(this._root, this._settings.basePath);
  28. });
  29. return this._emitter;
  30. }
  31. get isDestroyed() {
  32. return this._isDestroyed;
  33. }
  34. destroy() {
  35. if (this._isDestroyed) {
  36. throw new Error('The reader is already destroyed');
  37. }
  38. this._isDestroyed = true;
  39. this._queue.killAndDrain();
  40. }
  41. onEntry(callback) {
  42. this._emitter.on('entry', callback);
  43. }
  44. onError(callback) {
  45. this._emitter.once('error', callback);
  46. }
  47. onEnd(callback) {
  48. this._emitter.once('end', callback);
  49. }
  50. _pushToQueue(directory, base) {
  51. const queueItem = { directory, base };
  52. this._queue.push(queueItem, (error) => {
  53. if (error !== null) {
  54. this._handleError(error);
  55. }
  56. });
  57. }
  58. _worker(item, done) {
  59. this._scandir(item.directory, this._settings.fsScandirSettings, (error, entries) => {
  60. if (error !== null) {
  61. done(error, undefined);
  62. return;
  63. }
  64. for (const entry of entries) {
  65. this._handleEntry(entry, item.base);
  66. }
  67. done(null, undefined);
  68. });
  69. }
  70. _handleError(error) {
  71. if (this._isDestroyed || !common.isFatalError(this._settings, error)) {
  72. return;
  73. }
  74. this._isFatalError = true;
  75. this._isDestroyed = true;
  76. this._emitter.emit('error', error);
  77. }
  78. _handleEntry(entry, base) {
  79. if (this._isDestroyed || this._isFatalError) {
  80. return;
  81. }
  82. const fullpath = entry.path;
  83. if (base !== undefined) {
  84. entry.path = common.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  85. }
  86. if (common.isAppliedFilter(this._settings.entryFilter, entry)) {
  87. this._emitEntry(entry);
  88. }
  89. if (entry.dirent.isDirectory() && common.isAppliedFilter(this._settings.deepFilter, entry)) {
  90. this._pushToQueue(fullpath, entry.path);
  91. }
  92. }
  93. _emitEntry(entry) {
  94. this._emitter.emit('entry', entry);
  95. }
  96. }
  97. exports.default = AsyncReader;