Browse Source

refactor: abstract fetchReferenceBy in ensureRepoCollection

to improve ledgability and prepare for stargazing
master
DanConwayDev 2 years ago
parent
commit
24427e1682
No known key found for this signature in database
GPG Key ID: 68E15486D73F75E1
  1. 67
      src/lib/stores/repos.ts

67
src/lib/stores/repos.ts

@ -88,12 +88,46 @@ export const ensureRepoCollection = (
events: [...repo_collection.events, repo_event as RepoEvent], events: [...repo_collection.events, repo_event as RepoEvent],
} }
}) })
fetchReferencedBy(
repo_event,
unique_commit_or_identifier,
collection_for_unique_commit
)
// TODO fetch stargazers
}
})
sub.on('eose', () => {
// still awaiting reference_by at this point
repos[unique_commit_or_identifier].update((repo_collection) => {
return {
...repo_collection,
loading: false,
}
})
})
}
setTimeout(() => {
repos[unique_commit_or_identifier].update((collection) => {
return {
...collection,
events: collection.events.map((e) => ({ ...e, loading: false })),
loading: false,
}
})
}, 5000)
return repos[unique_commit_or_identifier]
}
const fetchReferencedBy = (
repo_event: RepoEvent,
unique_commit_or_identifier: string,
collection_for_unique_commit: boolean
) => {
const relays_to_use = const relays_to_use =
repo_event.relays.length < 3 repo_event.relays.length < 3
? repo_event.relays ? repo_event.relays
: [...base_relays].concat(repo_event.relays) : [...base_relays].concat(repo_event.relays)
// get references
const ref_sub = ndk.subscribe( const ref_sub = ndk.subscribe(
{ {
'#a': [ '#a': [
@ -145,13 +179,8 @@ export const ensureRepoCollection = (
return latest_ref_event return latest_ref_event
}), }),
] ]
const still_loading_events_in_collection = events.some( const still_loading_events_in_collection = events.some((e) => e.loading)
(e) => e.loading if (collection_for_unique_commit && !still_loading_events_in_collection)
)
if (
collection_for_unique_commit &&
!still_loading_events_in_collection
)
addEventsWithMatchingIdentifiers(events) addEventsWithMatchingIdentifiers(events)
return { return {
@ -164,28 +193,6 @@ export const ensureRepoCollection = (
} }
}) })
}) })
}
})
sub.on('eose', () => {
// still awaiting reference_by at this point
repos[unique_commit_or_identifier].update((repo_collection) => {
return {
...repo_collection,
loading: false,
}
})
})
}
setTimeout(() => {
repos[unique_commit_or_identifier].update((collection) => {
return {
...collection,
events: collection.events.map((e) => ({ ...e, loading: false })),
loading: false,
}
})
}, 5000)
return repos[unique_commit_or_identifier]
} }
export const eventToRepoEvent = (event: NDKEvent): RepoEvent | undefined => { export const eventToRepoEvent = (event: NDKEvent): RepoEvent | undefined => {

Loading…
Cancel
Save