Skip to content

Instantly share code, notes, and snippets.

@gustable42
Created January 20, 2021 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustable42/8bc5f6867f7bb0c0826293e19b220b94 to your computer and use it in GitHub Desktop.
Save gustable42/8bc5f6867f7bb0c0826293e19b220b94 to your computer and use it in GitHub Desktop.

6 design tips for non-designers

This post isn't for designers as the title suggests, but can help. I picked 6 great tips for all of you developers like me that aren't a design genius, but like beatiful products and interfaces.

Most of these, I found in Refactoring UI course and it

(Se for pra fazer um artigo desse, preciso comprar o livro do Refactoring UI)

Why you should (almost) always write Typescript interfaces instead of using 'any'

  • Melhora a legibilidade do código
  • Bom para definir bem com o back como espera respostas de requisições
  • Apesar do passo pra trás, te dá velocidade por evitar bugs bestas
  • Use com moderação, criar uma nova interace pode ser um overhead se usada em um escopo muito pequeno

RxJS essentials that every Angular beginner needs to learn

If you don't understand at least the basics, the 101, the bread and butter of RxJS, you're leaving a lot of helpers behind. You're wasting your time and energy! Let me explain:

Angular was grounded in RxJS paradigms and "lifestyle". You won't see Promises been handled very often on a file generated by CLI or on Angular documentation, because Observables can do everything Promise could and even more.

To understand that bold statement, we need to differentiate what is a pull system and a push one. A pull systems is when a data consumer determines when it gets new data from a data producer. An iterator is a pull system, because, whenever it needs more values from the iterated array, for example, it can gets. Push systems is the opposite. It's when the data producer defines when new values will be available for consumer to use. Promise is the favorite answer, basically, when you write a set of instructions inside the .then() callback, you telling that whenever the data producer send the data, those instructions will be executed.

Observables and observers

Understanding what push system is, you can understand why Observables is Promises with superpowers. Mainly, Observables is a push-based structure which can handle multiple values coming from a single data producer, Promises can handle just a single value. Observables start a stream between the producer and consumer forever and ever until it complete its mission.

And before I show you an example to clarify all of that, you need to understand how .subscribe() method is also a .then() method with superpowers. We call .subscribe() by an observer, as the name says, it observes an observable thing. And while it observes new values coming from data producer, it has three parameters that are callbacks, the first: (next => {}) handles every new value, after, (error => {}) handles any Javascript error that data producer sent and last (() => {}) execute one single time when the Observable complete, when its stream is closed. Quick hint: if you explicit don't complete an Observable, it goes forever.

https://gist.github.com/5bc8b7feb580801780e8c210b92b6495

As you can see, I first created an observable, emitted values 2 times and then completed the observable stream, because I don't want my observer observes forever.

Async pipe and Subscription

I already mention twice that Observables can go through infinite and beyond, but we already know that every resource is limited, like your processor, RAM memory, etc. When you're in real life, working with real Angular code, you'll not always build your own Observable and defines when it will be completed, sometimes an Observable will be an event listener, a http request and more, in those cases, you need to manually finish the infinite stream, or .unsubscribe() a Subscription - an execution of an Observable.

When you .unsubscribe(), you cut in half the bond between the Observable and observer, observer no longer observes, then, the infinite statement is over. Because this subscribe-unsubscribe could be really repetitive, Angular implements Async pipe. I'm assuming you know what a pipe here. You can bind a Observable directly in the template followed by a simple: '| async'. Therefore, you get every data producer change right on template and when that component is destroyed, that stream will be unsubscribed without any effort.

Another handy trick Angular does for us is unsubscribe every http request made with HttpClient as default. Imagine if you need to unsubscribe every request, would be a pain in the neck.

pipe() and operators

When you're working with arrays in js, you often goes with a .reduce(), .filter() to handle the source and raw data into a user-friendly data. When you work with Observables, you can do the same, just put those methods inside a pipe() method and start list what you need.

https://gist.github.com/4748a4eea15db80c57c0975bff1f4990

Applying map and filter that way, the console input becomes:

2 4 'It's over'

There are lots of different operators and as you progress on Angular you'll need more and more of those operators, so, go deeper on the RxJS documentations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment