|
| 1 | +import AbstractReader from "../AbstractReader.js"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Reader allowing to switch its underlying reader at runtime. |
| 5 | + * If no reader is set, read operations will be halted/paused until a reader is set. |
| 6 | + */ |
| 7 | +export default class Switch extends AbstractReader { |
| 8 | + #reader; |
| 9 | + #pendingCalls = []; |
| 10 | + |
| 11 | + constructor({name, reader}) { |
| 12 | + super(name); |
| 13 | + this.#reader = reader; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Sets the underlying reader and processes any pending read operations. |
| 18 | + * |
| 19 | + * @param {@ui5/fs/AbstractReader} reader The reader to delegate to. |
| 20 | + */ |
| 21 | + setReader(reader) { |
| 22 | + this.#reader = reader; |
| 23 | + this._processPendingCalls(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Unsets the underlying reader. Future calls will be queued. |
| 28 | + */ |
| 29 | + unsetReader() { |
| 30 | + this.#reader = null; |
| 31 | + } |
| 32 | + |
| 33 | + async _byGlob(virPattern, options, trace) { |
| 34 | + if (this.#reader) { |
| 35 | + return this.#reader._byGlob(virPattern, options, trace); |
| 36 | + } |
| 37 | + |
| 38 | + // No reader set, so we queue the call and return a pending promise |
| 39 | + return this._enqueueCall("_byGlob", [virPattern, options, trace]); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + async _byPath(virPath, options, trace) { |
| 44 | + if (this.#reader) { |
| 45 | + return this.#reader._byPath(virPath, options, trace); |
| 46 | + } |
| 47 | + |
| 48 | + // No reader set, so we queue the call and return a pending promise |
| 49 | + return this._enqueueCall("_byPath", [virPath, options, trace]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Queues a method call by returning a promise and storing its resolver. |
| 54 | + * |
| 55 | + * @param {string} methodName The method name to call later. |
| 56 | + * @param {Array} args The arguments to pass to the method. |
| 57 | + * @returns {Promise} A promise that will be resolved/rejected when the call is processed. |
| 58 | + */ |
| 59 | + _enqueueCall(methodName, args) { |
| 60 | + return new Promise((resolve, reject) => { |
| 61 | + this.#pendingCalls.push({methodName, args, resolve, reject}); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Processes all pending calls in the queue using the current reader. |
| 67 | + * |
| 68 | + * @private |
| 69 | + */ |
| 70 | + _processPendingCalls() { |
| 71 | + const callsToProcess = this.#pendingCalls; |
| 72 | + this.#pendingCalls = []; // Clear queue immediately to prevent race conditions |
| 73 | + |
| 74 | + for (const call of callsToProcess) { |
| 75 | + const {methodName, args, resolve, reject} = call; |
| 76 | + // Execute the pending call with the newly set reader |
| 77 | + this.#reader[methodName](...args) |
| 78 | + .then(resolve) |
| 79 | + .catch(reject); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments