-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsource.ts
More file actions
61 lines (56 loc) · 1.84 KB
/
source.ts
File metadata and controls
61 lines (56 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export type * from "@vim-fall/core/source";
import type { Denops } from "@denops/std";
import type { CollectParams, Source } from "@vim-fall/core/source";
import { type DerivableArray, deriveArray } from "@vim-fall/custom/derivable";
import type { Detail, IdItem } from "./item.ts";
/**
* Defines a source responsible for collecting items.
*
* @param collect - A function that collects items asynchronously.
* @returns A source object containing the `collect` function.
*/
export function defineSource<T extends Detail>(
collect: (
denops: Denops,
params: CollectParams,
options: { signal?: AbortSignal },
) => AsyncIterableIterator<IdItem<T>>,
): Source<T> {
return { collect };
}
/**
* Composes multiple sources into a single source.
*
* Each source is collected sequentially in the order it is provided. The
* resulting items are combined into a single asynchronous iterable, with each
* item assigned a unique incremental ID.
*
* @param sources - The sources to compose.
* @returns A single composed source that collects items from all given sources.
*/
export function composeSources<
S extends DerivableArray<[Source<Detail>, ...Source<Detail>[]]>,
R extends UnionSource<S>,
>(...sources: S): Source<R> {
return {
collect: async function* (denops, params, options) {
let id = 0;
for (const source of deriveArray(sources)) {
for await (const item of source.collect(denops, params, options)) {
yield { ...item, id: id++ } as IdItem<R>;
}
}
},
};
}
/**
* Recursively constructs a union type from an array of sources.
*
* @template S - Array of sources to create a union type from.
*/
type UnionSource<
S extends DerivableArray<Source<Detail>[]>,
> = S extends DerivableArray<
[Source<infer T>, ...infer R extends DerivableArray<Source<Detail>[]>]
> ? T | UnionSource<R>
: never;