inDirectory static method

Future<SimpleOpfsFileSystem> inDirectory(
  1. FileSystemDirectoryHandle root, {
  2. String vfsName = 'simple-opfs',
  3. bool readWriteUnsafe = false,
})

Loads an SimpleOpfsFileSystem in the desired root directory, which must be a Dart wrapper around a FileSystemDirectoryHandle.

When readWriteUnsafe is passed, the synchronous file handles are opened using the proposed lock mode. This mode is currently not supported across browsers, but can be used on Chrome for faster database access across tabs.

Implementation

static Future<SimpleOpfsFileSystem> inDirectory(
  FileSystemDirectoryHandle root, {
  String vfsName = 'simple-opfs',
  bool readWriteUnsafe = false,
}) async {
  Future<FileSystemSyncAccessHandle> open(String name) async {
    final handle = await root.openFile(name, create: true);

    final syncHandlePromise = readWriteUnsafe
        ? ProposedLockingSchemeApi(handle).createSyncAccessHandle(
            FileSystemCreateSyncAccessHandleOptions.unsafeReadWrite(),
          )
        : handle.createSyncAccessHandle();

    return await syncHandlePromise.toDart;
  }

  final meta = await open('meta');
  meta.truncate(2);
  final files = {
    for (final type in FileType.values) type: await open(type.name)
  };

  return SimpleOpfsFileSystem._(meta, files, vfsName: vfsName);
}