Skip to main content

with_file_mutation_queue

Function with_file_mutation_queue 

Source
pub async fn with_file_mutation_queue<T, E, F, Fut>(
    file_path: &str,
    cwd: &Path,
    f: F,
) -> Result<T, E>
where F: FnOnce() -> Fut, Fut: Future<Output = Result<T, E>>,
Expand description

Serialize file mutation operations targeting the same file.

Operations for different files still run in parallel. This mirrors pi’s withFileMutationQueue in file-mutation-queue.ts.

The implementation:

  • Each file has a Notify stored in a global map, representing the “next operation” signal.
  • An operation registers by replacing the entry with its own Notify (for the operation after it), and picking up the previous Notify to wait on.
  • When the operation finishes, it signals its own Notify (which the next operation is waiting on) and, if it is still the latest entry, cleans up.