UNPKG

4.35 kBTypeScriptView Raw
1import { DataInterface, PartItem } from "./data-interface";
2import { DataSet } from "./data-set";
3/**
4 * This interface is used to control the pipe.
5 */
6export interface DataPipe {
7 /**
8 * Take all items from the source data set or data view, transform them as
9 * configured and update the target data set.
10 */
11 all(): this;
12 /**
13 * Start observing the source data set or data view, transforming the items
14 * and updating the target data set.
15 *
16 * @remarks
17 * The current content of the source data set will be ignored. If you for
18 * example want to process all the items that are already there use:
19 * `pipe.all().start()`.
20 */
21 start(): this;
22 /**
23 * Stop observing the source data set or data view, transforming the items
24 * and updating the target data set.
25 */
26 stop(): this;
27}
28/**
29 * This interface is used to construct the pipe.
30 */
31export declare type DataPipeFactory = InstanceType<typeof DataPipeUnderConstruction>;
32/**
33 * Create new data pipe.
34 *
35 * @param from - The source data set or data view.
36 * @remarks
37 * Example usage:
38 * ```typescript
39 * interface AppItem {
40 * whoami: string;
41 * appData: unknown;
42 * visData: VisItem;
43 * }
44 * interface VisItem {
45 * id: number;
46 * label: string;
47 * color: string;
48 * x: number;
49 * y: number;
50 * }
51 *
52 * const ds1 = new DataSet<AppItem, "whoami">([], { fieldId: "whoami" });
53 * const ds2 = new DataSet<VisItem, "id">();
54 *
55 * const pipe = createNewDataPipeFrom(ds1)
56 * .filter((item): boolean => item.enabled === true)
57 * .map<VisItem, "id">((item): VisItem => item.visData)
58 * .to(ds2);
59 *
60 * pipe.start();
61 * ```
62 * @returns A factory whose methods can be used to configure the pipe.
63 */
64export declare function createNewDataPipeFrom<SI extends PartItem<SP>, SP extends string = "id">(from: DataInterface<SI, SP>): DataPipeUnderConstruction<SI, SP>;
65/**
66 * Internal implementation of the pipe factory. This should be accessible
67 * only through `createNewDataPipeFrom` from the outside.
68 *
69 * @typeParam TI - Target item type.
70 * @typeParam TP - Target item type's id property name.
71 */
72declare class DataPipeUnderConstruction<SI extends PartItem<SP>, SP extends string = "id"> {
73 private readonly _source;
74 /**
75 * Array transformers used to transform items within the pipe. This is typed
76 * as any for the sake of simplicity.
77 */
78 private readonly _transformers;
79 /**
80 * Create a new data pipe factory. This is an internal constructor that
81 * should never be called from outside of this file.
82 *
83 * @param _source - The source data set or data view for this pipe.
84 */
85 constructor(_source: DataInterface<SI, SP>);
86 /**
87 * Filter the items.
88 *
89 * @param callback - A filtering function that returns true if given item
90 * should be piped and false if not.
91 * @returns This factory for further configuration.
92 */
93 filter(callback: (item: SI) => boolean): DataPipeUnderConstruction<SI, SP>;
94 /**
95 * Map each source item to a new type.
96 *
97 * @param callback - A mapping function that takes a source item and returns
98 * corresponding mapped item.
99 * @typeParam TI - Target item type.
100 * @typeParam TP - Target item type's id property name.
101 * @returns This factory for further configuration.
102 */
103 map<TI extends PartItem<TP>, TP extends string = "id">(callback: (item: SI) => TI): DataPipeUnderConstruction<TI, TP>;
104 /**
105 * Map each source item to zero or more items of a new type.
106 *
107 * @param callback - A mapping function that takes a source item and returns
108 * an array of corresponding mapped items.
109 * @typeParam TI - Target item type.
110 * @typeParam TP - Target item type's id property name.
111 * @returns This factory for further configuration.
112 */
113 flatMap<TI extends PartItem<TP>, TP extends string = "id">(callback: (item: SI) => TI[]): DataPipeUnderConstruction<TI, TP>;
114 /**
115 * Connect this pipe to given data set.
116 *
117 * @param target - The data set that will receive the items from this pipe.
118 * @returns The pipe connected between given data sets and performing
119 * configured transformation on the processed items.
120 */
121 to(target: DataSet<SI, SP>): DataPipe;
122}
123export {};
124//# sourceMappingURL=data-pipe.d.ts.map
\No newline at end of file