Skip to content

Nullable/Try variant for picker methods like ShowDirectoryPickerAsync? #62

@datvm

Description

@datvm

Currently methods like ShowDirectoryPickerAsync return a non-nullable object and throw when the user cancels. It's quite awkward to use:

        FileSystemDirectoryHandleInProcess folder;
        try
        {
            folder = await Fs.ShowDirectoryPickerAsync(new DirectoryPickerOptions()
            {
                Mode = FileSystemPermissionMode.ReadWrite,
                Id = "id",
            });
        }
        catch (JSException ex) when (ex.Message.Contains("AbortError"))
        {
            return; // When cancel
        }
        // Use folder here

I understand it would be breaking change to make it nullable, so we can either add a method to the services directly or write extension methods for them. Suggestion:

static async<T?> TryShowAsync<T>(Func<Task<T>> action); // Common wrapper to be reused for all picker methods.
public async<FileSystemDirectoryHandleInProcess> TryShowDirectoryPickerAsync() => await TryShowAsync(async() => Fs.ShowDirectoryPickerAsync());
// ...

I am happy to make a PR if you agree and choose the preferred way to implement it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions