UNPKG

20.1 kBTypeScriptView Raw
1import { Assignable } from "vis-util/esnext";
2import { DataSet } from "./data-set";
3import { DataStream } from "./data-stream";
4declare type ValueOf<T> = T[keyof T];
5/** Valid id type. */
6export declare type Id = number | string;
7/** Nullable id type. */
8export declare type OptId = undefined | null | Id;
9/**
10 * Determine whether a value can be used as an id.
11 *
12 * @param value - Input value of unknown type.
13 * @returns True if the value is valid id, false otherwise.
14 */
15export declare function isId(value: unknown): value is Id;
16/**
17 * Make an object deeply partial.
18 */
19export declare type DeepPartial<T> = T extends any[] | Function | Node ? T : T extends object ? {
20 [key in keyof T]?: DeepPartial<T[key]>;
21} : T;
22/**
23 * An item that may ([[Id]]) or may not (absent, undefined or null) have an id property.
24 *
25 * @typeParam IdProp - Name of the property that contains the id.
26 */
27export declare type PartItem<IdProp extends string> = Partial<Record<IdProp, OptId>>;
28/**
29 * An item that has a property containing an id and all other required properties of given item type.
30 *
31 * @typeParam Item - Item type that may or may not have an id.
32 * @typeParam IdProp - Name of the property that contains the id.
33 */
34export declare type FullItem<Item extends PartItem<IdProp>, IdProp extends string> = Item & Record<IdProp, Id>;
35/**
36 * An item that has a property containing an id and optionally other properties of given item type.
37 *
38 * @typeParam Item - Item type that may or may not have an id.
39 * @typeParam IdProp - Name of the property that contains the id.
40 */
41export declare type UpdateItem<Item extends PartItem<IdProp>, IdProp extends string> = Assignable<FullItem<Item, IdProp>> & Record<IdProp, Id>;
42/**
43 * Test whether an item has an id (is a [[FullItem]]).
44 *
45 * @param item - The item to be tested.
46 * @param idProp - Name of the id property.
47 * @typeParam Item - Item type that may or may not have an id.
48 * @typeParam IdProp - Name of the property that contains the id.
49 * @returns True if this value is a [[FullItem]], false otherwise.
50 */
51export declare function isFullItem<Item extends PartItem<IdProp>, IdProp extends string>(item: Item, idProp: IdProp): item is FullItem<Item, IdProp>;
52/** Add event payload. */
53export interface AddEventPayload {
54 /** Ids of added items. */
55 items: Id[];
56}
57/** Update event payload. */
58export interface UpdateEventPayload<Item, IdProp extends string> {
59 /** Ids of updated items. */
60 items: Id[];
61 /** Items as they were before this update. */
62 oldData: FullItem<Item, IdProp>[];
63 /**
64 * Items as they are now.
65 *
66 * @deprecated Just get the data from the data set or data view.
67 */
68 data: FullItem<Item, IdProp>[];
69}
70/** Remove event payload. */
71export interface RemoveEventPayload<Item, IdProp extends string> {
72 /** Ids of removed items. */
73 items: Id[];
74 /** Items as they were before their removal. */
75 oldData: FullItem<Item, IdProp>[];
76}
77/**
78 * Map of event payload types (event name → payload).
79 *
80 * @typeParam Item - Item type that may or may not have an id.
81 * @typeParam IdProp - Name of the property that contains the id.
82 */
83export interface EventPayloads<Item, IdProp extends string> {
84 add: AddEventPayload;
85 update: UpdateEventPayload<Item, IdProp>;
86 remove: RemoveEventPayload<Item, IdProp>;
87}
88/**
89 * Map of event payload types including any event (event name → payload).
90 *
91 * @typeParam Item - Item type that may or may not have an id.
92 * @typeParam IdProp - Name of the property that contains the id.
93 */
94export interface EventPayloadsWithAny<Item, IdProp extends string> extends EventPayloads<Item, IdProp> {
95 "*": ValueOf<EventPayloads<Item, IdProp>>;
96}
97/**
98 * Map of event callback types (event name → callback).
99 *
100 * @typeParam Item - Item type that may or may not have an id.
101 * @typeParam IdProp - Name of the property that contains the id.
102 */
103export interface EventCallbacks<Item, IdProp extends string> {
104 /**
105 * @param name - The name of the event ([[EventName]]).
106 * @param payload - Data about the items affected by this event.
107 * @param senderId - A senderId, optionally provided by the application code which triggered the event. If senderId is not provided, the argument will be `null`.
108 */
109 add(name: "add", payload: AddEventPayload | null, senderId?: Id | null): void;
110 /**
111 * @param name - The name of the event ([[EventName]]).
112 * @param payload - Data about the items affected by this event.
113 * @param senderId - A senderId, optionally provided by the application code which triggered the event. If senderId is not provided, the argument will be `null`.
114 */
115 update(name: "update", payload: UpdateEventPayload<Item, IdProp> | null, senderId?: Id | null): void;
116 /**
117 * @param name - The name of the event ([[EventName]]).
118 * @param payload - Data about the items affected by this event.
119 * @param senderId - A senderId, optionally provided by the application code which triggered the event. If senderId is not provided, the argument will be `null`.
120 */
121 remove(name: "remove", payload: RemoveEventPayload<Item, IdProp> | null, senderId?: Id | null): void;
122}
123/**
124 * Map of event callback types including any event (event name → callback).
125 *
126 * @typeParam Item - Item type that may or may not have an id.
127 * @typeParam IdProp - Name of the property that contains the id.
128 */
129export interface EventCallbacksWithAny<Item, IdProp extends string> extends EventCallbacks<Item, IdProp> {
130 /**
131 * @param name - The name of the event ([[EventName]]).
132 * @param payload - Data about the items affected by this event.
133 * @param senderId - A senderId, optionally provided by the application code which triggered the event. If senderId is not provided, the argument will be `null`.
134 */
135 "*"<N extends keyof EventCallbacks<Item, IdProp>>(name: N, payload: EventPayloads<Item, IdProp>[N], senderId?: Id | null): void;
136}
137/** Available event names. */
138export declare type EventName = keyof EventPayloads<never, "">;
139/** Available event names and '*' to listen for all. */
140export declare type EventNameWithAny = keyof EventPayloadsWithAny<never, "">;
141/**
142 * Data interface order parameter.
143 * - A string value determines which property will be used for sorting (using < and > operators for numeric comparison).
144 * - A function will be used the same way as in Array.sort.
145 *
146 * @typeParam Item - Item type that may or may not have an id.
147 */
148export declare type DataInterfaceOrder<Item> = keyof Item | ((a: Item, b: Item) => number);
149/**
150 * Data interface get options (return type independent).
151 *
152 * @typeParam Item - Item type that may or may not have an id.
153 */
154export interface DataInterfaceGetOptionsBase<Item> {
155 /**
156 * An array with field names, or an object with current field name and new field name that the field is returned as. By default, all properties of the items are emitted. When fields is defined, only the properties whose name is specified in fields will be included in the returned items.
157 *
158 * @remarks
159 * Warning**: There is no TypeScript support for this.
160 */
161 fields?: string[] | Record<string, string>;
162 /** Items can be filtered on specific properties by providing a filter function. A filter function is executed for each of the items in the DataSet, and is called with the item as parameter. The function must return a boolean. All items for which the filter function returns true will be emitted. */
163 filter?: (item: Item) => boolean;
164 /** Order the items by a field name or custom sort function. */
165 order?: DataInterfaceOrder<Item>;
166}
167/**
168 * Data interface get options (returns a single item or an array).
169 *
170 * @remarks
171 * Whether an item or and array of items is returned is determined by the type of the id(s) argument.
172 * If an array of ids is requested an array of items will be returned.
173 * If a single id is requested a single item (or null if the id doesn't correspond to any item) will be returned.
174 * @typeParam Item - Item type that may or may not have an id.
175 */
176export interface DataInterfaceGetOptionsArray<Item> extends DataInterfaceGetOptionsBase<Item> {
177 /** Items will be returned as a single item (if invoked with an id) or an array of items (if invoked with an array of ids). */
178 returnType?: undefined | "Array";
179}
180/**
181 * Data interface get options (returns an object).
182 *
183 * @remarks
184 * The returned object has ids as keys and items as values of corresponding ids.
185 * @typeParam Item - Item type that may or may not have an id.
186 */
187export interface DataInterfaceGetOptionsObject<Item> extends DataInterfaceGetOptionsBase<Item> {
188 /** Items will be returned as an object map (id → item). */
189 returnType: "Object";
190}
191/**
192 * Data interface get options (returns single item, an array or object).
193 *
194 * @typeParam Item - Item type that may or may not have an id.
195 */
196export declare type DataInterfaceGetOptions<Item> = DataInterfaceGetOptionsArray<Item> | DataInterfaceGetOptionsObject<Item>;
197/**
198 * Data interface get ids options.
199 *
200 * @typeParam Item - Item type that may or may not have an id.
201 */
202export interface DataInterfaceGetIdsOptions<Item> {
203 /** Items can be filtered on specific properties by providing a filter function. A filter function is executed for each of the items in the DataSet, and is called with the item as parameter. The function must return a boolean. All items for which the filter function returns true will be emitted. */
204 filter?: (item: Item) => boolean;
205 /** Order the items by a field name or custom sort function. */
206 order?: DataInterfaceOrder<Item>;
207}
208/**
209 * Data interface for each options.
210 *
211 * @typeParam Item - Item type that may or may not have an id.
212 */
213export interface DataInterfaceForEachOptions<Item> {
214 /** An array with field names, or an object with current field name and new field name that the field is returned as. By default, all properties of the items are emitted. When fields is defined, only the properties whose name is specified in fields will be included in the returned items. */
215 fields?: string[] | Record<string, string>;
216 /** Items can be filtered on specific properties by providing a filter function. A filter function is executed for each of the items in the DataSet, and is called with the item as parameter. The function must return a boolean. All items for which the filter function returns true will be emitted. */
217 filter?: (item: Item) => boolean;
218 /** Order the items by a field name or custom sort function. */
219 order?: DataInterfaceOrder<Item>;
220}
221/**
222 * Data interface map oprions.
223 *
224 * @typeParam Original - The original item type in the data.
225 * @typeParam Mapped - The type after mapping.
226 */
227export interface DataInterfaceMapOptions<Original, Mapped> {
228 /** An array with field names, or an object with current field name and new field name that the field is returned as. By default, all properties of the items are emitted. When fields is defined, only the properties whose name is specified in fields will be included in the returned items. */
229 fields?: string[] | Record<string, string>;
230 /** Items can be filtered on specific properties by providing a filter function. A filter function is executed for each of the items in the DataSet, and is called with the item as parameter. The function must return a boolean. All items for which the filter function returns true will be emitted. */
231 filter?: (item: Original) => boolean;
232 /** Order the items by a field name or custom sort function. */
233 order?: DataInterfaceOrder<Mapped>;
234}
235/**
236 * Common interface for data sets and data view.
237 *
238 * @typeParam Item - Item type that may or may not have an id (missing ids will be generated upon insertion).
239 * @typeParam IdProp - Name of the property on the Item type that contains the id.
240 */
241export interface DataInterface<Item extends PartItem<IdProp>, IdProp extends string = "id"> {
242 /** The number of items. */
243 length: number;
244 /** The key of id property. */
245 idProp: IdProp;
246 /**
247 * Add a universal event listener.
248 *
249 * @remarks The `*` event is triggered when any of the events `add`, `update`, and `remove` occurs.
250 * @param event - Event name.
251 * @param callback - Callback function.
252 */
253 on(event: "*", callback: EventCallbacksWithAny<Item, IdProp>["*"]): void;
254 /**
255 * Add an `add` event listener.
256 *
257 * @remarks The `add` event is triggered when an item or a set of items is added, or when an item is updated while not yet existing.
258 * @param event - Event name.
259 * @param callback - Callback function.
260 */
261 on(event: "add", callback: EventCallbacksWithAny<Item, IdProp>["add"]): void;
262 /**
263 * Add a `remove` event listener.
264 *
265 * @remarks The `remove` event is triggered when an item or a set of items is removed.
266 * @param event - Event name.
267 * @param callback - Callback function.
268 */
269 on(event: "remove", callback: EventCallbacksWithAny<Item, IdProp>["remove"]): void;
270 /**
271 * Add an `update` event listener.
272 *
273 * @remarks The `update` event is triggered when an existing item or a set of existing items is updated.
274 * @param event - Event name.
275 * @param callback - Callback function.
276 */
277 on(event: "update", callback: EventCallbacksWithAny<Item, IdProp>["update"]): void;
278 /**
279 * Remove a universal event listener.
280 *
281 * @param event - Event name.
282 * @param callback - Callback function.
283 */
284 off(event: "*", callback: EventCallbacksWithAny<Item, IdProp>["*"]): void;
285 /**
286 * Remove an `add` event listener.
287 *
288 * @param event - Event name.
289 * @param callback - Callback function.
290 */
291 off(event: "add", callback: EventCallbacksWithAny<Item, IdProp>["add"]): void;
292 /**
293 * Remove a `remove` event listener.
294 *
295 * @param event - Event name.
296 * @param callback - Callback function.
297 */
298 off(event: "remove", callback: EventCallbacksWithAny<Item, IdProp>["remove"]): void;
299 /**
300 * Remove an `update` event listener.
301 *
302 * @param event - Event name.
303 * @param callback - Callback function.
304 */
305 off(event: "update", callback: EventCallbacksWithAny<Item, IdProp>["update"]): void;
306 /**
307 * Get all the items.
308 *
309 * @returns An array containing all the items.
310 */
311 get(): FullItem<Item, IdProp>[];
312 /**
313 * Get all the items.
314 *
315 * @param options - Additional options.
316 * @returns An array containing requested items.
317 */
318 get(options: DataInterfaceGetOptionsArray<Item>): FullItem<Item, IdProp>[];
319 /**
320 * Get all the items.
321 *
322 * @param options - Additional options.
323 * @returns An object map of items (may be an empty object if there are no items).
324 */
325 get(options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
326 /**
327 * Get all the items.
328 *
329 * @param options - Additional options.
330 * @returns An array containing requested items or if requested an object map of items (may be an empty object if there are no items).
331 */
332 get(options: DataInterfaceGetOptions<Item>): FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
333 /**
334 * Get one item.
335 *
336 * @param id - The id of the item.
337 * @returns The item or null if the id doesn't correspond to any item.
338 */
339 get(id: Id): null | FullItem<Item, IdProp>;
340 /**
341 * Get one item.
342 *
343 * @param id - The id of the item.
344 * @param options - Additional options.
345 * @returns The item or null if the id doesn't correspond to any item.
346 */
347 get(id: Id, options: DataInterfaceGetOptionsArray<Item>): null | FullItem<Item, IdProp>;
348 /**
349 * Get one item.
350 *
351 * @param id - The id of the item.
352 * @param options - Additional options.
353 * @returns An object map of items (may be an empty object if no item was found).
354 */
355 get(id: Id, options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
356 /**
357 * Get one item.
358 *
359 * @param id - The id of the item.
360 * @param options - Additional options.
361 * @returns The item if found or null otherwise. If requested an object map with 0 to 1 items.
362 */
363 get(id: Id, options: DataInterfaceGetOptions<Item>): null | FullItem<Item, IdProp> | Record<Id, FullItem<Item, IdProp>>;
364 /**
365 * Get multiple items.
366 *
367 * @param ids - An array of requested ids.
368 * @returns An array of found items (ids that do not correspond to any item are omitted).
369 */
370 get(ids: Id[]): FullItem<Item, IdProp>[];
371 /**
372 * Get multiple items.
373 *
374 * @param ids - An array of requested ids.
375 * @param options - Additional options.
376 * @returns An array of found items (ids that do not correspond to any item are omitted).
377 */
378 get(ids: Id[], options: DataInterfaceGetOptionsArray<Item>): FullItem<Item, IdProp>[];
379 /**
380 * Get multiple items.
381 *
382 * @param ids - An array of requested ids.
383 * @param options - Additional options.
384 * @returns An object map of items (may be an empty object if no item was found).
385 */
386 get(ids: Id[], options: DataInterfaceGetOptionsObject<Item>): Record<Id, FullItem<Item, IdProp>>;
387 /**
388 * Get multiple items.
389 *
390 * @param ids - An array of requested ids.
391 * @param options - Additional options.
392 * @returns An array of found items (ids that do not correspond to any item are omitted).
393 * If requested an object map of items (may be an empty object if no item was found).
394 */
395 get(ids: Id[], options: DataInterfaceGetOptions<Item>): FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
396 /**
397 * Get items.
398 *
399 * @param ids - Id or ids to be returned.
400 * @param options - Options to specify iteration details.
401 * @returns The items (format is determined by ids (single or array) and the options.
402 */
403 get(ids: Id | Id[], options?: DataInterfaceGetOptions<Item>): null | FullItem<Item, IdProp> | FullItem<Item, IdProp>[] | Record<Id, FullItem<Item, IdProp>>;
404 /**
405 * Get the DataSet to which the instance implementing this interface is connected.
406 * In case there is a chain of multiple DataViews, the root DataSet of this chain is returned.
407 *
408 * @returns The data set that actually contains the data.
409 */
410 getDataSet(): DataSet<Item, IdProp>;
411 /**
412 * Get ids of items.
413 *
414 * @remarks
415 * No guarantee is given about the order of returned ids unless an ordering function is supplied.
416 * @param options - Additional configuration.
417 * @returns An array of requested ids.
418 */
419 getIds(options?: DataInterfaceGetIdsOptions<Item>): Id[];
420 /**
421 * Execute a callback function for each item.
422 *
423 * @remarks
424 * No guarantee is given about the order of iteration unless an ordering function is supplied.
425 * @param callback - Executed in similar fashion to Array.forEach callback, but instead of item, index, array receives item, id.
426 * @param options - Options to specify iteration details.
427 */
428 forEach(callback: (item: Item, id: Id) => void, options?: DataInterfaceForEachOptions<Item>): void;
429 /**
430 * Map each item into different item and return them as an array.
431 *
432 * @remarks
433 * No guarantee is given about the order of iteration even if ordering function is supplied (the items are sorted after the mapping).
434 * @param callback - Array.map-like callback, but only with the first two params.
435 * @param options - Options to specify iteration details.
436 * @returns The mapped items.
437 */
438 map<T>(callback: (item: Item, id: Id) => T, options?: DataInterfaceMapOptions<Item, T>): T[];
439 /**
440 * Stream.
441 *
442 * @param ids - Ids of the items to be included in this stream (missing are ignored), all if omitted.
443 * @returns The data stream for this data set.
444 */
445 stream(ids?: Iterable<Id>): DataStream<Item>;
446}
447export {};
448//# sourceMappingURL=data-interface.d.ts.map
\No newline at end of file