import { Atom, AtomId } from './Atom2'; import { AtomIndex } from './AtomIndex'; /** * Defines a possible result from manipulating the weave. */ export type WeaveResult = AtomAddedResult | AtomsAddedResult | AlreadyAddedResult | AtomRemovedResult | CauseNotFoundResult | HashFailedResult | InvalidTimestampResult | AtomConflictResult | InvalidArgumentResult | AtomNotFoundResult | InvalidCardinalityResult | CardinalityViolatedResult | NothingResult; /** * Defines an interface that indicates the given atom was removed from the weave. */ export interface AtomRemovedResult { type: 'atom_removed'; ref: WeaveNode; } /** * Defines an interface that indicates the given atoms were added to the weave. */ export interface AtomAddedResult { type: 'atom_added'; atom: Atom; } /** * Defines an interface that indicates the given atom was added to the weave. */ export interface AtomsAddedResult { type: 'atoms_added'; atoms: Atom[]; } /** * Defines an interface that indicates the given atom has already been added to the weave. */ export interface AlreadyAddedResult { type: 'atom_already_added'; atom: Atom; } /** * Defines an interface that indicates the given atom's cause was missing from the tree. */ export interface CauseNotFoundResult { type: 'cause_not_found'; atom: Atom; /** * Whether the atom was saved in the reorder buffer. * If true, then the atom will potentially be included in a atoms_added result when the cause atom is added to the weave. * If false, then the atom has been discarded. */ savedInReorderBuffer: boolean; } /** * Defines an interface that indicates the given atom's hash does not match its contents. */ export interface HashFailedResult { type: 'hash_failed'; atom: Atom; } /** * Defines an interface that indicates the given atom's timestamp is invalid. * This usually means that the timestamp is before it's cause's timestamp. */ export interface InvalidTimestampResult { type: 'invalid_timestamp'; atom: Atom; } /** * Defines an interface that indicates that the given atom conflicted with an atom that was already in the weave. */ export interface AtomConflictResult { type: 'conflict'; /** * The atom that was kept/added to the weave. */ winner: Atom; /** * The atom that removed/excluded from the weave. */ loser: Atom; /** * The weave reference of the looser. * Null if the loser was not in the weave. */ loserRef: WeaveNode; } /** * Defines an interface that indicates that one of the given arguments was invalid. */ export interface InvalidArgumentResult { type: 'invalid_argument'; } /** * Defines an interface that indicates that the given atom was not found. */ export interface AtomNotFoundResult { type: 'atom_not_found'; atom: Atom; } /** * Defines an interface that indicates that the cardinality of the atom was invalid. */ export interface InvalidCardinalityResult { type: 'invalid_cardinality'; /** * The atom that was rejected. */ atom: Atom; } /** * Defines an interface that indicates that the given atom violated the cardinality restraints of another atom that was already in the weave. */ export interface CardinalityViolatedResult { type: 'cardinality_violated'; /** * The atom that was rejected. */ atom: Atom; } /** * Defines an interface that indicates that nothing happened. */ export interface NothingResult { type: 'nothing_happened'; } /** * Defines a node for a doubly linked list of atoms. */ export interface WeaveNode { /** * The atom for the node. */ atom: Atom; /** * The next node. */ next: WeaveNode; /** * The previous node. */ prev: WeaveNode; } /** * Options for a weave. */ export interface WeaveOptions { /** * The maximum number of causeless atoms that can be stored in the reorder buffer. */ maxReorderBufferSize: number; } /** * The default size of the reorder buffer in a weave. */ export declare const DEFAULT_REORDER_BUFFER_SIZE = 10; /** * Defines a weave. * That is, the depth-first preorder traversal of a causal tree. * * Weaves preserve the causality and order of a causal tree. * This means storing the list of atoms that are in the tree and preserving the integrity of the tree. */ export declare class Weave { private _roots; private _cardinality; private _idMap; private _hashMap; /** * A map of atom IDs to nodes that are waiting for a cause. */ private _reorderBuffer; private _maxReorderBufferSize; /** * Gets the root nodes used by this weave. */ get roots(): WeaveNode[]; /** * Iterates all of the atoms in the weave. */ iterateAtoms(): Generator, void, unknown>; /** * Gets the full list of atoms from the weave. */ getAtoms(): Atom[]; /** * Creates a new weave. */ constructor(options?: WeaveOptions); /** * Inserts the given atom into the weave and returns it. * @param atom The atom. */ insert(atom: Atom): WeaveResult; private _addRoot; private _addToReorderBuffer; /** * Finds atoms in the reorder buffer that are children of the given node. * @param atomNode The * @param reorderBuffer Whether the added atoms should still be part of the reorder buffer or not. */ private _addMissingChildren; /** * Gets the node reference to the given atom. * @param atomId The ID of the atom. */ getNode(atomId: AtomId | string): WeaveNode; /** * Gets the node reference to the given atom by hash. * @param hash The hash of the atom to get. */ getNodeByHash(hash: string): WeaveNode; /** * Calculates the chain of references from the root directly to the given reference. * Returns the chain from the given reference to the rootmost reference. * @param weave The weave that the reference is from. * @param ref The reference. */ referenceChain(ref: AtomId): WeaveNode[]; /** * Removes the given atom and all of its children from the weave. * Returns a reference to a linked list that contains all of the removed atoms. * @param atom The atom that should be removed. */ remove(atom: Atom): WeaveResult; /** * Removes the siblings of the given atom which occurred before it. * Returns a reference to a linked list that contains all of the removed atoms. * @param atom The atom whose siblings should be removed. */ removeSiblingsBefore(atom: Atom): WeaveResult; /** * Calculates the index for this weave. */ calculateIndex(): AtomIndex; private _resolveConflict; /** * Inserts the given atom under the given cause atom. * @param cause The cause. * @param atom The atom. */ private _insertUnder; private _insertNodeUnder; private _insertNodeBefore; private _insertNodeAfter; private _insertBefore; private _insertAfter; private _remove; private _removeSpan; /** * Adds the given node to the lookup maps. * @param node The node to add. * @param reorderBuffer Whether to add the node to the reorder buffer instead of the normal buffers. */ private _addNodeToMaps; /** * Removes the given node and all linked nodes from the lookup maps. * @param node The nodes to remove. * @param reorderBuffer Whether to remove them from the reorder buffer instead of the normal buffers. */ private _removeListFromMaps; private _removeNodeIdFromMaps; } /** * Gets the last node in the given node's causal group. * @param start The node to start from. */ export declare function lastInCausalGroup(start: WeaveNode): WeaveNode; /** * Gets the first value from the given iterator. * returns undefined if the iterator contains no values. * @param iterator The iterator. */ export declare function first(iterator: IterableIterator): T; /** * Gets the last value from the given iterator. * Returns undefined if the iterator contains no values. * @param iterator The iterator. */ export declare function last(iterator: IterableIterator): T; /** * Gets the item at the given index in the iterator. * @param iterator The iterator. * @param item The index of the item to get. */ export declare function nth(iterator: IterableIterator, item: number): T; /** * Iterates all of the direct children of the given node. * @param parent The node. */ export declare function iterateChildren(parent: WeaveNode): Generator, void, unknown>; /** * Iterates all of sibling nodes that occur after the given node. * @param start The start node. */ export declare function iterateSiblings(start: WeaveNode): Generator, void, unknown>; /** * Iterates all of the sibling nodes that occur before the given node. * Iterated in reverse order from the oldest to the newest. * @param start The start node. */ export declare function iterateNewerSiblings(start: WeaveNode): Generator, void, unknown>; /** * Iterates all of the nodes in the given node's causal group. * @param start The node to start from. */ export declare function iterateCausalGroup(start: WeaveNode): Generator, void, unknown>; /** * Iterates all of the nodes to the end of the linked list. * @param start The node to start from. */ export declare function iterateFrom(start: WeaveNode): Generator, void, unknown>; /** * Iterates all of the nodes before this node to the beginning of the linked list. * @param start The node to start from. */ export declare function iterateReverse(start: WeaveNode): Generator, void, unknown>; /** * Calculates the atom that was added to the tree from the given result. * Returns null if no atom was added. * @param result The weave result. */ export declare function addedAtom(result: WeaveResult): Atom | Atom[]; /** * Calculates the atom that was added to the tree from the given result. * Returns null if no atom was added. * @param result The weave result. */ export declare function addedWeaveAtoms(result: WeaveResult): Atom[]; /** * Calculates the atoms that were removed from the tree with the given result. * @param result The result. */ export declare function weaveRemovedAtoms(result: WeaveResult): Atom[]; //# sourceMappingURL=Weave2.d.ts.map