TIL, 2022-09-09
Decoded Front-end
- Try catch can catch errors but it doesn’t catch ALL errors, and it doesn’t catch async errors.
- Angular has
ErrorHandler
class. We can DI it. Reference unknown
vsany
- will force error to be narrowed first.- Angular does not catch errors that are in
setTimeout
, butErrorHandler
will catch that error.ngZone.onError
is subscribed to byErrorHandler
and isrunOutsideAngular
.
tap
can catch errors. But, we can also usecatchError()
anyway and return a new observable (let’s say an empty array).- We can also combine both, so we can use
tap
to set the component error, and also docatchError
after that replaces the initial error and returns an empty array. So an async pipe in the template will receive what was returned withcatchError
. - HTTP interceptor = can catch errors on a global scale.
Push-based Architectures with RxJS
- Traditional pull-based architecture: the views will explicitly call service methods to “force-reload” the data. Consider
findAllUsers
to retried a list of users.- Issues: How do the views know when the data is updated?
- How do unrelated views get notified that new data is available?
- Should uncoupled view components poll for updated data?
- Why is my shared data changing, and who is changing the data when?
- Push-based architecture:
- Direct data access is prohibited. The true data is exposed via a service.
- Data is always immutable. Data is read-only. Changes to the inside properties are not allowed.
- We are encouraged to write passive view components.
- Views that only render when the data arrives via a push-stream.
- Views that delegate user interactions to a non-view layer.
- Views that require no business-logic testing.