@verdant-web/store 3.4.0-next.0 → 3.4.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.
Files changed (72) hide show
  1. package/dist/esm/BackoffScheduler.d.ts +19 -0
  2. package/dist/esm/DocumentManager.d.ts +28 -0
  3. package/dist/esm/FakeWeakRef.d.ts +11 -0
  4. package/dist/esm/IDBService.d.ts +30 -0
  5. package/dist/esm/UndoHistory.d.ts +16 -0
  6. package/dist/esm/__tests__/batching.test.d.ts +1 -0
  7. package/dist/esm/__tests__/documents.test.d.ts +1 -0
  8. package/dist/esm/__tests__/fixtures/testStorage.d.ts +76 -0
  9. package/dist/esm/__tests__/legacyOids.test.d.ts +1 -0
  10. package/dist/esm/__tests__/mutations.test.d.ts +1 -0
  11. package/dist/esm/__tests__/queries.test.d.ts +1 -0
  12. package/dist/esm/__tests__/setup/indexedDB.d.ts +1 -0
  13. package/dist/esm/__tests__/undo.test.d.ts +1 -0
  14. package/dist/esm/backup.d.ts +10 -0
  15. package/dist/esm/client/Client.d.ts +98 -0
  16. package/dist/esm/client/ClientDescriptor.d.ts +76 -0
  17. package/dist/esm/client/constants.d.ts +1 -0
  18. package/dist/esm/constants.d.ts +1 -0
  19. package/dist/esm/context.d.ts +38 -0
  20. package/dist/esm/entities/Entity.d.ts +148 -0
  21. package/dist/esm/entities/Entity.test.d.ts +1 -0
  22. package/dist/esm/entities/EntityCache.d.ts +15 -0
  23. package/dist/esm/entities/EntityMetadata.d.ts +68 -0
  24. package/dist/esm/entities/EntityStore.d.ts +81 -0
  25. package/dist/esm/entities/OperationBatcher.d.ts +52 -0
  26. package/dist/esm/entities/types.d.ts +103 -0
  27. package/dist/esm/files/EntityFile.d.ts +35 -0
  28. package/dist/esm/files/FileManager.d.ts +47 -0
  29. package/dist/esm/files/FileStorage.d.ts +39 -0
  30. package/dist/esm/files/utils.d.ts +10 -0
  31. package/dist/esm/files/utils.test.d.ts +1 -0
  32. package/dist/esm/idb.d.ts +13 -0
  33. package/dist/esm/index.d.ts +23 -0
  34. package/dist/esm/metadata/AckInfoStore.d.ts +10 -0
  35. package/dist/esm/metadata/BaselinesStore.d.ts +40 -0
  36. package/dist/esm/metadata/LocalReplicaStore.d.ts +18 -0
  37. package/dist/esm/metadata/MessageCreator.d.ts +19 -0
  38. package/dist/esm/metadata/Metadata.d.ts +135 -0
  39. package/dist/esm/metadata/OperationsStore.d.ts +62 -0
  40. package/dist/esm/metadata/SchemaStore.d.ts +9 -0
  41. package/dist/esm/metadata/openMetadataDatabase.d.ts +19 -0
  42. package/dist/esm/migration/db.d.ts +8 -0
  43. package/dist/esm/migration/errors.d.ts +5 -0
  44. package/dist/esm/migration/openDatabase.d.ts +20 -0
  45. package/dist/esm/migration/paths.d.ts +6 -0
  46. package/dist/esm/migration/paths.test.d.ts +1 -0
  47. package/dist/esm/queries/BaseQuery.d.ts +53 -0
  48. package/dist/esm/queries/CollectionQueries.d.ts +55 -0
  49. package/dist/esm/queries/FindAllQuery.d.ts +12 -0
  50. package/dist/esm/queries/FindInfiniteQuery.d.ts +19 -0
  51. package/dist/esm/queries/FindOneQuery.d.ts +12 -0
  52. package/dist/esm/queries/FindPageQuery.d.ts +24 -0
  53. package/dist/esm/queries/GetQuery.d.ts +10 -0
  54. package/dist/esm/queries/QueryCache.d.ts +17 -0
  55. package/dist/esm/queries/QueryableStorage.d.ts +20 -0
  56. package/dist/esm/queries/dbQueries.d.ts +22 -0
  57. package/dist/esm/queries/keys.d.ts +10 -0
  58. package/dist/esm/queries/ranges.d.ts +2 -0
  59. package/dist/esm/queries/types.d.ts +6 -0
  60. package/dist/esm/queries/utils.d.ts +3 -0
  61. package/dist/esm/sync/FileSync.d.ts +24 -0
  62. package/dist/esm/sync/Heartbeat.d.ts +25 -0
  63. package/dist/esm/sync/PresenceManager.d.ts +55 -0
  64. package/dist/esm/sync/PushPullSync.d.ts +39 -0
  65. package/dist/esm/sync/ServerSyncEndpointProvider.d.ts +34 -0
  66. package/dist/esm/sync/Sync.d.ts +160 -0
  67. package/dist/esm/sync/WebSocketSync.d.ts +44 -0
  68. package/dist/esm/types.d.ts +12 -0
  69. package/dist/esm/utils/Disposable.d.ts +6 -0
  70. package/dist/esm/utils/Resolvable.d.ts +8 -0
  71. package/dist/esm/vanilla.d.ts +1 -0
  72. package/package.json +2 -2
@@ -0,0 +1,19 @@
1
+ import { EventSubscriber } from '@verdant-web/common';
2
+ export declare class BackoffScheduler extends EventSubscriber<{
3
+ trigger: () => void;
4
+ }> {
5
+ private readonly backoff;
6
+ private timer;
7
+ private isScheduled;
8
+ constructor(backoff: Backoff);
9
+ next: () => void;
10
+ reset: () => void;
11
+ }
12
+ export declare class Backoff {
13
+ current: number;
14
+ private readonly max;
15
+ private readonly factor;
16
+ constructor(max: number, factor: number);
17
+ next: () => void;
18
+ reset: () => void;
19
+ }
@@ -0,0 +1,28 @@
1
+ import { StorageSchema } from '@verdant-web/common';
2
+ import { EntityStore } from './entities/EntityStore.js';
3
+ import { Metadata } from './metadata/Metadata.js';
4
+ /**
5
+ * Exposes functionality for creating documents,
6
+ * the only mutation which is available as an entry
7
+ * point in the storage system.
8
+ */
9
+ export declare class DocumentManager<Schema extends StorageSchema<any>> {
10
+ private meta;
11
+ private schema;
12
+ private entities;
13
+ constructor(meta: Metadata, schema: Schema, entities: EntityStore);
14
+ private getOid;
15
+ private addDefaults;
16
+ create: (collection: string, init: any, options?: {
17
+ undoable?: boolean;
18
+ }) => any;
19
+ delete: (collection: string, primaryKey: string, options?: {
20
+ undoable?: boolean;
21
+ }) => Promise<void>;
22
+ deleteAll: (ids: [string, string][], options?: {
23
+ undoable?: boolean;
24
+ }) => Promise<void>;
25
+ deleteAllFromCollection: (collection: string, ids: string[], options?: {
26
+ undoable?: boolean;
27
+ }) => Promise<void>;
28
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * This exists since I'm a little anxious about using WeakRef in production
3
+ * and want to be able to roll it back quickly to debug issues. Basically by adding
4
+ * import { WeakRef } from 'FakeWeakRef' to the top of the file, we can switch back
5
+ * to using a simple object reference.
6
+ */
7
+ export declare class FakeWeakRef<T extends symbol | object> {
8
+ constructor(value: T);
9
+ value: T;
10
+ deref(): T | undefined;
11
+ }
@@ -0,0 +1,30 @@
1
+ import { Context } from './context.js';
2
+ import { Disposable } from './utils/Disposable.js';
3
+ export declare class IDBService extends Disposable {
4
+ protected db: IDBDatabase;
5
+ protected log?: Context['log'];
6
+ private globalAbortController;
7
+ constructor(db: IDBDatabase, { log }?: {
8
+ log?: Context['log'];
9
+ });
10
+ createTransaction: (storeNames: string[], opts?: {
11
+ mode?: 'readonly' | 'readwrite';
12
+ abort?: AbortSignal;
13
+ }) => IDBTransaction;
14
+ run: <T>(storeName: string, getRequest: (store: IDBObjectStore) => IDBRequest<T>, opts?: {
15
+ mode?: 'readonly' | 'readwrite';
16
+ transaction?: IDBTransaction;
17
+ abort?: AbortSignal;
18
+ }) => Promise<T>;
19
+ runAll: <T>(storeName: string, getRequests: (store: IDBObjectStore) => IDBRequest<T>[], opts?: {
20
+ mode: 'readonly' | 'readwrite';
21
+ transaction?: IDBTransaction;
22
+ abort?: AbortSignal;
23
+ }) => Promise<T[]>;
24
+ iterate: <T>(storeName: string, getRequest: (store: IDBObjectStore) => IDBRequest | IDBRequest[], iterator: (value: T, store: IDBObjectStore) => void, opts?: {
25
+ mode?: 'readonly' | 'readwrite';
26
+ transaction?: IDBTransaction;
27
+ abort?: AbortSignal;
28
+ }) => Promise<void>;
29
+ clear: (storeName: string) => Promise<undefined>;
30
+ }
@@ -0,0 +1,16 @@
1
+ import { EventSubscriber } from '@verdant-web/common';
2
+ type Undoable = () => Undoable | Promise<Undoable | null> | null;
3
+ export declare class UndoHistory extends EventSubscriber<{
4
+ change: () => void;
5
+ }> {
6
+ private _undoable;
7
+ private _undone;
8
+ get canUndo(): boolean;
9
+ get canRedo(): boolean;
10
+ undo: () => Promise<boolean>;
11
+ redo: () => Promise<boolean>;
12
+ addUndo: (undoPoint: Undoable) => void;
13
+ addRedo: (redoPoint: Undoable) => void;
14
+ clear: () => void;
15
+ }
16
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ import { ClientWithCollections } from '../../index.js';
2
+ export declare const todoCollection: import("@verdant-web/common").StorageCollectionSchema<{
3
+ id: import("@verdant-web/common").StorageStringFieldSchema;
4
+ content: import("@verdant-web/common").StorageStringFieldSchema;
5
+ done: import("@verdant-web/common").StorageBooleanFieldSchema;
6
+ tags: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
7
+ category: import("@verdant-web/common").StorageStringFieldSchema;
8
+ attachments: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
9
+ name: import("@verdant-web/common").StorageStringFieldSchema;
10
+ test: import("@verdant-web/common").StorageNumberFieldSchema;
11
+ }>>;
12
+ }, {
13
+ content: {
14
+ field: "content";
15
+ };
16
+ example: {
17
+ type: "string";
18
+ compute: (doc: import("@verdant-web/common").ShapeFromFields<{
19
+ id: import("@verdant-web/common").StorageStringFieldSchema;
20
+ content: import("@verdant-web/common").StorageStringFieldSchema;
21
+ done: import("@verdant-web/common").StorageBooleanFieldSchema;
22
+ tags: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
23
+ category: import("@verdant-web/common").StorageStringFieldSchema;
24
+ attachments: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
25
+ name: import("@verdant-web/common").StorageStringFieldSchema;
26
+ test: import("@verdant-web/common").StorageNumberFieldSchema;
27
+ }>>;
28
+ }>) => string;
29
+ };
30
+ }, {
31
+ tagsSortedByDone: {
32
+ of: ("done" | "tags")[];
33
+ };
34
+ categorySortedByDone: {
35
+ of: ("done" | "category")[];
36
+ };
37
+ }>;
38
+ export declare const weirdCollection: import("@verdant-web/common").StorageCollectionSchema<{
39
+ id: import("@verdant-web/common").StorageStringFieldSchema;
40
+ weird: import("@verdant-web/common").StorageAnyFieldSchema<any>;
41
+ map: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
42
+ fileList: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageFileFieldSchema>;
43
+ objectMap: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
44
+ content: import("@verdant-web/common").StorageStringFieldSchema;
45
+ }>>;
46
+ }, import("@verdant-web/common").StorageSyntheticIndices<{
47
+ id: import("@verdant-web/common").StorageStringFieldSchema;
48
+ weird: import("@verdant-web/common").StorageAnyFieldSchema<any>;
49
+ map: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
50
+ fileList: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageFileFieldSchema>;
51
+ objectMap: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
52
+ content: import("@verdant-web/common").StorageStringFieldSchema;
53
+ }>>;
54
+ }>, import("@verdant-web/common").CollectionCompoundIndices<{
55
+ id: import("@verdant-web/common").StorageStringFieldSchema;
56
+ weird: import("@verdant-web/common").StorageAnyFieldSchema<any>;
57
+ map: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
58
+ fileList: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageFileFieldSchema>;
59
+ objectMap: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
60
+ content: import("@verdant-web/common").StorageStringFieldSchema;
61
+ }>>;
62
+ }, import("@verdant-web/common").StorageSyntheticIndices<{
63
+ id: import("@verdant-web/common").StorageStringFieldSchema;
64
+ weird: import("@verdant-web/common").StorageAnyFieldSchema<any>;
65
+ map: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageStringFieldSchema>;
66
+ fileList: import("@verdant-web/common").StorageArrayFieldSchema<import("@verdant-web/common").StorageFileFieldSchema>;
67
+ objectMap: import("@verdant-web/common").StorageMapFieldSchema<import("@verdant-web/common").StorageObjectFieldSchema<{
68
+ content: import("@verdant-web/common").StorageStringFieldSchema;
69
+ }>>;
70
+ }>>>;
71
+ export declare function createTestStorage({ idb, disableRebasing, metadataVersion, log, }?: {
72
+ idb?: IDBFactory;
73
+ disableRebasing?: boolean;
74
+ metadataVersion?: number;
75
+ log?: (...args: any[]) => void;
76
+ }): Promise<ClientWithCollections>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ import 'fake-indexeddb/auto';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Client } from './client/Client.js';
2
+ import { ExportData } from './metadata/Metadata.js';
3
+ import { ReturnedFileData } from './files/FileStorage.js';
4
+ export declare function createClientBackup(client: Client): Promise<Blob>;
5
+ export declare function readBackupFile(file: Blob): Promise<{
6
+ data: ExportData;
7
+ fileData: Omit<ReturnedFileData, "file">[];
8
+ files: File[];
9
+ }>;
10
+ export declare function importClientBackup(client: Client, file: Blob): Promise<void>;
@@ -0,0 +1,98 @@
1
+ import { EventSubscriber, Migration } from '@verdant-web/common';
2
+ import { Context } from '../context.js';
3
+ import { DocumentManager } from '../DocumentManager.js';
4
+ import { FileManagerConfig } from '../files/FileManager.js';
5
+ import { ExportData, Metadata } from '../metadata/Metadata.js';
6
+ import { EntityStore } from '../entities/EntityStore.js';
7
+ import { ServerSyncOptions, Sync } from '../sync/Sync.js';
8
+ import { CollectionQueries } from '../queries/CollectionQueries.js';
9
+ import { ReturnedFileData } from '../files/FileStorage.js';
10
+ interface ClientConfig<Presence = any> {
11
+ syncConfig?: ServerSyncOptions<Presence>;
12
+ migrations: Migration[];
13
+ files?: FileManagerConfig;
14
+ }
15
+ export type ClientWithCollections = Client & {
16
+ [key: string]: CollectionQueries<any, any, any>;
17
+ };
18
+ export declare class Client<Presence = any, Profile = any> extends EventSubscriber<{
19
+ /**
20
+ * Called when a change from a future version of the application has
21
+ * been witnessed. These changes are not applied but it indicates
22
+ * the app has been updated and a peer is using a newer version.
23
+ * You should listen to this event and prompt the user to reload
24
+ * their client, or reload it for them.
25
+ *
26
+ * This event may be called multiple times.
27
+ */
28
+ futureSeen: () => void;
29
+ }> {
30
+ private config;
31
+ private context;
32
+ readonly meta: Metadata;
33
+ private _entities;
34
+ private _queryCache;
35
+ private _documentManager;
36
+ private _fileManager;
37
+ private _closed;
38
+ readonly collectionNames: string[];
39
+ private _sync;
40
+ get sync(): Sync<Presence, Profile>;
41
+ get entities(): EntityStore;
42
+ get documentManager(): DocumentManager<any>;
43
+ constructor(config: ClientConfig, context: Context, components: {
44
+ meta: Metadata;
45
+ });
46
+ private addData;
47
+ get documentDb(): IDBDatabase;
48
+ get metaDb(): IDBDatabase;
49
+ get schema(): import("@verdant-web/common").StorageSchema;
50
+ get namespace(): string;
51
+ get undoHistory(): import("../UndoHistory.js").UndoHistory;
52
+ /**
53
+ * Batch multiple operations together to be executed in a single transaction.
54
+ * The changes made will not be included in the same undo history step as
55
+ * any other changes made outside of the batch. You can also disable undo
56
+ * for your batch to omit changes from undo history.
57
+ *
58
+ * Provide a batch name to apply multiple changes to the same batch
59
+ * across different invocations. Batches will automatically flush after
60
+ * a short delay or if they reach a maximum size.
61
+ */
62
+ get batch(): ({ undoable, batchName, max, timeout, }?: {
63
+ undoable?: boolean | undefined;
64
+ batchName?: string | undefined;
65
+ max?: number | null | undefined;
66
+ timeout?: number | null | undefined;
67
+ }) => import("../entities/OperationBatcher.js").OperationBatch;
68
+ stats: () => Promise<any>;
69
+ close: () => Promise<void>;
70
+ __dangerous__resetLocal: () => Promise<void>;
71
+ export: ({ downloadRemoteFiles }?: {
72
+ downloadRemoteFiles?: boolean | undefined;
73
+ }) => Promise<{
74
+ data: ExportData;
75
+ fileData: Omit<ReturnedFileData, "file">[];
76
+ files: File[];
77
+ }>;
78
+ private getFileExportName;
79
+ private parseFileExportname;
80
+ import: ({ data, fileData, files, }: {
81
+ data: ExportData;
82
+ fileData: Array<Omit<ReturnedFileData, 'file'>>;
83
+ files: File[];
84
+ }) => Promise<void>;
85
+ /**
86
+ * Export all data, then re-import it. This might resolve
87
+ * some issues with the local database, but it should
88
+ * only be done as a second-to-last resort. The last resort
89
+ * would be __dangerous__resetLocal on ClientDescriptor, which
90
+ * clears all local data.
91
+ *
92
+ * Unlike __dangerous__resetLocal, this method allows local-only
93
+ * clients to recover data, whereas __dangerous__resetLocal only
94
+ * lets networked clients recover from the server.
95
+ */
96
+ __dangerous__hardReset: () => Promise<void>;
97
+ }
98
+ export {};
@@ -0,0 +1,76 @@
1
+ import { Migration, StorageSchema } from '@verdant-web/common';
2
+ import { FileManagerConfig } from '../files/FileManager.js';
3
+ import { ServerSyncOptions } from '../sync/Sync.js';
4
+ import { UndoHistory } from '../UndoHistory.js';
5
+ import { Client } from './Client.js';
6
+ import { METADATA_VERSION_KEY } from './constants.js';
7
+ export interface ClientDescriptorOptions<Presence = any, Profile = any> {
8
+ /** The schema used to create this client */
9
+ schema: StorageSchema<any>;
10
+ /** Migrations, in order, to upgrade to each successive version of the schema */
11
+ migrations: Migration<any>[];
12
+ /** Provide a sync config to turn on synchronization with a server */
13
+ sync?: ServerSyncOptions<Profile, Presence>;
14
+ /** Optionally override the IndexedDB implementation */
15
+ indexedDb?: IDBFactory;
16
+ /**
17
+ * Namespaces are used to separate data from different clients in IndexedDB.
18
+ */
19
+ namespace: string;
20
+ /**
21
+ * Provide your own UndoHistory to have a unified undo system across multiple
22
+ * clients if you so desire.
23
+ */
24
+ undoHistory?: UndoHistory;
25
+ /**
26
+ * Provide a log function to log internal debug messages
27
+ */
28
+ log?: (...args: any[]) => void;
29
+ disableRebasing?: boolean;
30
+ /**
31
+ * Provide a specific schema number to override the schema version
32
+ * in the database. This is useful for testing migrations or recovering
33
+ * from a mistakenly deployed incorrect schema. A specific version is required
34
+ * so that you don't leave this on accidentally for all new schemas.
35
+ */
36
+ overrideSchemaConflict?: number;
37
+ /**
38
+ * Configuration for file management
39
+ */
40
+ files?: FileManagerConfig;
41
+ /**
42
+ * Enables experimental WeakRef usage to cull documents
43
+ * from cache that aren't being used. This is a performance
44
+ * optimization which has been tested under all Verdant's test
45
+ * suites but I still want to keep testing it in the real world
46
+ * before turning it on.
47
+ */
48
+ EXPERIMENTAL_weakRefs?: boolean;
49
+ [METADATA_VERSION_KEY]?: number;
50
+ }
51
+ /**
52
+ * Since storage initialization is async, this class wraps the core
53
+ * Storage creation promise and exposes some metadata which can
54
+ * be useful immediately.
55
+ */
56
+ export declare class ClientDescriptor<Presence = any, Profile = any, ClientImpl extends Client = Client> {
57
+ private readonly init;
58
+ private readonly _readyPromise;
59
+ private resolveReady;
60
+ private rejectReady;
61
+ private _resolvedValue;
62
+ private _initializing;
63
+ private _namespace;
64
+ get namespace(): string;
65
+ constructor(init: ClientDescriptorOptions<Presence, Profile>);
66
+ private initialize;
67
+ private initializeDatabases;
68
+ private initializeWIPDatabases;
69
+ private cleanupWIPDatabases;
70
+ get current(): ClientImpl | undefined;
71
+ get readyPromise(): Promise<ClientImpl>;
72
+ get schema(): StorageSchema<any>;
73
+ open: () => Promise<ClientImpl>;
74
+ close: () => Promise<void>;
75
+ __dangerous__resetLocal: () => Promise<void>;
76
+ }
@@ -0,0 +1 @@
1
+ export declare const METADATA_VERSION_KEY: unique symbol;
@@ -0,0 +1 @@
1
+ export declare const TEST_API = "__unsafe__test_api__";
@@ -0,0 +1,38 @@
1
+ import { EventSubscriber, Migration, ObjectIdentifier, StorageSchema } from '@verdant-web/common';
2
+ import { UndoHistory } from './UndoHistory.js';
3
+ /**
4
+ * Common components utilized across various client
5
+ * services.
6
+ */
7
+ export interface Context {
8
+ namespace: string;
9
+ metaDb: IDBDatabase;
10
+ documentDb: IDBDatabase;
11
+ undoHistory: UndoHistory;
12
+ schema: StorageSchema;
13
+ log: (...args: any[]) => void;
14
+ entityEvents: EventSubscriber<{
15
+ collectionsChanged: (names: string[]) => void;
16
+ documentChanged: (oid: ObjectIdentifier) => void;
17
+ }>;
18
+ internalEvents: EventSubscriber<{
19
+ documentDbChanged: (db: IDBDatabase) => void;
20
+ }>;
21
+ globalEvents: EventSubscriber<{
22
+ /**
23
+ * A change from a future version of the application has been
24
+ * witnessed. These changes are not applied but it indicates
25
+ * the app has been updated and we should prompt the user
26
+ * to reload or have their app user manually reload.
27
+ *
28
+ * The parameter is the timestamp of the future change.
29
+ */
30
+ futureSeen: (timestamp: string) => void;
31
+ }>;
32
+ weakRef<T extends object>(value: T): WeakRef<T>;
33
+ migrations: Migration<any>[];
34
+ /**
35
+ * Get the current logical timestamp
36
+ */
37
+ getNow(): string;
38
+ }
@@ -0,0 +1,148 @@
1
+ import { EntityValidationProblem, EventSubscriber, ObjectIdentifier, PatchCreator, StorageFieldSchema } from '@verdant-web/common';
2
+ import { Context } from '../context.js';
3
+ import { FileManager } from '../files/FileManager.js';
4
+ import { EntityCache } from './EntityCache.js';
5
+ import { EntityFamilyMetadata, EntityMetadataView } from './EntityMetadata.js';
6
+ import { BaseEntityValue, DataFromInit, EntityChange, EntityEvents, ListEntity, ListItemInit, ListItemValue, ObjectEntity } from './types.js';
7
+ import { EntityStoreEvents } from './EntityStore.js';
8
+ export interface EntityInit {
9
+ oid: ObjectIdentifier;
10
+ schema: StorageFieldSchema;
11
+ entityFamily?: EntityCache;
12
+ metadataFamily: EntityFamilyMetadata;
13
+ parent?: Entity;
14
+ ctx: Context;
15
+ files: FileManager;
16
+ readonlyKeys?: string[];
17
+ fieldPath?: (string | number)[];
18
+ patchCreator: PatchCreator;
19
+ events: EntityStoreEvents;
20
+ }
21
+ export declare class Entity<Init = any, KeyValue extends BaseEntityValue = any, Snapshot extends any = DataFromInit<Init>> extends EventSubscriber<EntityEvents> implements ObjectEntity<Init, KeyValue, Snapshot>, ListEntity<Init, KeyValue, Snapshot> {
22
+ readonly oid: ObjectIdentifier;
23
+ private readonlyKeys;
24
+ private fieldPath;
25
+ private entityFamily;
26
+ private metadataFamily;
27
+ private schema;
28
+ private parent;
29
+ private ctx;
30
+ private files;
31
+ private patchCreator;
32
+ private events;
33
+ private _viewData;
34
+ private validationError;
35
+ private cachedDeepUpdatedAt;
36
+ private wasDeletedLastChange;
37
+ private cachedView;
38
+ constructor({ oid, schema, entityFamily: childCache, parent, ctx, metadataFamily, readonlyKeys, files, patchCreator, events, }: EntityInit);
39
+ private onAdd;
40
+ private onReplace;
41
+ private onResetAll;
42
+ private get metadata();
43
+ /**
44
+ * The view of this Entity, not including nested
45
+ * entities (that's the snapshot - see #getSnapshot())
46
+ *
47
+ * Nested entities are represented by refs.
48
+ */
49
+ private get viewData();
50
+ /** convenience getter for viewData.view */
51
+ private get rawView();
52
+ /**
53
+ * An Entity's View includes the rendering of its underlying data,
54
+ * connecting of children where refs were, and validation
55
+ * and pruning according to schema.
56
+ */
57
+ private get view();
58
+ private childIsNull;
59
+ get uid(): string;
60
+ get deleted(): boolean;
61
+ get invalid(): boolean;
62
+ get isList(): any;
63
+ get updatedAt(): number;
64
+ get deepUpdatedAt(): number;
65
+ /**
66
+ * @internal - this is relevant to Verdant's system, not users.
67
+ *
68
+ * Indicates whether this document is from an outdated version
69
+ * of the schema - which means it cannot be used until it is upgraded.
70
+ */
71
+ get isOutdatedVersion(): boolean;
72
+ /**
73
+ * Pruning - when entities have invalid children, we 'prune' that
74
+ * data up to the nearest prunable point - a nullable field,
75
+ * or a list.
76
+ */
77
+ protected validate: (...args: unknown[]) => EntityValidationProblem | undefined;
78
+ private viewWithMappedChildren;
79
+ /**
80
+ * A current snapshot of this Entity's data, including nested
81
+ * Entities.
82
+ */
83
+ getSnapshot: () => any;
84
+ private addPendingOperations;
85
+ private addConfirmedData;
86
+ private replaceAllData;
87
+ private resetAllData;
88
+ private change;
89
+ protected deepChange: (target: Entity, ev: EntityChange) => void;
90
+ private getChild;
91
+ /**
92
+ * Gets a value from this Entity. If the value
93
+ * is an object, it will be wrapped in another
94
+ * Entity.
95
+ */
96
+ get: <Key extends keyof KeyValue>(key: Key) => KeyValue[Key];
97
+ private processInputValue;
98
+ private getDeleteMode;
99
+ /**
100
+ * Returns the referent value of an item in the list, used for
101
+ * operations which act on items. if the item is an object,
102
+ * it will attempt to create an OID reference to it. If it
103
+ * is a primitive, it will return the primitive.
104
+ */
105
+ private getItemRefValue;
106
+ set: <Key extends keyof Init>(key: Key, value: Init[Key]) => void;
107
+ /**
108
+ * Returns a destructured version of this Entity, where child
109
+ * Entities are accessible at their respective keys.
110
+ */
111
+ getAll: () => KeyValue;
112
+ delete: (key: any) => void;
113
+ keys: () => string[];
114
+ entries: () => [string, Exclude<KeyValue[keyof KeyValue], undefined>][];
115
+ values: () => Exclude<KeyValue[keyof KeyValue], undefined>[];
116
+ update: (data: any, { merge, replaceSubObjects, }?: {
117
+ replaceSubObjects?: boolean | undefined;
118
+ merge?: boolean | undefined;
119
+ }) => void;
120
+ get length(): number;
121
+ push: (value: ListItemInit<Init>) => void;
122
+ insert: (index: number, value: ListItemInit<Init>) => void;
123
+ move: (from: number, to: number) => void;
124
+ moveItem: (item: ListItemValue<KeyValue>, to: number) => void;
125
+ add: (value: ListItemValue<KeyValue>) => void;
126
+ removeAll: (item: ListItemValue<KeyValue>) => void;
127
+ removeFirst: (item: ListItemValue<KeyValue>) => void;
128
+ removeLast: (item: ListItemValue<KeyValue>) => void;
129
+ [Symbol.iterator](): {
130
+ next: () => {
131
+ readonly value: ListItemValue<KeyValue>;
132
+ readonly done: false;
133
+ } | {
134
+ readonly value: undefined;
135
+ readonly done: true;
136
+ };
137
+ };
138
+ map: <U>(callback: (value: ListItemValue<KeyValue>, index: number) => U) => U[];
139
+ filter: (callback: (value: ListItemValue<KeyValue>, index: number) => boolean) => ListItemValue<KeyValue>[];
140
+ has: (value: ListItemValue<KeyValue>) => boolean;
141
+ forEach: (callback: (value: ListItemValue<KeyValue>, index: number) => void) => void;
142
+ some: (predicate: (value: ListItemValue<KeyValue>) => boolean) => boolean;
143
+ every: (predicate: (value: ListItemValue<KeyValue>) => boolean) => boolean;
144
+ find: (predicate: (value: ListItemValue<KeyValue>) => boolean) => ListItemValue<KeyValue> | undefined;
145
+ includes: (value: ListItemValue<KeyValue>) => boolean;
146
+ __getViewData__: (oid: ObjectIdentifier, type: 'confirmed' | 'pending') => EntityMetadataView;
147
+ __getFamilyOids__: () => string[];
148
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { Entity, EntityInit } from './Entity.js';
2
+ import { EntityFile } from '../files/EntityFile.js';
3
+ import { ObjectIdentifier } from '@verdant-web/common';
4
+ export declare class EntityCache {
5
+ private cache;
6
+ constructor({ initial }?: {
7
+ initial?: Entity[];
8
+ });
9
+ get: (init: EntityInit) => Entity;
10
+ has: (oid: ObjectIdentifier) => boolean;
11
+ getFile: (id: string, options: {
12
+ downloadRemote: boolean;
13
+ }) => EntityFile;
14
+ getCached: (oid: string) => Entity<any, any, any> | EntityFile | undefined;
15
+ }