@@ -19,7 +19,10 @@ export const collectionsStore = new Store(new Map<string, Collection<any>>())
19
19
20
20
// Map to track loading collections
21
21
22
- const loadingCollections = new Map < string , Promise < void > > ( )
22
+ const loadingCollections = new Map <
23
+ string ,
24
+ Promise < Collection < Record < string , unknown > > >
25
+ > ( )
23
26
24
27
interface PendingSyncedTransaction < T extends object = Record < string , unknown > > {
25
28
committed : boolean
@@ -55,18 +58,20 @@ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
55
58
*/
56
59
export function preloadCollection < T extends object = Record < string , unknown > > (
57
60
config : CollectionConfig < T >
58
- ) : Promise < void > {
61
+ ) : Promise < Collection < T > > {
59
62
// If the collection is already fully loaded, return a resolved promise
60
63
if (
61
64
collectionsStore . state . has ( config . id ) &&
62
65
! loadingCollections . has ( config . id )
63
66
) {
64
- return Promise . resolve ( )
67
+ return Promise . resolve (
68
+ collectionsStore . state . get ( config . id ) ! as Collection < T >
69
+ )
65
70
}
66
71
67
72
// If the collection is in the process of loading, return its promise
68
73
if ( loadingCollections . has ( config . id ) ) {
69
- return loadingCollections . get ( config . id ) !
74
+ return loadingCollections . get ( config . id ) ! as Promise < Collection < T > >
70
75
}
71
76
72
77
// Create a new collection instance if it doesn't exist
@@ -90,8 +95,10 @@ export function preloadCollection<T extends object = Record<string, unknown>>(
90
95
91
96
// Create a promise that will resolve after the first commit
92
97
let resolveFirstCommit : ( ) => void
93
- const firstCommitPromise = new Promise < void > ( ( resolve ) => {
94
- resolveFirstCommit = resolve
98
+ const firstCommitPromise = new Promise < Collection < T > > ( ( resolve ) => {
99
+ resolveFirstCommit = ( ) => {
100
+ resolve ( collection )
101
+ }
95
102
} )
96
103
97
104
// Register a one-time listener for the first commit
@@ -103,7 +110,10 @@ export function preloadCollection<T extends object = Record<string, unknown>>(
103
110
} )
104
111
105
112
// Store the loading promise
106
- loadingCollections . set ( config . id , firstCommitPromise )
113
+ loadingCollections . set (
114
+ config . id ,
115
+ firstCommitPromise as Promise < Collection < Record < string , unknown > > >
116
+ )
107
117
108
118
return firstCommitPromise
109
119
}
0 commit comments