Skip to content

Commit 6dadba4

Browse files
authored
return collection from preload call (#8)
1 parent 3a12f83 commit 6dadba4

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/collection.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export const collectionsStore = new Store(new Map<string, Collection<any>>())
1919

2020
// Map to track loading collections
2121

22-
const loadingCollections = new Map<string, Promise<void>>()
22+
const loadingCollections = new Map<
23+
string,
24+
Promise<Collection<Record<string, unknown>>>
25+
>()
2326

2427
interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
2528
committed: boolean
@@ -55,18 +58,20 @@ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
5558
*/
5659
export function preloadCollection<T extends object = Record<string, unknown>>(
5760
config: CollectionConfig<T>
58-
): Promise<void> {
61+
): Promise<Collection<T>> {
5962
// If the collection is already fully loaded, return a resolved promise
6063
if (
6164
collectionsStore.state.has(config.id) &&
6265
!loadingCollections.has(config.id)
6366
) {
64-
return Promise.resolve()
67+
return Promise.resolve(
68+
collectionsStore.state.get(config.id)! as Collection<T>
69+
)
6570
}
6671

6772
// If the collection is in the process of loading, return its promise
6873
if (loadingCollections.has(config.id)) {
69-
return loadingCollections.get(config.id)!
74+
return loadingCollections.get(config.id)! as Promise<Collection<T>>
7075
}
7176

7277
// Create a new collection instance if it doesn't exist
@@ -90,8 +95,10 @@ export function preloadCollection<T extends object = Record<string, unknown>>(
9095

9196
// Create a promise that will resolve after the first commit
9297
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+
}
95102
})
96103

97104
// Register a one-time listener for the first commit
@@ -103,7 +110,10 @@ export function preloadCollection<T extends object = Record<string, unknown>>(
103110
})
104111

105112
// 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+
)
107117

108118
return firstCommitPromise
109119
}

0 commit comments

Comments
 (0)