Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept awaitable factory #377

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions denops/tataku/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Query = {
name: string;
};

type Factory<T> = (denops: Denops, options: unknown) => T;
type Factory<T> = (denops: Denops, options: unknown) => T | Promise<T>;

function search(
denops: Denops,
Expand Down Expand Up @@ -91,7 +91,7 @@ export function loadProcessor(
.andThen((path) => {
return ResultAsync.fromPromise(
import(path.href).then((e) => e.default),
(cause) => new Error(`Failed to processor-${name}`, { cause }),
(cause) => new Error(`Failed to load processor-${name}`, { cause }),
);
})
.andThen((factory: unknown) => {
Expand All @@ -110,7 +110,7 @@ export function loadEmitter(
.andThen((path) => {
return ResultAsync.fromPromise(
import(path.href).then((e) => e.default),
(cause) => new Error(`Failed to emitter-${name}`, { cause }),
(cause) => new Error(`Failed to load emitter-${name}`, { cause }),
);
})
.andThen((factory: unknown) => {
Expand Down
19 changes: 15 additions & 4 deletions denops/tataku/tataku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validate,
} from "./types.ts";
import { loadCollector, loadEmitter, loadProcessor } from "./load.ts";
import { convertError } from "./utils.ts";

type Streams = {
collector: Collector;
Expand Down Expand Up @@ -42,19 +43,29 @@ export function prepareStreams(
ResultAsync.combine([
loadCollector(denops, recipe.collector.name)
.andThen((factory) =>
okAsync(factory(denops, recipe.collector.options ?? {}))
ResultAsync.fromPromise(
Promise.resolve(factory(denops, recipe.collector.options ?? {})),
convertError("Failed to load collector"),
)
),
ResultAsync.combine(
recipe.processor.map((page) =>
loadProcessor(denops, page.name)
.andThen((factory) =>
okAsync(factory(denops, page.options ?? {}))
ResultAsync.fromPromise(
Promise.resolve(factory(denops, page.options ?? {})),
convertError("Failed to load processor"),
)
)
),
).andThen((streams) => okAsync(new CombinedProcessorStream(streams))),
)
.andThen((streams) => okAsync(new CombinedProcessorStream(streams))),
loadEmitter(denops, recipe.emitter.name)
.andThen((factory) =>
okAsync(factory(denops, recipe.emitter.options ?? {}))
ResultAsync.fromPromise(
Promise.resolve(factory(denops, recipe.emitter.options ?? {})),
convertError("Failed to load emitter"),
)
),
])
)
Expand Down
16 changes: 16 additions & 0 deletions denops/tataku/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ export async function handleError(
);
}
}

/**
* Convert some value to Error
*
* @param cause Some error thing
* @param message The message of error if using cause is not Error
* @returns Converted error
*/
export function convertError(message = "Failed"): (e: unknown) => Error {
return (cause) => {
if (cause instanceof Error) {
return cause;
}
return new Error(message, { cause });
};
}
Loading