|
1 |
| -import 'dart:js_interop'; |
2 |
| -import 'dart:js_interop_unsafe'; |
3 |
| -import 'dart:typed_data'; |
| 1 | +/// Very regrettably, `package:drift` imports this exact file and because it |
| 2 | +/// used to define FileSystem Access API bindings before we've migrated these to |
| 3 | +/// `package:web`. |
| 4 | +/// |
| 5 | +/// To avoid breaking drift, we're exporting the subset of APIs used by drift |
| 6 | +/// to keep that working. This file is not used anywhere in this package and |
| 7 | +/// will be removed in the next major release. |
| 8 | +@Deprecated('Do not import this at all') |
| 9 | +library; |
4 | 10 |
|
5 |
| -import 'package:web/web.dart'; |
| 11 | +import 'dart:js_interop'; |
6 | 12 |
|
| 13 | +import 'package:web/web.dart' as web; |
7 | 14 | import 'core.dart';
|
| 15 | +import 'new_file_system_access.dart' as fixed; |
8 | 16 |
|
9 |
| -@JS('navigator') |
10 |
| -external Navigator get _navigator; |
11 |
| - |
12 |
| -StorageManager? get storageManager { |
13 |
| - final navigator = _navigator; |
| 17 | +typedef FileSystemDirectoryHandle = LegacyDirectoryHandle; |
| 18 | +typedef FileSystemFileHandle = LegacyFileHandle; |
| 19 | +typedef FileSystemSyncAccessHandle = LegacySyncFileHandle; |
14 | 20 |
|
15 |
| - if (navigator.has('storage')) { |
16 |
| - return navigator.storage; |
17 |
| - } |
18 |
| - |
19 |
| - return null; |
| 21 | +LegacyStorageManager? get storageManager { |
| 22 | + final raw = fixed.storageManager; |
| 23 | + return raw != null ? LegacyStorageManager(raw) : null; |
20 | 24 | }
|
21 | 25 |
|
22 |
| -extension StorageManagerApi on StorageManager { |
23 |
| - Future<FileSystemDirectoryHandle> get directory => getDirectory().toDart; |
| 26 | +extension type LegacyStorageManager(web.StorageManager inner) { |
| 27 | + Future<FileSystemDirectoryHandle> get directory async => |
| 28 | + FileSystemDirectoryHandle(await inner.directory); |
24 | 29 | }
|
25 | 30 |
|
26 |
| -extension FileSystemSyncAccessHandleApi on FileSystemSyncAccessHandle { |
27 |
| - int readDart(Uint8List buffer, [FileSystemReadWriteOptions? options]) { |
28 |
| - if (options == null) { |
29 |
| - return read(buffer.toJS); |
30 |
| - } else { |
31 |
| - return read(buffer.toJS, options); |
32 |
| - } |
33 |
| - } |
| 31 | +extension type LegacyHandle(web.FileSystemHandle inner) { |
| 32 | + bool get isDirectory => inner.isDirectory; |
34 | 33 |
|
35 |
| - int writeDart(Uint8List buffer, [FileSystemReadWriteOptions? options]) { |
36 |
| - if (options == null) { |
37 |
| - return write(buffer.toJS); |
38 |
| - } else { |
39 |
| - return write(buffer.toJS, options); |
40 |
| - } |
41 |
| - } |
| 34 | + String get name => inner.name; |
42 | 35 | }
|
43 | 36 |
|
44 |
| -extension FileSystemHandleApi on FileSystemHandle { |
45 |
| - bool get isFile => kind == 'file'; |
46 |
| - |
47 |
| - bool get isDirectory => kind == 'directory'; |
48 |
| -} |
| 37 | +extension type LegacyDirectoryHandle(web.FileSystemDirectoryHandle inner) { |
| 38 | + Future<LegacyFileHandle> openFile(String name, {bool create = false}) async { |
| 39 | + return LegacyFileHandle(await inner.openFile(name, create: create)); |
| 40 | + } |
49 | 41 |
|
50 |
| -extension FileSystemDirectoryHandleApi on FileSystemDirectoryHandle { |
51 |
| - Future<FileSystemFileHandle> openFile(String name, {bool create = false}) { |
52 |
| - return getFileHandle(name, FileSystemGetFileOptions(create: create)).toDart; |
| 42 | + Future<void> removeEntry(String name, {bool recursive = false}) async { |
| 43 | + await fixed.FileSystemDirectoryHandleApi(inner) |
| 44 | + .remove(name, recursive: recursive); |
53 | 45 | }
|
54 | 46 |
|
55 |
| - Future<FileSystemDirectoryHandle> getDirectory(String name, |
56 |
| - {bool create = false}) { |
57 |
| - return getDirectoryHandle( |
58 |
| - name, FileSystemGetDirectoryOptions(create: create)) |
59 |
| - .toDart; |
| 47 | + Future<LegacyDirectoryHandle> getDirectory(String name) async { |
| 48 | + return LegacyDirectoryHandle(await inner.getDirectory(name)); |
60 | 49 | }
|
61 | 50 |
|
62 |
| - Future<void> remove(String name, {bool recursive = false}) { |
63 |
| - return removeEntry(name, FileSystemRemoveOptions(recursive: recursive)) |
64 |
| - .toDart; |
| 51 | + Stream<LegacyHandle> list() { |
| 52 | + return AsyncJavaScriptIteratable<JSArray>(inner) |
| 53 | + .map((data) => LegacyHandle(data.toDart[1] as web.FileSystemHandle)); |
65 | 54 | }
|
| 55 | +} |
66 | 56 |
|
67 |
| - Stream<FileSystemHandle> list() { |
68 |
| - return AsyncJavaScriptIteratable<JSArray>(this) |
69 |
| - .map((data) => data.toDart[1] as FileSystemHandle); |
| 57 | +extension type LegacyFileHandle(web.FileSystemFileHandle inner) { |
| 58 | + Future<LegacySyncFileHandle> createSyncAccessHandle() async { |
| 59 | + final raw = await inner.createSyncAccessHandle().toDart; |
| 60 | + return LegacySyncFileHandle(raw); |
70 | 61 | }
|
| 62 | +} |
71 | 63 |
|
72 |
| - Stream<FileSystemHandle> getFilesRecursively() async* { |
73 |
| - await for (final entry in list()) { |
74 |
| - if (entry.isFile) { |
75 |
| - yield entry; |
76 |
| - } else if (entry.isDirectory) { |
77 |
| - yield* (entry as FileSystemDirectoryHandle).getFilesRecursively(); |
78 |
| - } |
79 |
| - } |
| 64 | +extension type LegacySyncFileHandle(web.FileSystemSyncAccessHandle inner) { |
| 65 | + void close() { |
| 66 | + inner.close(); |
80 | 67 | }
|
81 | 68 | }
|
0 commit comments