Skip to content

Commit

Permalink
feat: update deps, change adaptor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Aug 25, 2024
1 parent e15a889 commit 605badc
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 72 deletions.
10 changes: 5 additions & 5 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
},
"imports": {
"@denosaurs/plug": "jsr:@denosaurs/plug@^1.0.6",
"@miyauci/fs": "jsr:@miyauci/fs@1.0.0-beta.1",
"@miyauci/infra": "jsr:@miyauci/infra@1.0.0-beta.2",
"@miyauci/file-system-access": "./src/mod.ts",
"@miyauci/file-system-access/deno": "./src/deno/mod.ts",
"@miyauci/fs": "jsr:@miyauci/fs@1.0.0-beta.13",
"@miyauci/infra": "jsr:@miyauci/infra@^1.0.0",
"@std/bytes": "jsr:@std/bytes@^1.0.2",
"@std/media-types": "jsr:@std/media-types@^1.0.2",
"@std/path": "jsr:@std/path@^1.0.1",
"@miyauci/file-system-access": "./src/mod.ts",
"@miyauci/file-system-access/deno": "./src/deno/mod.ts"
"@std/path": "jsr:@std/path@^1.0.1"
},
"exports": {
".": "./src/mod.ts",
Expand Down
36 changes: 27 additions & 9 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions src/deno/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { type FileEntry, UA, type UserAgent } from "@miyauci/fs";
import { FileSystem } from "@miyauci/fs/deno";
import { BucketFileSystem } from "@miyauci/fs/deno";
import { openDirectoryDialog, openFileDialog } from "./ffi.ts";
import type { Adaptor, OpenDirectoryPicker, OpenFileDialog } from "../type.ts";
import { typeByExtension } from "@std/media-types";
import { extname } from "@std/path/extname";

export class DenoAdaptor implements Adaptor {
openFileDialog: OpenFileDialog = openFileDialog;
openDirectoryDialog: OpenDirectoryPicker = openDirectoryDialog;

userAgent: UserAgent = new UA();
typeByEntry(entry: FileEntry): string | undefined {
return typeByExtension(extname(entry.name));
}
locateEntry = FileSystem.prototype.locateEntry;
locateEntry = BucketFileSystem.prototype.locateEntry;
}
8 changes: 4 additions & 4 deletions src/file_system_access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import type {
FileSystemFileHandle,
} from "@miyauci/fs";
import type { Adaptor } from "./type.ts";
import { crateShowOpenFilePicker } from "./show_open_file_picker.ts";
import { createShowOpenFilePicker } from "./show_open_file_picker.ts";
import type { DirectoryPickerOptions, OpenFilePickerOptions } from "./type.ts";
import { createShowDirectoryPicker } from "./show_directory_picker.ts";

export class FileSystemAccess {
constructor(adaptor: Adaptor) {
this.showOpenFilePicker = crateShowOpenFilePicker(
adaptor,
this.showOpenFilePicker = createShowOpenFilePicker(
adaptor.locateEntry,
adaptor.openFileDialog.bind(adaptor),
);

this.showDirectoryPicker = createShowDirectoryPicker(
adaptor,
adaptor.locateEntry,
adaptor.openDirectoryDialog.bind(adaptor),
);
}
Expand Down
35 changes: 19 additions & 16 deletions src/show_directory_picker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {
createFileSystemDirectoryHandle,
createNewFileSystemDirectoryHandle,
type FileSystem,
type FileSystemDirectoryHandle,
type FileSystemFileOrDirectoryHandleContext,
} from "@miyauci/fs";
import { List } from "@miyauci/infra";
import { List, Set } from "@miyauci/infra";
import { isTooSensitiveOrDangerous } from "./algorithm.ts";
import type { DirectoryPickerOptions, OpenDirectoryPicker } from "./type.ts";
import type {
DirectoryPickerOptions,
LocateEntry,
OpenDirectoryPicker,
} from "./type.ts";

export function showDirectoryPickerWith(
context: Pick<
FileSystemFileOrDirectoryHandleContext,
"locateEntry" | "typeByEntry" | "userAgent"
>,
locateEntry: LocateEntry,
openDirectoryPicker: OpenDirectoryPicker,
options?: DirectoryPickerOptions,
): Promise<FileSystemDirectoryHandle> {
Expand Down Expand Up @@ -45,11 +46,16 @@ export function showDirectoryPickerWith(
// 2. At the discretion of the user agent, either go back to the beginning of these in parallel steps, or reject p with an "AbortError" DOMException and abort.
}

// 7. Set result to a new FileSystemDirectoryHandle associated with entry.
const result = createFileSystemDirectoryHandle(
const fileSystem = {
locateEntry,
root,
observations: new Set(),
} satisfies FileSystem;

// 7. Set result to a new FileSystemDirectoryHandle associated with entry.
const result = createNewFileSystemDirectoryHandle(
fileSystem,
new List([""]),
context,
);

// 8. Remember a picked directory given options["id"], entry and environment.
Expand Down Expand Up @@ -85,12 +91,9 @@ export function showDirectoryPickerWith(
}

export function createShowDirectoryPicker(
context: Pick<
FileSystemFileOrDirectoryHandleContext,
"locateEntry" | "typeByEntry" | "userAgent"
>,
locateEntry: LocateEntry,
openDirectoryPicker: OpenDirectoryPicker,
) {
return (options?: DirectoryPickerOptions) =>
showDirectoryPickerWith(context, openDirectoryPicker, options);
showDirectoryPickerWith(locateEntry, openDirectoryPicker, options);
}
46 changes: 23 additions & 23 deletions src/show_open_file_picker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import {
FileSystemFileHandle,
type FileSystemFileOrDirectoryHandleContext,
createNewFileSystemFileHandle,
type FileSystem,
type FileSystemFileHandle,
} from "@miyauci/fs";
import { isTooSensitiveOrDangerous } from "./algorithm.ts";
import type { OpenFileDialog, OpenFilePickerOptions } from "./type.ts";
import { List } from "jsr:@miyauci/infra@1.0.0-beta.2";
import type {
LocateEntry,
OpenFileDialog,
OpenFilePickerOptions,
} from "./type.ts";
import { List, Set } from "@miyauci/infra";

export function showOpenFilePickerWith(
context: Pick<
FileSystemFileOrDirectoryHandleContext,
"locateEntry" | "typeByEntry" | "userAgent"
>,
locateEntry: LocateEntry,
openFileDialog: OpenFileDialog,
options: OpenFilePickerOptions = {},
): Promise<FileSystemFileHandle[]> {
Expand All @@ -35,7 +37,6 @@ export function showOpenFilePickerWith(
// When possible, this prompt should start out showing starting directory.

// 3. Wait for the user to have made their selection.

const entries = openFileDialog(options);

// 4. If the user dismissed the prompt without making a selection, reject p with an "AbortError" DOMException and abort.
Expand All @@ -54,15 +55,17 @@ export function showOpenFilePickerWith(
// 2. At the discretion of the user agent, either go back to the beginning of these in parallel steps, or reject p with an "AbortError" DOMException and abort.
}

// 2. Add a new FileSystemFileHandle associated with entry to result.
result.push(
new FileSystemFileHandle({
locateEntry: context.locateEntry.bind(context),
locator: { kind: "file", path: new List([name]), root },
typeByEntry: context.typeByEntry.bind(context),
userAgent: context.userAgent,
}),
const fileSystem = {
locateEntry,
observations: new Set(),
root,
} satisfies FileSystem;
const handle = createNewFileSystemFileHandle(
fileSystem,
new List([name]),
);
// 2. Add a new FileSystemFileHandle associated with entry to result.
result.push(handle);
}

// 8. Remember a picked directory given options["id"], entries[0] and environment.
Expand All @@ -83,13 +86,10 @@ export function showOpenFilePickerWith(
// environment: unknown,
// ) {}

export function crateShowOpenFilePicker(
context: Pick<
FileSystemFileOrDirectoryHandleContext,
"locateEntry" | "typeByEntry" | "userAgent"
>,
export function createShowOpenFilePicker(
locateEntry: LocateEntry,
open: OpenFileDialog,
) {
return (options?: OpenFilePickerOptions) =>
showOpenFilePickerWith(context, open, options);
showOpenFilePickerWith(locateEntry, open, options);
}
14 changes: 8 additions & 6 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
FileSystemFileOrDirectoryHandleContext,
FileSystemEntry,
FileSystemHandle,
FileSystemPath,
} from "@miyauci/fs";

export interface FilePickerOptions {
Expand Down Expand Up @@ -65,11 +66,12 @@ export interface OpenFileDialog {
(options?: OpenFilePickerOptions): { root: string; name: string }[];
}

export interface Adaptor extends
Pick<
FileSystemFileOrDirectoryHandleContext,
"locateEntry" | "typeByEntry" | "userAgent"
> {
export interface Adaptor {
openFileDialog: OpenFileDialog;
openDirectoryDialog: OpenDirectoryPicker;
locateEntry: LocateEntry;
}

export interface LocateEntry {
(path: FileSystemPath): FileSystemEntry | null;
}

0 comments on commit 605badc

Please sign in to comment.