Skip to content

Instantly share code, notes, and snippets.

@SimGus
SimGus / unsubscribe.ts
Last active January 13, 2020 21:01
Observable unsubscribing inside Angular components
////////////////////////////////////////////////////////////////////////////////////////////
// In Angular, we often use *RxJS* observables. //
// //
// A frequent usecase of observables within Angular component is to have a subscription //
// to an observable for the whole lifetime of that component. //
// //
// To do this, we need to subscribe when the component comes up, //
// and we will *HAVE to* unsubscribe when the component is destroyed. //
// (Seriously, not unsubscribing will lead to memory leaking everytime the component gets //
// destroyed.) //
@SimGus
SimGus / maps.ts
Created January 8, 2020 20:42
Maps (dicts) in TypeScript
// When programming, there will certainly be times you will want to associate values to other values,
// that is building a dictionary.
// In TypeScript, a `Map<K, V>` exists (it seems it actually is the one defined in JavaScript ES6).
// However, at least in TypeScript 3.1.6, objects from this class are not very reliable:
// You will sometimes get errors such as "TypeError: map.get is not a function", at seemingly random times.
// A solution is to replace `Map<string, V>` by plain objects defined as `{ [key: string]: V }`.
// Another solution seems to be to replace them by `Record<string, V>`.