@interncom/diplomatic 0.8.4 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { Status } from "../shared/consts";
2
2
  import { EntityID, Hash, ICrypto, IOp } from "../shared/types";
3
3
  import { ValStat } from "../shared/valstat";
4
- import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
4
+ import { EntitiesQuery, IEntDB, IEntity, IEntRow } from "./entdb";
5
5
  export type EntChangeListener = (types: Set<string>) => void;
6
6
  export type OpenEntDBOptions = {
7
7
  /**
@@ -48,12 +48,13 @@ export declare class CachedEntDB implements IEntDB {
48
48
  */
49
49
  ingestFromDurable(eids: Iterable<EntityID>): Promise<Status>;
50
50
  /**
51
- * Install durable truth for each eid into mem.
52
- * Always writes mem from durable; notifies types only when the full ent
53
- * identity differs from what mem had (or the row was deleted).
51
+ * Install durable truth for each eid into mem (live or tombstone).
52
+ * Always writes mem from durable; notifies types only when the full row
53
+ * identity differs from what mem had (or the row appeared/vanished).
54
54
  */
55
55
  private pullEids;
56
56
  private warmType;
57
+ getRow<T>(eid: EntityID): Promise<ValStat<IEntRow<T> | undefined>>;
57
58
  getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
58
59
  getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
59
60
  countEntities({ type }: {
@@ -9,6 +9,23 @@ export interface IEntity<T = unknown> extends Omit<IMsgEntBody<T>, "body"> {
9
9
  ctr: number;
10
10
  body: T;
11
11
  }
12
+ /**
13
+ * Deleted eid's LWW frontier: eid + updatedAt + ctr only (no type/body/…).
14
+ * Permanent — pruning would allow obsolete mutates to resurrect the ent.
15
+ * `type?: never` is type-only so IEntity is not assignable to ITombstone.
16
+ */
17
+ export interface ITombstone {
18
+ eid: EntityID;
19
+ updatedAt: Date;
20
+ ctr: number;
21
+ type?: never;
22
+ }
23
+ /** Live ent or tombstone (what the store holds per eid). */
24
+ export type IEntRow<T = unknown> = IEntity<T> | ITombstone;
25
+ /** Live ent (has `type`). */
26
+ export declare function isLiveEnt<T>(row: IEntRow<T>): row is IEntity<T>;
27
+ /** Tombstone frontier (no `type`). */
28
+ export declare function isTombstone(row: IEntRow): row is ITombstone;
12
29
  export interface IDateRange {
13
30
  start: Date;
14
31
  end: Date;
@@ -44,20 +61,28 @@ export type ApplyResult = {
44
61
  export interface IEntDB {
45
62
  apply: (ops: IOp[]) => Promise<ApplyResult>;
46
63
  clear: () => Promise<Status>;
64
+ /**
65
+ * Live ent only. Tombstones and missing eids both yield undefined.
66
+ * Use {@link getRow} when LWW / cache reconcile needs the frontier.
67
+ */
47
68
  getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
69
+ /** Live ent or permanent tombstone. */
70
+ getRow<T>(eid: EntityID): Promise<ValStat<IEntRow<T> | undefined>>;
48
71
  getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
49
72
  countEntities({ type }: {
50
73
  type: string;
51
74
  }): Promise<ValStat<number>>;
52
75
  /**
53
- * Frontier checksum of live rows: eid + updatedAt + ctr per ent
76
+ * Frontier checksum of all rows (live + tombstone): eid + updatedAt + ctr
54
77
  * (see encodeEntRev / checksumEntRevs). Not a content hash of bodies.
55
78
  */
56
79
  checksum(crypto: ICrypto): Promise<ValStat<Hash>>;
57
80
  }
58
- /** Prior rev from a loaded entity (typical update/delete input). */
59
- export declare function revFromEntity(ent: IEntity): IEntRev;
81
+ /** Prior rev from a live ent or tombstone. */
82
+ export declare function revFromEntity(ent: IEntRow): IEntRev;
60
83
  /** Prior rev from a msg head returned by insert/update/delete. */
61
84
  export declare function revFromHead(head: IMessageHead): ValStat<IEntRev>;
62
- export declare function applyOp(curr: IEntity | undefined, op: IOp): ValStat<IEntity | undefined>;
85
+ export declare function applyOp(curr: IEntRow | undefined, op: IOp): ValStat<IEntRow | undefined>;
86
+ /** Types to notify when curr → next after a successful apply. */
87
+ export declare function typesChanged(curr: IEntRow | undefined, next: IEntRow): string[];
63
88
  export declare const nullEntDB: IEntDB;
@@ -1,7 +1,7 @@
1
1
  import { Status } from "../shared/consts";
2
2
  import { EntityID, GroupID, Hash, ICrypto, IOp } from "../shared/types";
3
3
  import { ValStat } from "../shared/valstat.ts";
4
- import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
4
+ import { EntitiesQuery, IEntDB, IEntity, IEntRow } from "./entdb";
5
5
  export declare const entityTableName = "entities";
6
6
  export declare const typeIndexName = "entity_type_created_at";
7
7
  export declare const typeUpdatedAtIndexName = "entity_type_updated_at";
@@ -30,6 +30,7 @@ export declare class EntIDB implements IEntDB {
30
30
  eids: EntityID[];
31
31
  }>;
32
32
  clear(): Promise<Status>;
33
+ getRow<T>(eid: EntityID): Promise<ValStat<IEntRow<T> | undefined>>;
33
34
  getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
34
35
  getAllOfTypeUpdatedBetween<T>(opType: string, start: Date, end: Date): Promise<ValStat<IEntity<T>[]>>;
35
36
  getGroupMembers<T>(opType: string, gid: GroupID): Promise<ValStat<IEntity<T>[]>>;
@@ -1,4 +1,4 @@
1
- import { IEntDB, IEntity } from "./entdb";
1
+ import { IEntDB, IEntity, IEntRow } from "./entdb";
2
2
  import { Status } from "../shared/consts";
3
3
  import { EntityID, GroupID, Hash, ICrypto, IOp } from "../shared/types";
4
4
  import { ValStat } from "../shared/valstat.ts";
@@ -22,7 +22,8 @@ export type EntDBMemoryOptions = {
22
22
  indexes?: boolean;
23
23
  };
24
24
  export declare class EntDBMemory implements IEntDB {
25
- ents: Map<string, IEntity>;
25
+ /** Live ents and permanent tombstones. */
26
+ ents: Map<string, IEntRow>;
26
27
  private readonly useIndex;
27
28
  /** type → eidKey → ent */
28
29
  private byType;
@@ -33,8 +34,8 @@ export declare class EntDBMemory implements IEntDB {
33
34
  /** type → tag → eidKey → ent */
34
35
  private byTypeTag;
35
36
  constructor(initEnts?: IEntity[], opts?: EntDBMemoryOptions);
36
- /** Install or replace an entity; keeps secondary indexes in sync. */
37
- put(ent: IEntity): void;
37
+ /** Install or replace a row; live ents are indexed, tombstones are not. */
38
+ put(row: IEntRow): void;
38
39
  /** Remove by eid key; keeps secondary indexes in sync. */
39
40
  del(key: string): void;
40
41
  private index;
@@ -47,6 +48,7 @@ export declare class EntDBMemory implements IEntDB {
47
48
  eids: EntityID[];
48
49
  }>;
49
50
  clear(): Promise<Status>;
51
+ getRow<T>(eid: EntityID): Promise<ValStat<IEntRow<T> | undefined>>;
50
52
  getEnt<T>(eid: EntityID): Promise<ValStat<IEntity<T> | undefined>>;
51
53
  private getAllEntities;
52
54
  getEntities<T>(query: EntitiesQuery): Promise<ValStat<IEntity<T>[]>>;
package/dist/web/hlc.d.ts CHANGED
@@ -12,9 +12,9 @@ export declare function headUpdatedAtMs(head: IHlcHead): number | undefined;
12
12
  * Higher updatedAt first; tie-break higher ctr (later update at same ms).
13
13
  * Unknown/unparseable eids sort last.
14
14
  *
15
- * EntDB/StateManager already no-ops obsolete msgs (LWW). Sorting newest-first
16
- * is so app state converges to its final view early in a large apply, not to
17
- * save work on the later NoChange applies.
15
+ * EntDB LWW no-ops obsolete msgs against the current row, including permanent
16
+ * tombstones after delete. Newest-first is so app state converges early in a
17
+ * large apply, not to save work on later NoChange applies.
18
18
  */
19
19
  export declare function compareHeadHlcDesc(a: IHlcHead, b: IHlcHead): number;
20
20
  /** Stable copy sorted newest-first by HLC. */
@@ -1,7 +1,7 @@
1
1
  import { SyncClient } from "./client";
2
2
  import crypto from "./crypto";
3
3
  import { CachedEntDB, type CachedEntDBOptions, openEntDB, type OpenEntDBOptions } from "./entdb/cached";
4
- import { EntitiesQuery, entStateManager, IEntDB, IEntity, normalizeTags, nullEntDB, revFromEntity, revFromHead } from "./entdb/entdb";
4
+ import { EntitiesQuery, entStateManager, IEntDB, IEntity, IEntRow, isLiveEnt, isTombstone, ITombstone, normalizeTags, nullEntDB, revFromEntity, revFromHead } from "./entdb/entdb";
5
5
  import { EntIDB } from "./entdb/idb";
6
6
  import { EntDBMemory, type EntDBMemoryOptions } from "./entdb/memory";
7
7
  import { useClient, useClientState, useClientXferState, useSyncOnResume } from "./react/useClient";
@@ -30,5 +30,5 @@ export declare function genWebClient(stateMgr: IStateManager, url: URL): Promise
30
30
  client: SyncClient<URL>;
31
31
  setSeed: (seedHex: string) => Promise<void>;
32
32
  }>;
33
- export { APLD_APPLIED, APLD_ERROR, APLD_PENDING, apldFromStored, b64tob, btob64, btoh, CachedEntDB, checksumEntRevs, checksumSet, Clock, cmpBytes, crypto, Decoder, defaultPeekProgressEvery, eidCodec, encodeEntRev, Encoder, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, hostHTTPTransport, htob, HTTPTransport, IDBStore, idleProgress, IEntDB, IEntity, isApldState, isPendingApply, isTerminalApplyFailure, IStore, MasterSeed, MemoryStore, normalizeTags, nullEntDB, nullStateManager, openDiplomaticClient, openEntDB, openIDBStore, revFromEntity, revFromHead, setApld, shouldEmitItemProgress, SingletonStateManager, StateManager, Status, SyncClient, TypedEventEmitter, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, WorkerClient, };
34
- export type { ApldState, Applier, CachedEntDBOptions, EntDBMemoryOptions, HostHandle, HostStatsUpdate, IClient, ICrypto, IDeleteParams, IDiplomaticClientState, IEntRev, IHostConnectionInfo, IHostRow, IMessage, IMutateOp, IOp, IStateManager, IStoredMessage, IStoredMessageData, IStoredMessageWrite, ITransport, IUpdateParams, ListMsgsOpts, OpenDiplomaticClientMainOptions, OpenDiplomaticClientOptions, OpenDiplomaticClientWorkerOptions, OpenedDiplomaticClient, OpenEntDBOptions, ReconcileOpts, ReconcileReport, SyncProgressEvent, WorkerClientOptions, };
33
+ export { APLD_APPLIED, APLD_ERROR, APLD_PENDING, apldFromStored, b64tob, btob64, btoh, CachedEntDB, checksumEntRevs, checksumSet, Clock, cmpBytes, crypto, Decoder, defaultPeekProgressEvery, eidCodec, encodeEntRev, Encoder, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, hostHTTPTransport, htob, HTTPTransport, IDBStore, idleProgress, IEntDB, IEntity, isApldState, isLiveEnt, isPendingApply, isTerminalApplyFailure, isTombstone, IStore, MasterSeed, MemoryStore, normalizeTags, nullEntDB, nullStateManager, openDiplomaticClient, openEntDB, openIDBStore, revFromEntity, revFromHead, setApld, shouldEmitItemProgress, SingletonStateManager, StateManager, Status, SyncClient, TypedEventEmitter, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, WorkerClient, };
34
+ export type { ApldState, Applier, CachedEntDBOptions, EntDBMemoryOptions, HostHandle, HostStatsUpdate, IClient, ICrypto, IDeleteParams, IDiplomaticClientState, IEntRev, IEntRow, IHostConnectionInfo, IHostRow, IMessage, IMutateOp, IOp, IStateManager, IStoredMessage, IStoredMessageData, IStoredMessageWrite, ITombstone, ITransport, IUpdateParams, ListMsgsOpts, OpenDiplomaticClientMainOptions, OpenDiplomaticClientOptions, OpenDiplomaticClientWorkerOptions, OpenedDiplomaticClient, OpenEntDBOptions, ReconcileOpts, ReconcileReport, SyncProgressEvent, WorkerClientOptions, };