FileSystemSyncAccessHandle: close() 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 close() của giao diện FileSystemSyncAccessHandle đóng một file handle đồng bộ đang mở, vô hiệu hóa mọi thao tác tiếp theo trên nó và giải phóng khóa độc quyền đặt trước đó trên tệp liên kết với file handle.
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
close()
Tham số
Không có.
Giá trị trả về
Không có (undefined).
Ngoại lệ
Không có.
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-close> |