angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

feat(AngularFire): single injectable service for all APIs

Open jeffbcross opened this issue 10 years ago • 9 comments

In order to simplify getting a hold of AngularFire APIs via DI, a single service could contain all AngularFire APIs (authentication, getting a list, getting a root reference, etc).

With the current API surface, the service could look like:

interface AngularFire {
  auth: AuthObservable; // Pending merge of #33
  list: FirebaseListFactory;
  object: FirebaseObjectFactory; // Not yet implemented
  ref: RefFactory; // Not yet implemented
}
@Component(...)
class QuestionsList { 
  this.orderProp: string = 'askedTime';
  this.pageSize: Observable<number>;
  constructor(private af:AngularFire) {
    this.questions = af.list('/questions', {
      query: {
         orderBy: this.orderProp
         limitToFirst: this.pageSize
      }]
    })
  }
}
@Component({
  template: `
    <h1 *ngIf="af.auth | async">Welcome, {{(af.auth | async).github.userName}}</h1>
  `
})
class Login {
  constructor(private af:AngularFire) {
  }
}

jeffbcross avatar Feb 02 '16 23:02 jeffbcross

BTW the biggest sore spot with the current API is the required usage of @Inject to get dependencies. That, and the current Provider-based design of FirebaseList requires two steps: declaring the provider in a Component's providers list as providers: [FirebaseList('/path')], and then injecting the configured provider via constructor(@Inject('/path').

jeffbcross avatar Feb 02 '16 23:02 jeffbcross

I like the idea of creating a top level API. This will improve the discoverability using TypeScript's code completion.

As authentication goes, I'm most concerned with making sure you can inject the currently authenticated user into the component.

@Component({
  template: `
    <h1>Welcome, {{ user.github.username }}</h1>
  `
})
class Login {
  constructor(private af:AngularFire, private user: FirebaseUser) {
  }
}

With the user injected you can depend on it's existence. That means no if checks, and simplifying your component.

However, that work will likely come with the router integration. Otherwise I'm down with this interface.

davideast avatar Feb 02 '16 23:02 davideast

An additional design goal: have a pipe equivalent of each API, without having to inject the service into the component. I.e. <li *ngFor="#question in '/questions' | afList | async">

jeffbcross avatar Feb 03 '16 00:02 jeffbcross

What is a FirebaseListFactory? An Observable?

benlesh avatar Feb 03 '16 00:02 benlesh

@blesh it returns an observable

jeffbcross avatar Feb 03 '16 00:02 jeffbcross

An observable of an array for the given path.

jeffbcross avatar Feb 03 '16 00:02 jeffbcross

FirstbaseListObservableFactory then? Because FirebaseListFactory just makes me think it's giving me an Array or some sort of static collection.

benlesh avatar Feb 03 '16 00:02 benlesh

How to get FirebaseUser?

joyceview avatar Aug 17 '16 00:08 joyceview

As the current version does Angularfire 2 setup to be a singleton? That way only one instance is initiated? If not should it be?

Thanks.

Sun3 avatar Nov 30 '16 06:11 Sun3