Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playing around with generics and options #12

Closed
wants to merge 2 commits into from

Conversation

jamesdaniels
Copy link
Collaborator

@jamesdaniels jamesdaniels commented May 14, 2021

In playing around with the ergonomics of the current API and looking at our feature requests, I thought it would be a good pattern to break our second argument into an options object. We have a chance to break since we're going 5.0. Here's a first pass on what it might look like.

collectionChanges(ref) (default all events, includeMetadata true)
collectionChanges(ref, { events: ['added'] })
collectionChanges(ref, { events: ['added'], snapshotListenOptions: { includeMetadata: false } })

Also I've typed the idField option a little better, given our recent generics work.

collectionData(ref) => Observable<DocumentData[]>
collectionData(ref, { idField: 'id' }) => Observable<DocumentData[]>
collectionData<Foo>(ref, { idField: 'id' }) => Observable<Foo[]> with typescript erroring if Foo doesn't contain an id: string field.

I don't think I could force it to be DocumentData & { id: string } (like we do in AngularFire [but some folk don't like]) in the case that they don't explicitly pass a generic, unless we hurt the DevExp with something like collectionData<Foo, 'id'>(ref, { idField: 'id' }), which I'd rather not do. This would also forbid us from being flexible with options and adding something like collectionData<Foo>(ref, { idField: 'id', referenceField: 'ref', metadataField: 'meta' }), etc. which have been requested.

WDYTT? If you're both amenable, I'll draft it up for the other functions too.

@jhuleatt
Copy link
Contributor

Sounds cool! Leaving the API open for more options (maybe an optional custom snapToData function so people could add in whatever metadata they want?) would be nice, and getting some extra type enforcement is always a plus

): Observable<DocumentChange<T>[]> {
return fromCollectionRef(query, { includeMetadataChanges: true }).pipe(
const events = options.events || ALL_EVENTS;
const snapshotListenOptions = options.snapshotListenOptions ?? DEFAULT_SNAPSHOT_LISTEN_OPTIONS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may want to spread these instead, so that if someone specifies some options but not others, it doesn't get confused:

const snapshotListenOptions = {
  ...DEFAULT_SNAPSHOT_LISTEN_OPTIONS,
  ...options.snapshotListenOptions,
};

@jits
Copy link

jits commented Aug 17, 2023

@davideast – any chance of getting something like this in the next (breaking) version, and available in angular/angularfire#3402? This would resolve the "double updates" issue.

I'm happy to contribute a PR, with some guidance.

@davideast
Copy link
Collaborator

@jits If you'd like to create an proposal first and open it as an issue for discussion I would love to review it with you. I'm very much in favor!

@jits
Copy link

jits commented Aug 27, 2023

@davideast – sounds good! Will do, in the next couple of days.

@jits
Copy link

jits commented Aug 28, 2023

@davideast#84 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants