@@ -2,31 +2,171 @@ import { create } from 'zustand';
22
33export interface IDocumentMapStore < T extends { _id : string } > {
44 readonly records : ReadonlyMap < T [ '_id' ] , T > ;
5+ /**
6+ * Checks if a document with the given _id exists in the store.
7+ *
8+ * @param _id - The _id of the document to check.
9+ * @returns true if the document exists, false otherwise.
10+ */
511 has ( _id : T [ '_id' ] ) : boolean ;
12+ /**
13+ * Retrieves a document by its _id.
14+ *
15+ * @param _id - The _id of the document to retrieve.
16+ * @returns The document if found, or undefined if not found.
17+ */
618 get ( _id : T [ '_id' ] ) : T | undefined ;
19+ /**
20+ * Checks if any document in the store satisfies the given predicate.
21+ *
22+ * @param predicate - A function that takes a document and returns true if it matches the condition.
23+ * @returns true if at least one document matches the predicate, false otherwise.
24+ */
725 some ( predicate : ( record : T ) => boolean ) : boolean ;
26+ /**
27+ * Finds a document that satisfies the given predicate.
28+ *
29+ * @param predicate - A function that takes a document and returns true if it matches the condition.
30+ * @returns The first document that matches the predicate, or undefined if no document matches.
31+ */
832 find < U extends T > ( predicate : ( record : T ) => record is U ) : U | undefined ;
33+ /**
34+ * Finds a document that satisfies the given predicate.
35+ *
36+ * @param predicate - A function that takes a document and returns true if it matches the condition.
37+ * @returns The first document that matches the predicate, or undefined if no document matches.
38+ */
939 find ( predicate : ( record : T ) => boolean ) : T | undefined ;
40+ /**
41+ * Finds the first document that satisfies the given predicate, using a comparator to determine the best match.
42+ *
43+ * Usually the "best" document is the first of a ordered set, but it can be any criteria defined by the comparator.
44+ *
45+ * @param predicate - A function that takes a document and returns true if it matches the condition.
46+ * @param comparator - A function that compares two documents and returns a negative number if the first is better, zero if they are equal, or a positive number if the second is better.
47+ * @returns The best matching document according to the predicate and comparator, or undefined if no document matches.
48+ */
1049 findFirst < U extends T > ( predicate : ( record : T ) => record is U , comparator : ( a : T , b : T ) => number ) : U | undefined ;
50+ /**
51+ * Finds the first document that satisfies the given predicate, using a comparator to determine the best match.
52+ *
53+ * Usually the "best" document is the first of a ordered set, but it can be any criteria defined by the comparator.
54+ *
55+ * @param predicate - A function that takes a document and returns true if it matches the condition.
56+ * @param comparator - A function that compares two documents and returns a negative number if the first is better, zero if they are equal, or a positive number if the second is better.
57+ * @returns The best matching document according to the predicate and comparator, or undefined if no document matches.
58+ */
1159 findFirst ( predicate : ( record : T ) => boolean , comparator : ( a : T , b : T ) => number ) : T | undefined ;
60+ /**
61+ * Filters documents in the store based on a predicate.
62+ *
63+ * @param predicate - A function that takes a document and returns true if it matches the condition.
64+ * @returns An array of documents that match the predicate.
65+ */
1266 filter < U extends T > ( predicate : ( record : T ) => record is U ) : U [ ] ;
67+ /**
68+ * Filters documents in the store based on a predicate.
69+ *
70+ * @param predicate - A function that takes a document and returns true if it matches the condition.
71+ * @returns An array of documents that match the predicate.
72+ */
1373 filter ( predicate : ( record : T ) => boolean ) : T [ ] ;
14- indexBy < TKey extends keyof T > ( key : TKey ) : Map < T [ TKey ] , T > ;
74+ /**
75+ * Creates an index of documents by a specified key.
76+ *
77+ * @param key - The key to index the documents by.
78+ * @returns A Map where the keys are the values of the specified key in the documents, and the values are the documents themselves.
79+ */
80+ indexBy < TKey extends keyof T > ( key : TKey ) : ReadonlyMap < T [ TKey ] , T > ;
81+ /**
82+ * Replaces all documents in the store with the provided records.
83+ *
84+ * @param records - An array of documents to replace the current records in the store.
85+ */
1586 replaceAll ( records : T [ ] ) : void ;
87+ /**
88+ * Stores a single document in the store.
89+ *
90+ * @param doc - The document to store.
91+ */
1692 store ( doc : T ) : void ;
93+ /**
94+ * Stores multiple documents in the store.
95+ *
96+ * @param docs - An iterable of documents to store.
97+ */
1798 storeMany ( docs : Iterable < T > ) : void ;
99+ /**
100+ * Deletes a document from the store by its _id.
101+ *
102+ * @param _id - The _id of the document to delete.
103+ */
18104 delete ( _id : T [ '_id' ] ) : void ;
105+ /**
106+ * Updates documents in the store that match a predicate.
107+ *
108+ * @param predicate - A function that takes a document and returns true if it matches the condition.
109+ * @param modifier - A function that takes a document and returns the modified document.
110+ * @returns void
111+ */
19112 update < U extends T > ( predicate : ( record : T ) => record is U , modifier : ( record : U ) => U ) : void ;
113+ /**
114+ * Updates documents in the store that match a predicate.
115+ *
116+ * @param predicate - A function that takes a document and returns true if it matches the condition.
117+ * @param modifier - A function that takes a document and returns the modified document.
118+ * @returns void
119+ */
20120 update ( predicate : ( record : T ) => boolean , modifier : ( record : T ) => T ) : void ;
121+ /**
122+ * Asynchronously updates documents in the store that match a predicate.
123+ *
124+ * @param predicate - A function that takes a document and returns true if it matches the condition.
125+ * @param modifier - A function that takes a document and returns a Promise that resolves to the modified document.
126+ * @returns void
127+ */
21128 updateAsync < U extends T > ( predicate : ( record : T ) => record is U , modifier : ( record : U ) => Promise < U > ) : Promise < void > ;
129+ /**
130+ * Asynchronously updates documents in the store that match a predicate.
131+ *
132+ * @param predicate - A function that takes a document and returns true if it matches the condition.
133+ * @param modifier - A function that takes a document and returns a Promise that resolves to the modified document.
134+ * @returns void
135+ */
22136 updateAsync ( predicate : ( record : T ) => boolean , modifier : ( record : T ) => Promise < T > ) : Promise < void > ;
137+ /**
138+ * Removes documents from the store that match a predicate.
139+ *
140+ * @param predicate - A function that takes a document and returns true if it matches the condition.
141+ */
23142 remove ( predicate : ( record : T ) => boolean ) : void ;
24143}
25144
145+ /**
146+ * Factory function to create a Zustand store that holds a map of documents.
147+ *
148+ * @param options - Optional callbacks to handle invalidation of documents.
149+ * @returns the Zustand store with methods to manage the document map.
150+ */
26151export const createDocumentMapStore = < T extends { _id : string } > ( {
27152 onInvalidate,
28153 onInvalidateAll,
29- } : { onInvalidate ?: ( ...docs : T [ ] ) => void ; onInvalidateAll ?: ( ) => void } = { } ) =>
154+ } : {
155+ /**
156+ * Callback invoked when a document is stored, updated or deleted.
157+ *
158+ * This is useful to recompute Minimongo queries that depend on the changed documents.
159+ * @deprecated prefer subscribing to the store
160+ */
161+ onInvalidate ?: ( ...docs : T [ ] ) => void ;
162+ /**
163+ * Callback invoked when all documents are replaced in the store.
164+ *
165+ * This is useful to recompute Minimongo queries that depend on the changed documents.
166+ * @deprecated prefer subscribing to the store
167+ */
168+ onInvalidateAll ?: ( ) => void ;
169+ } = { } ) =>
30170 create < IDocumentMapStore < T > > ( ) ( ( set , get ) => ( {
31171 records : new Map ( ) ,
32172 has : ( id : T [ '_id' ] ) => get ( ) . records . has ( id ) ,
0 commit comments