FileSystemSyncAccessHandle: flush() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is only available in Dedicated Web Workers.
Phương thức flush() của giao diện FileSystemSyncAccessHandle lưu trữ mọi thay đổi được thực hiện đối với tệp liên kết với handle qua phương thức write() vào đĩa.
Lưu ý rằng bạn chỉ cần gọi phương thức này nếu cần các thay đổi được cam kết vào đĩa tại một thời điểm cụ thể. Nếu không, bạn có thể để hệ điều hành cơ bản xử lý khi nào thích hợp.
Note:
Trong các phiên bản trước của đặc tả, close(), flush(), getSize(), và truncate() được chỉ định sai là phương thức bất đồng bộ. Tuy nhiên, tất cả các trình duyệt hiện tại hỗ trợ các phương thức này đều triển khai chúng là phương thức đồng bộ.
Cú pháp
flush()
Tham số
Không có.
Giá trị trả về
Không có (undefined).
Ngoại lệ
InvalidStateErrorDOMException-
Được ném ra nếu access handle liên kết đã bị đóng.
Ví dụ
Hàm xử lý sự kiện bất đồng bộ dưới đây nằm trong một Web Worker. Khi nhận thông báo từ luồng chính, nó:
- Tạo một file access handle đồng bộ.
- Lấy kích thước tệp và tạo một
ArrayBufferđể chứa nó. - Đọc nội dung tệp vào buffer.
- Mã hóa thông báo và ghi nó vào cuối tệp.
- Lưu các thay đổi vào đĩa và đóng access handle.
onmessage = async (e) => {
// Retrieve message sent to work from main script
const message = e.data;
// Get handle to draft file
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle("draft.txt", { create: true });
// Get sync access handle
const accessHandle = await draftHandle.createSyncAccessHandle();
// Get size of the file.
const fileSize = accessHandle.getSize();
// Read file content to a buffer.
const buffer = new DataView(new ArrayBuffer(fileSize));
const readBuffer = accessHandle.read(buffer, { at: 0 });
// Write the message to the end of the file.
const encoder = new TextEncoder();
const encodedMessage = encoder.encode(message);
const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer });
// Persist changes to disk.
accessHandle.flush();
// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
};
Thông số kỹ thuật
| Specification |
|---|
| File System> # api-filesystemsyncaccesshandle-flush> |