|
| 1 | +export * from "./loro_wasm"; |
| 2 | +export type * from "./loro_wasm"; |
| 3 | +import { AwarenessWasm, EphemeralStoreWasm, PeerID, Container, ContainerID, ContainerType, LoroCounter, LoroDoc, LoroList, LoroMap, LoroText, LoroTree, OpId, Value, AwarenessListener, EphemeralListener, EphemeralLocalListener } from "./loro_wasm"; |
| 4 | +/** |
| 5 | + * @deprecated Please use LoroDoc |
| 6 | + */ |
| 7 | +export declare class Loro extends LoroDoc { |
| 8 | +} |
| 9 | +export declare function isContainerId(s: string): s is ContainerID; |
| 10 | +/** Whether the value is a container. |
| 11 | + * |
| 12 | + * # Example |
| 13 | + * |
| 14 | + * ```ts |
| 15 | + * const doc = new LoroDoc(); |
| 16 | + * const map = doc.getMap("map"); |
| 17 | + * const list = doc.getList("list"); |
| 18 | + * const text = doc.getText("text"); |
| 19 | + * isContainer(map); // true |
| 20 | + * isContainer(list); // true |
| 21 | + * isContainer(text); // true |
| 22 | + * isContainer(123); // false |
| 23 | + * isContainer("123"); // false |
| 24 | + * isContainer({}); // false |
| 25 | + * ``` |
| 26 | + */ |
| 27 | +export declare function isContainer(value: any): value is Container; |
| 28 | +/** Get the type of a value that may be a container. |
| 29 | + * |
| 30 | + * # Example |
| 31 | + * |
| 32 | + * ```ts |
| 33 | + * const doc = new LoroDoc(); |
| 34 | + * const map = doc.getMap("map"); |
| 35 | + * const list = doc.getList("list"); |
| 36 | + * const text = doc.getText("text"); |
| 37 | + * getType(map); // "Map" |
| 38 | + * getType(list); // "List" |
| 39 | + * getType(text); // "Text" |
| 40 | + * getType(123); // "Json" |
| 41 | + * getType("123"); // "Json" |
| 42 | + * getType({}); // "Json" |
| 43 | + * ``` |
| 44 | + */ |
| 45 | +export declare function getType<T>(value: T): T extends LoroText ? "Text" : T extends LoroMap<any> ? "Map" : T extends LoroTree<any> ? "Tree" : T extends LoroList<any> ? "List" : T extends LoroCounter ? "Counter" : "Json"; |
| 46 | +export declare function newContainerID(id: OpId, type: ContainerType): ContainerID; |
| 47 | +export declare function newRootContainerID(name: string, type: ContainerType): ContainerID; |
| 48 | +/** |
| 49 | + * @deprecated Please use `EphemeralStore` instead. |
| 50 | + * |
| 51 | + * Awareness is a structure that allows to track the ephemeral state of the peers. |
| 52 | + * |
| 53 | + * If we don't receive a state update from a peer within the timeout, we will remove their state. |
| 54 | + * The timeout is in milliseconds. This can be used to handle the offline state of a peer. |
| 55 | + */ |
| 56 | +export declare class Awareness<T extends Value = Value> { |
| 57 | + inner: AwarenessWasm<T>; |
| 58 | + private peer; |
| 59 | + private timer; |
| 60 | + private timeout; |
| 61 | + private listeners; |
| 62 | + constructor(peer: PeerID, timeout?: number); |
| 63 | + apply(bytes: Uint8Array, origin?: string): void; |
| 64 | + setLocalState(state: T): void; |
| 65 | + getLocalState(): T | undefined; |
| 66 | + getAllStates(): Record<PeerID, T>; |
| 67 | + encode(peers: PeerID[]): Uint8Array; |
| 68 | + encodeAll(): Uint8Array; |
| 69 | + addListener(listener: AwarenessListener): void; |
| 70 | + removeListener(listener: AwarenessListener): void; |
| 71 | + peers(): PeerID[]; |
| 72 | + destroy(): void; |
| 73 | + private startTimerIfNotEmpty; |
| 74 | +} |
| 75 | +/** |
| 76 | + * EphemeralStore tracks ephemeral key-value state across peers. |
| 77 | + * |
| 78 | + * - Use it for lightweight presence/state like cursors, selections, and UI hints. |
| 79 | + * - Conflict resolution is timestamp-based LWW (Last-Write-Wins) per key. |
| 80 | + * - Timeout unit: milliseconds. |
| 81 | + * - After timeout: keys are considered expired. They are omitted from |
| 82 | + * `encode(key)`, `encodeAll()` and `getAllStates()`. A periodic cleanup runs |
| 83 | + * while the store is non-empty and removes expired keys; when removals happen |
| 84 | + * subscribers receive an event with `by: "timeout"` and the `removed` keys. |
| 85 | + * |
| 86 | + * See: https://loro.dev/docs/tutorial/ephemeral |
| 87 | + * |
| 88 | + * @param timeout Inactivity timeout in milliseconds (default: 30000). If a key |
| 89 | + * doesn't receive updates within this duration, it will expire and be removed |
| 90 | + * on the next cleanup tick. |
| 91 | + * |
| 92 | + * @example |
| 93 | + * ```ts |
| 94 | + * const store = new EphemeralStore(); |
| 95 | + * const store2 = new EphemeralStore(); |
| 96 | + * // Subscribe to local updates and forward over the wire |
| 97 | + * store.subscribeLocalUpdates((data) => { |
| 98 | + * store2.apply(data); |
| 99 | + * }); |
| 100 | + * // Subscribe to all updates (including removals by timeout) |
| 101 | + * store2.subscribe((event) => { |
| 102 | + * console.log("event:", event); |
| 103 | + * }); |
| 104 | + * // Set a value |
| 105 | + * store.set("key", "value"); |
| 106 | + * // Encode the value |
| 107 | + * const encoded = store.encode("key"); |
| 108 | + * // Apply the encoded value |
| 109 | + * store2.apply(encoded); |
| 110 | + * ``` |
| 111 | + */ |
| 112 | +export declare class EphemeralStore<T extends Record<string, Value> = Record<string, Value>> { |
| 113 | + inner: EphemeralStoreWasm; |
| 114 | + private timer; |
| 115 | + private timeout; |
| 116 | + constructor(timeout?: number); |
| 117 | + apply(bytes: Uint8Array): void; |
| 118 | + set<K extends keyof T>(key: K, value: T[K]): void; |
| 119 | + delete<K extends keyof T>(key: K): void; |
| 120 | + get<K extends keyof T>(key: K): T[K] | undefined; |
| 121 | + getAllStates(): Partial<T>; |
| 122 | + encode<K extends keyof T>(key: K): Uint8Array; |
| 123 | + encodeAll(): Uint8Array; |
| 124 | + keys(): string[]; |
| 125 | + destroy(): void; |
| 126 | + subscribe(listener: EphemeralListener): () => void; |
| 127 | + subscribeLocalUpdates(listener: EphemeralLocalListener): () => void; |
| 128 | + private startTimerIfNotEmpty; |
| 129 | +} |
| 130 | +export declare function idStrToId(idStr: `${number}@${PeerID}`): OpId; |
0 commit comments