Skip to content

Commit

Permalink
✨ Add accepting abort controller signal
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 5, 2021
1 parent cca5496 commit 3ae8489
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
4 changes: 3 additions & 1 deletion api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const client = async <T extends Registry>(
) => {
const url = stringify({ name, ...option });

const res = await fetch(url);
const res = await fetch(url, {
signal: option.signal,
});
return await res.json() as RegisterableResult<T>;
};

Expand Down
4 changes: 2 additions & 2 deletions cli/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RegisterableResult, Registry } from "../types/mod.ts";
import { summarize } from "../format/json.ts";
import { flattenDeep, pipe, uniq } from "../deps.ts";

const query2Direct = async <T extends Registry>(
const server = async <T extends Registry>(
queries: any[],
name: string,
): Promise<RegisterableResult<T>> => {
Expand All @@ -16,4 +16,4 @@ const uniqFlatten = pipe(flattenDeep, uniq);
const mapper = <T, U>(map: Record<PropertyKey, T>, val: U[]): T[] =>
val.map((key) => map[key as any]);

export { mapper, query2Direct, uniqFlatten };
export { mapper, server, uniqFlatten };
8 changes: 4 additions & 4 deletions cli/_utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2021-present the Registerable authors. All rights reserved. MIT license.
import { query2Direct, uniqFlatten } from "./_utils.ts";
import { server, uniqFlatten } from "./_utils.ts";
import { assertEquals } from "../dev_deps.ts";
import { QUERY_MAP } from "../constants/query.ts";

Expand All @@ -20,7 +20,7 @@ Deno.test("uniqFlatten", () => {
});
});

Deno.test("query2Direct", async () => {
Deno.test("server", async () => {
const table: [unknown[], string, Record<PropertyKey, unknown>][] = [
[[], "fonction", {
error: {},
Expand Down Expand Up @@ -99,9 +99,9 @@ Deno.test("query2Direct", async () => {

await Promise.all(table.map(async ([queries, name, expected]) => {
assertEquals(
await query2Direct(queries, name),
await server(queries, name),
expected,
`query2Direct(${queries}, ${name}) -> ${expected}`,
`server(${queries}, ${name}) -> ${expected}`,
);
}));
});
2 changes: 1 addition & 1 deletion cli/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Option } from "../types/mod.ts";

type CommandLine = Omit<Option, "mode"> & {
type CommandLine = Omit<Option, "mode" | "signal"> & {
verbose: boolean;
json: boolean;
};
Expand Down
6 changes: 1 addition & 5 deletions cli/deno.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// Copyright 2021-present the Registerable authors. All rights reserved. MIT license.
import yargs from "https://deno.land/x/yargs@v17.0.1-deno/deno.ts";
import type { Option } from "../types/mod.ts";

type CommandLine = Omit<Option, "mode"> & {
verbose: boolean;
json: boolean;
};
import { CommandLine } from "./constants.ts";

const defaultOption: CommandLine = {
verbose: false,
Expand Down
4 changes: 2 additions & 2 deletions cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loggerFactory, logTableFactory } from "../log/log.ts";
import { QUERY_MAP } from "../constants/mod.ts";
import { entries, ifElse, NN } from "../deps.ts";
import { CommandLine, defaultOption } from "./constants.ts";
import { mapper, query2Direct, uniqFlatten } from "./_utils.ts";
import { mapper, server, uniqFlatten } from "./_utils.ts";

const checkNameWithLog = async (
name: string,
Expand All @@ -21,7 +21,7 @@ const checkNameWithLog = async (

const query = uniqFlatten(mapper(QUERY_MAP, registry)).filter((fn) => NN(fn));

const { result, error, hasError, errorRegistry } = await query2Direct(
const { result, error, hasError, errorRegistry } = await server(
query,
name,
);
Expand Down
8 changes: 5 additions & 3 deletions registerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import { QUERY_MAP } from "./constants/mod.ts";
import { ifElse, NN } from "./deps.ts";
import { client } from "./api/client.ts";
import { mapper, query2Direct, uniqFlatten } from "./cli/_utils.ts";
import { mapper, server, uniqFlatten } from "./cli/_utils.ts";
import { Option, Registry } from "./types/mod.ts";
import { RegisterableResult } from "./types/mod.ts";

const defaultOption: Option = {
mode: "server",
registry: ["deno.land", "nest.land", "npm"],
signal: undefined,
};

/**
Expand Down Expand Up @@ -65,14 +66,15 @@ const registerable = async <T extends Registry>(
const {
mode = defaultOption.mode,
registry = defaultOption.registry,
signal = defaultOption.signal,
} = option || defaultOption;

const query = uniqFlatten(mapper(QUERY_MAP, registry)).filter((fn) => NN(fn));

return await ifElse(
mode === "server",
async () => await query2Direct(query, name),
async () => await client(name, { registry }),
async () => await server(query, name),
async () => await client(name, { registry, signal }),
);
};

Expand Down
1 change: 1 addition & 0 deletions types/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface RegisterableResult<T extends Registry = Registry> {
type Option<T extends Registry = Registry> = {
registry: T[];
mode: "server" | "universal";
signal?: RequestInit["signal"];
};

export type { Option, RegisterableResult, Registry };

0 comments on commit 3ae8489

Please sign in to comment.