Google

Google

947 Followers
Organizing the world's information.
@google
google.com
mountain view
Tools by Google

Decisions 2

Eyas Sharaiha

Software Engineer at Google

One TypeScript / Angular 2 code health recommendation at Google is how to simplify dealing with RxJS Observables. Two common options in Angular are subscribing to an Observable inside of a Component's TypeScript code, versus using something like the AsyncPipe (foo | async) from the template html. We typically recommend the latter for most straightforward use cases (code without side effects, etc.)

I typically review a fair amount of Angular code at work. One thing I typically encourage is using plain Observables in an Angular Component, and using AsyncPipe (foo | async) from the template html to handle subscription, rather than directly subscribing to an observable in a component TS file.

Subscribing in components

Unless you know a subscription you're starting in a component is very finite (e.g. an HTTP request with no retry logic, etc), subscriptions you make in a Component must:

  1. Be closed, stopped, or cancelled when exiting a component (e.g. when navigating away from a page),
  2. Only be opened (subscribed) when a component is actually loaded/visible (i.e. in ngOnInit rather than in a constructor).

AsyncPipe can take care of that for you

Instead of manually implementing component lifecycle hooks, remembering to subscribe and unsubscribe to an Observable, AsyncPipe can do that for you.

I'm sharing a version of this recommendation with some best practices and code samples.

#Typescript #Angular #RXJS #Async #Frontend

21 145.1K

Michael Xin Sun

Software Engineer at Google Inc

Comment from HackerNews (https://news.ycombinator.com/item?id=8998736)

This is how Google Inbox and Google Spreadsheets works. See the slides from my recently produced GWT Create Session (http://t.co/ZvoaHxCoZT). J2ObjC slide deck here (https://docs.google.com/presentation/d/1mW_Plm5jAygELf7qjVK7...) Videos of the conference will be online soon.

Prior to that, some 20% Googlers also produced the PlayN library (https://github.com/threerings/playn) This was taken over by Michael Bayne who added an iOS backend by Bytecode -> IKVM -> Mono conversion. I beleive j2objc and RoboVM backends exist now as well.

The major benefit of the j2objc approach is the avoidance of GC in favor of ARC, the conversion of message-sends into C-method calls when possible, and integration with existing iOS toolchain.

When we started, it seemed like an iffy idea, but after developing a product delivered to millions of users on a high volume site (gmail) that has 70% code sharing, and being able to simultaneously develop, test, and deploy across the platforms reasonably efficiently, a lot of skeptics have become converts to the concept. JUniversal

2 86.7K

Followers 947

Risto Anton
Kartik Bhargav
Angel Woo
natnael tadele
Steven Zgaljic
Gonçalo Ferreira
Diego Azevedo
Luis Appel
Ehab Warda
Baiyang Li
Малик Сулайманов
yamunavsaneesh
dulguun0225
Tuan Conandoyle
Nikolay Shebeko
Delvin Limanto
aboodi8853134
Ashesh Kumar Sinha
Manish  Thakur
Peter Do
cchuang1024
kola siva tharun
chandan patra
Shan Arif
Damrongsak Samanras
Anuj Seth
PromDress MeUk