import type { Agile } from '../agile'; import { PatchOptionConfigInterface } from '../state'; import { Item } from './item'; import { SelectorConfigInterface, Selector, SelectorKey } from './selector'; import { Group, GroupAddConfigInterface, GroupConfigInterface, GroupKey } from './group'; import { GroupIngestConfigInterface } from './group/group.observer'; import { CreatePersistentConfigInterface } from '../storages'; import { CollectionPersistent } from './collection.persistent'; export declare class Collection { agileInstance: () => Agile; config: CollectionConfigInterface; private initialConfig; _key?: CollectionKey; size: number; data: { [key: string]: Item; }; isPersisted: boolean; persistent: CollectionPersistent | undefined; groups: { [key: string]: Group; }; selectors: { [key: string]: Selector; }; isInstantiated: boolean; isCollection: boolean; /** * A Collection manages a reactive set of Information * that we need to remember globally at a later point in time. * While providing a toolkit to use and mutate this set of Information. * * It is designed for arrays of data objects following the same pattern. * * Each of these data object must have a unique `primaryKey` to be correctly identified later. * * You can create as many global Collections as you need. * * [Learn more..](https://agile-ts.org/docs/core/collection/) * * @public * @param agileInstance - Instance of Agile the Collection belongs to. * @param config - Configuration object */ constructor(agileInstance: Agile, config?: CreateCollectionConfig); /** * Updates the key/name identifier of the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/properties#key) * * @public * @param value - New key/name identifier. */ set key(value: CollectionKey | undefined); /** * Returns the key/name identifier of the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/properties#key) * * @public */ get key(): CollectionKey | undefined; /** * Updates the key/name identifier of the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#setkey) * * @public * @param value - New key/name identifier. */ setKey(value: CollectionKey | undefined): this; /** * Creates a new Group without associating it to the Collection. * * This way of creating a Group is intended for use in the Collection configuration object, * where the `constructor()` takes care of the binding. * * After a successful initiation of the Collection we recommend using `createGroup()`, * because it automatically connects the Group to the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#group) * * @public * @param initialItems - Key/Name identifiers of the Items to be clustered by the Group. * @param config - Configuration object */ Group(initialItems?: Array, config?: GroupConfigInterface): Group; /** * Creates a new Selector without associating it to the Collection. * * This way of creating a Selector is intended for use in the Collection configuration object, * where the `constructor()` takes care of the binding. * * After a successful initiation of the Collection we recommend using `createSelector()`, * because it automatically connects the Group to the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#selector) * * @public * @param initialKey - Key/Name identifier of the Item to be represented by the Selector. * @param config - Configuration object */ Selector(initialKey: ItemKey | null, config?: SelectorConfigInterface): Selector; /** * Sets up the specified Groups or Group keys * and assigns them to the Collection if they are valid. * * It also instantiates and assigns the default Group to the Collection. * The default Group reflects the default pattern of the Collection. * * @internal * @param groups - Entire Groups or Group keys to be set up. */ initGroups(groups: { [key: string]: Group; } | string[]): void; /** * Sets up the specified Selectors or Selector keys * and assigns them to the Collection if they are valid. * * @internal * @param selectors - Entire Selectors or Selector keys to be set up. */ initSelectors(selectors: { [key: string]: Selector; } | string[]): void; /** * Appends new data objects following the same pattern to the end of the Collection. * * Each collected `data object` requires a unique identifier at the primaryKey property (by default 'id') * to be correctly identified later. * * For example, if we collect some kind of user object, * it must contain such unique identifier at 'id' * to be added to the Collection. * ``` * MY_COLLECTION.collect({id: '1', name: 'jeff'}); // valid * MY_COLLECTION.collect({name: 'frank'}); // invalid * ``` * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#collect) * * @public * @param data - Data objects or entire Items to be added. * @param groupKeys - Group/s to which the specified data objects or Items are to be added. * @param config - Configuration object */ collect(data: DataType | Item | Array>, groupKeys?: GroupKey | Array, config?: CollectConfigInterface): this; /** * Updates the Item `data object` with the specified `object with changes`, if the Item exists. * By default the `object with changes` is merged into the Item `data object` at top level. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#update) * * @public * @param itemKey - Key/Name identifier of the Item to be updated. * @param changes - Object with changes to be merged into the Item data object. * @param config - Configuration object */ update(itemKey: ItemKey, changes: DefaultItem | DataType, config?: UpdateConfigInterface): Item | undefined; /** * Creates a new Group and associates it to the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#createGroup) * * @public * @param groupKey - Unique identifier of the Group to be created. * @param initialItems - Key/Name identifiers of the Items to be clustered by the Group. */ createGroup(groupKey: GroupKey, initialItems?: Array): Group; /** * Returns a boolean indicating whether a Group with the specified `groupKey` * exists in the Collection or not. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#hasgroup) * * @public * @param groupKey - Key/Name identifier of the Group to be checked for existence. * @param config - Configuration object */ hasGroup(groupKey: GroupKey | undefined, config?: HasConfigInterface): boolean; /** * Retrieves a single Group with the specified key/name identifier from the Collection. * * If the to retrieve Group doesn't exist, `undefined` is returned. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getgroup) * * @public * @param groupKey - Key/Name identifier of the Group. * @param config - Configuration object */ getGroup(groupKey: GroupKey | undefined | null, config?: HasConfigInterface): Group | undefined; /** * Retrieves the default Group from the Collection. * * Every Collection should have a default Group, * which represents the default pattern of the Collection. * * If the default Group, for what ever reason, doesn't exist, `undefined` is returned. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getdefaultgroup) * * @public */ getDefaultGroup(): Group | undefined; /** * Retrieves a single Group with the specified key/name identifier from the Collection. * * If the to retrieve Group doesn't exist, a reference Group is returned. * This has the advantage that Components that have the reference Group bound to themselves * are rerenderd when the original Group is created. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getgroupwithreference) * * @public * @param groupKey - Key/Name identifier of the Group. */ getGroupWithReference(groupKey: GroupKey): Group; /** * Removes a Group with the specified key/name identifier from the Collection, * if it exists in the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#removegroup) * * @public * @param groupKey - Key/Name identifier of the Group to be removed. */ removeGroup(groupKey: GroupKey): this; /** * Returns the count of registered Groups in the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getgroupcount) * * @public */ getGroupCount(): number; /** * Creates a new Selector and associates it to the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#createSelector) * * @public * @param selectorKey - Unique identifier of the Selector to be created. * @param itemKey - Key/Name identifier of the Item to be represented by the Selector. */ createSelector(selectorKey: SelectorKey, itemKey: ItemKey | null): Selector; /** * Creates a new Selector and associates it to the Collection. * * The specified `itemKey` is used as the unique identifier key of the new Selector. * ``` * MY_COLLECTION.select('1'); * // is equivalent to * MY_COLLECTION.createSelector('1', '1'); * ``` * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#select) * * @public * @param itemKey - Key/Name identifier of the Item to be represented by the Selector * and used as unique identifier of the Selector. */ select(itemKey: ItemKey): Selector; /** * Returns a boolean indicating whether a Selector with the specified `selectorKey` * exists in the Collection or not. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#hasselector) * * @public * @param selectorKey - Key/Name identifier of the Selector to be checked for existence. * @param config - Configuration object */ hasSelector(selectorKey: SelectorKey | undefined, config?: HasConfigInterface): boolean; /** * Retrieves a single Selector with the specified key/name identifier from the Collection. * * If the to retrieve Selector doesn't exist, `undefined` is returned. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getselector) * * @public * @param selectorKey - Key/Name identifier of the Selector. * @param config - Configuration object */ getSelector(selectorKey: SelectorKey | undefined | null, config?: HasConfigInterface): Selector | undefined; /** * Retrieves a single Selector with the specified key/name identifier from the Collection. * * If the to retrieve Selector doesn't exist, a reference Selector is returned. * This has the advantage that Components that have the reference Selector bound to themselves * are rerenderd when the original Selector is created. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getselectorwithreference) * * @public * @param selectorKey - Key/Name identifier of the Selector. */ getSelectorWithReference(selectorKey: SelectorKey): Selector; /** * Removes a Selector with the specified key/name identifier from the Collection, * if it exists in the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#removeselector) * * @public * @param selectorKey - Key/Name identifier of the Selector to be removed. */ removeSelector(selectorKey: SelectorKey): this; /** * Returns the count of registered Selectors in the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getselectorcount) * * @public */ getSelectorCount(): number; /** * Returns a boolean indicating whether a Item with the specified `itemKey` * exists in the Collection or not. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#hasitem) * * @public * @param itemKey - Key/Name identifier of the Item. * @param config - Configuration object */ hasItem(itemKey: ItemKey | undefined, config?: HasConfigInterface): boolean; /** * Retrieves a single Item with the specified key/name identifier from the Collection. * * If the to retrieve Item doesn't exist, `undefined` is returned. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getitem) * * @public * @param itemKey - Key/Name identifier of the Item. * @param config - Configuration object */ getItem(itemKey: ItemKey | undefined | null, config?: HasConfigInterface): Item | undefined; /** * Retrieves a single Item with the specified key/name identifier from the Collection. * * If the to retrieve Item doesn't exist, a reference Item is returned. * This has the advantage that Components that have the reference Item bound to themselves * are rerenderd when the original Item is created. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getitemwithreference) * * @public * @param itemKey - Key/Name identifier of the Item. */ getItemWithReference(itemKey: ItemKey): Item; /** * Creates a placeholder Item * that can be used to hold a reference to a not existing Item. * * @internal * @param itemKey - Unique identifier of the to create placeholder Item. * @param addToCollection - Whether to add the Item to be created to the Collection. */ createPlaceholderItem(itemKey: ItemKey, addToCollection?: boolean): Item; /** * Retrieves the value (data object) of a single Item * with the specified key/name identifier from the Collection. * * If the to retrieve Item containing the value doesn't exist, `undefined` is returned. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getitemvalue) * * @public * @param itemKey - Key/Name identifier of the Item. * @param config - Configuration object */ getItemValue(itemKey: ItemKey | undefined, config?: HasConfigInterface): DataType | undefined; /** * Retrieves all Items from the Collection. * ``` * MY_COLLECTION.getAllItems(); * // is equivalent to * MY_COLLECTION.getDefaultGroup().items; * ``` * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getallitems) * * @public * @param config - Configuration object */ getAllItems(config?: HasConfigInterface): Array>; /** * Retrieves the values (data objects) of all Items from the Collection. * ``` * MY_COLLECTION.getAllItemValues(); * // is equivalent to * MY_COLLECTION.getDefaultGroup().output; * ``` * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getallitemvalues) * * @public * @param config - Configuration object */ getAllItemValues(config?: HasConfigInterface): Array; /** * Preserves the Collection `value` in the corresponding external Storage. * * The Collection key/name is used as the unique identifier for the Persistent. * If that is not desired or the Collection has no unique identifier, * please specify a separate unique identifier for the Persistent. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#persist) * * @public * @param config - Configuration object */ persist(config?: CreatePersistentConfigInterface): this; /** * Fires immediately after the persisted `value` * is loaded into the Collection from a corresponding external Storage. * * Registering such callback function makes only sense * when the Collection is [persisted](https://agile-ts.org/docs/core/collection/methods/#persist) in an external Storage. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#onload) * * @public * @param callback - A function to be executed after the externally persisted `value` was loaded into the Collection. */ onLoad(callback: (success: boolean) => void): this; /** * Removes all Items from the Collection * and resets all Groups and Selectors of the Collection. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#reset) * * @public */ reset(): this; /** * Puts `itemKeys/s` into Group/s. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#put) * * @public * @param itemKeys - `itemKey/s` to be put into the specified Group/s. * @param groupKeys - Key/Name Identifier/s of the Group/s the specified `itemKey/s` are to put in. * @param config - Configuration object */ put(itemKeys: ItemKey | Array, groupKeys: GroupKey | Array, config?: GroupAddConfigInterface): this; /** * Moves specified `itemKey/s` from one Group to another Group. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#move) * * @public * @param itemKeys - `itemKey/s` to be moved. * @param oldGroupKey - Key/Name Identifier of the Group the `itemKey/s` are moved from. * @param newGroupKey - Key/Name Identifier of the Group the `itemKey/s` are moved in. * @param config - Configuration object */ move(itemKeys: ItemKey | Array, oldGroupKey: GroupKey, newGroupKey: GroupKey, config?: GroupAddConfigInterface): this; /** * Updates the key/name identifier of the Item * and returns a boolean indicating * whether the Item identifier was updated successfully. * * @internal * @param oldItemKey - Old key/name Item identifier. * @param newItemKey - New key/name Item identifier. * @param config - Configuration object */ updateItemKey(oldItemKey: ItemKey, newItemKey: ItemKey, config?: UpdateItemKeyConfigInterface): boolean; /** * Returns all key/name identifiers of the Group/s containing the specified `itemKey`. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#getgroupkeysthathaveitemkey) * * @public * @param itemKey - `itemKey` to be contained in Group/s. */ getGroupKeysThatHaveItemKey(itemKey: ItemKey): Array; /** * Removes Item/s from: * * - `.everywhere()`: * Removes Item/s from the entire Collection and all its Groups and Selectors (i.e. from everywhere) * ``` * MY_COLLECTION.remove('1').everywhere(); * // is equivalent to * MY_COLLECTION.removeItems('1'); * ``` * - `.fromGroups()`: * Removes Item/s only from specified Groups. * ``` * MY_COLLECTION.remove('1').fromGroups(['1', '2']); * // is equivalent to * MY_COLLECTION.removeFromGroups('1', ['1', '2']); * ``` * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#remove) * * @public * @param itemKeys - Item/s with identifier/s to be removed. */ remove(itemKeys: ItemKey | Array): { fromGroups: (groups: Array | ItemKey) => Collection; everywhere: (config?: RemoveItemsConfigInterface) => Collection; }; /** * Remove Item/s from specified Group/s. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#removefromgroups) * * @public * @param itemKeys - Key/Name Identifier/s of the Item/s to be removed from the Group/s. * @param groupKeys - Key/Name Identifier/s of the Group/s the Item/s are to remove from. */ removeFromGroups(itemKeys: ItemKey | Array, groupKeys: GroupKey | Array): this; /** * Removes Item/s from the entire Collection and all the Collection's Groups and Selectors. * * [Learn more..](https://agile-ts.org/docs/core/collection/methods/#removeitems) * * @public * @param itemKeys - Key/Name identifier/s of the Item/s to be removed from the entire Collection. * @param config - Configuration object */ removeItems(itemKeys: ItemKey | Array, config?: RemoveItemsConfigInterface): this; /** * Assigns the provided `data` object to an already existing Item * with specified key/name identifier found in the `data` object. * If the Item doesn't exist yet, a new Item with the `data` object as value * is created and assigned to the Collection. * * Returns a boolean indicating * whether the `data` object was assigned/updated successfully. * * @internal * @param data - Data object * @param config - Configuration object */ assignData(data: DataType, config?: AssignDataConfigInterface): boolean; /** * Assigns the specified Item to the Collection * at the key/name identifier of the Item. * * And returns a boolean indicating * whether the Item was assigned successfully. * * @internal * @param item - Item to be added. * @param config - Configuration object */ assignItem(item: Item, config?: AssignItemConfigInterface): boolean; /** * Rebuilds all Groups that contain the specified `itemKey`. * * @internal * @itemKey - `itemKey` Groups must contain to be rebuilt. * @config - Configuration object */ rebuildGroupsThatIncludeItemKey(itemKey: ItemKey, config?: GroupIngestConfigInterface): void; } export declare type DefaultItem = Record; export declare type CollectionKey = string | number; export declare type ItemKey = string | number; export interface CreateCollectionConfigImpl { /** * Initial Groups of the Collection. * @default [] */ groups?: { [key: string]: Group; } | string[]; /** * Initial Selectors of the Collection * @default [] */ selectors?: { [key: string]: Selector; } | string[]; /** * Key/Name identifier of the Collection. * @default undefined */ key?: CollectionKey; /** * Key/Name of the property * which represents the unique Item identifier * in collected data objects. * @default 'id' */ primaryKey?: string; /** * Key/Name identifier of the default Group that is created shortly after instantiation. * The default Group represents the default pattern of the Collection. * @default 'default' */ defaultGroupKey?: GroupKey; /** * Initial data objects of the Collection. * @default [] */ initialData?: Array; } export declare type CreateCollectionConfig = CreateCollectionConfigImpl | ((collection: Collection) => CreateCollectionConfigImpl); export interface CollectionConfigInterface { /** * Key/Name of the property * which represents the unique Item identifier * in collected data objects. * @default 'id' */ primaryKey: string; /** * Key/Name identifier of the default Group that is created shortly after instantiation. * The default Group represents the default pattern of the Collection. * @default 'default' */ defaultGroupKey: ItemKey; } export interface CollectConfigInterface extends AssignDataConfigInterface { /** * In which way the collected data should be added to the Collection. * - 'push' = at the end * - 'unshift' = at the beginning * https://www.tutorialspoint.com/what-are-the-differences-between-unshift-and-push-methods-in-javascript * @default 'push' */ method?: 'push' | 'unshift'; /** * Performs the specified action for each collected data object. * @default undefined */ forEachItem?: (data: DataType | Item, key: ItemKey, success: boolean, index: number) => void; /** * Whether to create a Selector for each collected data object. * @default false */ select?: boolean; } export interface UpdateConfigInterface { /** * Whether to merge the data object with changes into the existing Item data object * or overwrite the existing Item data object entirely. * @default true */ patch?: boolean | PatchOptionConfigInterface; /** * Whether to update the data object in background. * So that the UI isn't notified of these changes and thus doesn't rerender. * @default false */ background?: boolean; } export interface UpdateItemKeyConfigInterface { /** * Whether to update the Item key/name identifier in background * So that the UI isn't notified of these changes and thus doesn't rerender. * @default false */ background?: boolean; } export interface HasConfigInterface { /** * Whether Items that do not officially exist, * such as placeholder Items, can be found * @default true */ notExisting?: boolean; } export interface RemoveItemsConfigInterface { /** * Whether to remove not officially existing Items (such as placeholder Items). * Keep in mind that sometimes it won't remove an Item entirely * as another Instance (like a Selector) might need to keep reference to it. * https://github.com/agile-ts/agile/pull/152 * @default false */ notExisting?: boolean; /** * Whether to remove Selectors that have selected an Item to be removed. * @default false */ removeSelector?: boolean; } export interface AssignDataConfigInterface { /** * When the Item identifier of the to assign data object already exists in the Collection, * whether to merge the newly assigned data into the existing one * or overwrite the existing one entirely. * @default true */ patch?: boolean; /** * Whether to assign the data object to the Collection in background. * So that the UI isn't notified of these changes and thus doesn't rerender. * @default false */ background?: boolean; } export interface AssignItemConfigInterface { /** * If an Item with the Item identifier already exists, * whether to overwrite it entirely with the new one. * @default false */ overwrite?: boolean; /** * Whether to assign the Item to the Collection in background. * So that the UI isn't notified of these changes and thus doesn't rerender. * @default false */ background?: boolean; /** * Whether to rebuild all Groups that include the itemKey of the to assign Item. * @default true */ rebuildGroups?: boolean; }