Skip to content

Instantly share code, notes, and snippets.

@ValentinFunk
Created February 2, 2018 09:24
Show Gist options
  • Select an option

  • Save ValentinFunk/670578d2ea2941a01b545952721f174c to your computer and use it in GitHub Desktop.

Select an option

Save ValentinFunk/670578d2ea2941a01b545952721f174c to your computer and use it in GitHub Desktop.
Firebase Universal Example
@Injectable()
export class UserService {
constructor(private db: AngularFireDatabase, private zone: NgZone) {}
getUser(id: string | number): Observable<User> {
const user$ = this.database.object('profiles/' + id);
this.universalBlockUntilFirst(user$);
return user$;
}
private universalBlockUntilFirst(obs: Observable<any>) {
if (SERVER) {
const task = Zone.current.scheduleMacroTask('universalBlockUntilFirst', () => { }, {}, () => { }, () => { });
obs.first().subscribe(() => this.zone.runOutsideAngular(() => task.invoke()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment