CoreFileSystemObserver.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CoreFileSystemObserver = exports.FileSystemChangeRecord = void 0;
  4. const CoreFileSystemDirectoryHandle_1 = require("./CoreFileSystemDirectoryHandle");
  5. const CoreFileSystemFileHandle_1 = require("./CoreFileSystemFileHandle");
  6. const util_1 = require("./util");
  7. const fs_core_1 = require("@jsonjoy.com/fs-core");
  8. /**
  9. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemChangeRecord
  10. */
  11. class FileSystemChangeRecord {
  12. constructor(root, type, changedHandle, relativePathComponents, relativePathMovedFrom = null) {
  13. this.root = root;
  14. this.type = type;
  15. this.changedHandle = changedHandle;
  16. this.relativePathComponents = relativePathComponents;
  17. this.relativePathMovedFrom = relativePathMovedFrom;
  18. }
  19. }
  20. exports.FileSystemChangeRecord = FileSystemChangeRecord;
  21. /**
  22. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemObserver
  23. */
  24. class CoreFileSystemObserver {
  25. constructor(_core, callback) {
  26. this._core = _core;
  27. this.callback = callback;
  28. this._observations = new Map();
  29. this._records = [];
  30. this._flushScheduled = false;
  31. }
  32. async observe(handle, options) {
  33. const path = handle.__path;
  34. if (typeof path !== 'string')
  35. throw new TypeError("Failed to execute 'observe' on 'FileSystemObserver': Invalid handle.");
  36. const isDirectory = handle.kind === 'directory';
  37. const last = path[path.length - 1];
  38. const target = path.length > 1 && (last === '/' || last === '\\') ? path.slice(0, -1) : path;
  39. const ctx = (handle.ctx ?? handle._ctx);
  40. try {
  41. const watcher = new fs_core_1.CoreWatcher(this._core, target, { recursive: isDirectory && !!options?.recursive });
  42. watcher.changes.listen(event => this._onEvent(event, handle, watcher, ctx));
  43. this._observations.get(handle)?.close();
  44. this._observations.set(handle, watcher);
  45. }
  46. catch (error) {
  47. if (error && typeof error === 'object') {
  48. switch (error.code) {
  49. case "ENOENT" /* ERROR_CODE.ENOENT */:
  50. throw (0, util_1.newNotFoundError)();
  51. case "EACCES" /* ERROR_CODE.EACCES */:
  52. throw (0, util_1.newNotAllowedError)();
  53. }
  54. }
  55. throw error;
  56. }
  57. }
  58. unobserve(handle) {
  59. const watcher = this._observations.get(handle);
  60. if (!watcher)
  61. return;
  62. watcher.close();
  63. this._observations.delete(handle);
  64. }
  65. /** Disconnect and stop all observations. */
  66. disconnect() {
  67. for (const watcher of this._observations.values())
  68. watcher.close();
  69. this._observations.clear();
  70. this._records = [];
  71. }
  72. _onEvent(event, root, watcher, ctx) {
  73. let type;
  74. let changedHandle = null;
  75. let relativePathMovedFrom = null;
  76. switch (event.type) {
  77. case 0 /* FsEventType.CREATE */:
  78. type = 'appeared';
  79. changedHandle = this._changedHandle(event, watcher, ctx);
  80. break;
  81. case 1 /* FsEventType.DELETE */:
  82. if (!event.steps.length) {
  83. type = 'errored';
  84. if (this._observations.get(root) === watcher)
  85. this._observations.delete(root);
  86. }
  87. else {
  88. type = 'disappeared';
  89. }
  90. break;
  91. case 4 /* FsEventType.MOVE */:
  92. type = 'moved';
  93. changedHandle = this._changedHandle(event, watcher, ctx);
  94. relativePathMovedFrom = event.oldSteps ?? null;
  95. break;
  96. default:
  97. type = 'modified';
  98. changedHandle = this._changedHandle(event, watcher, ctx);
  99. }
  100. this._enqueue(new FileSystemChangeRecord(root, type, changedHandle, event.steps, relativePathMovedFrom));
  101. }
  102. _changedHandle(event, watcher, ctx) {
  103. const sep = ctx.separator;
  104. const path = watcher.link.steps.concat(event.steps).join(sep) || sep;
  105. return event.node.isDirectory()
  106. ? new CoreFileSystemDirectoryHandle_1.CoreFileSystemDirectoryHandle(this._core, path, ctx)
  107. : new CoreFileSystemFileHandle_1.CoreFileSystemFileHandle(this._core, path, ctx);
  108. }
  109. _enqueue(record) {
  110. this._records.push(record);
  111. if (this._flushScheduled)
  112. return;
  113. this._flushScheduled = true;
  114. queueMicrotask(() => {
  115. this._flushScheduled = false;
  116. const records = this._records;
  117. this._records = [];
  118. if (records.length)
  119. this.callback(records, this);
  120. });
  121. }
  122. }
  123. exports.CoreFileSystemObserver = CoreFileSystemObserver;
  124. //# sourceMappingURL=CoreFileSystemObserver.js.map