Twitter | Search | |
Victor (Vitya) Savkin
Every single Angular app I've looked at has a lot of bugs due to an incorrectly used switchMap. It's the biggest source of RxJS-related issues.
Reply Retweet Like More
Victor (Vitya) Savkin 13 Feb 18
Replying to @victorsavkin
e.g., using switchMap in an NgRx effect is almost never what you want
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @EFFECT
The following is correct: () fetchItems = this.actions.pipe( ofType(FetchItems), switchMap(a => this.backend.fetchItems()), ... )
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @EFFECT
The following is not correct: () fetchItem = this.actions.pipe( ofType(FetchItem), switchMap(a => this.backend.fetchItem()) )
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @EFFECT
The following is not correct: () updateItem = this.actions.pipe( ofType(UpdateItem), switchMap(a => this.b.updateItem(, a.payload)) ... )
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @EFFECT
The following is not correct: () updateItem = this.actions.pipe( ofType(AddItem), switchMap(a => this.backend.addItem(, a.payload)), ... )
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @victorsavkin
Most naive implementations of the client-server interactions using switchMap are broken.
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @victorsavkin
You have to be very careful to use switchMap correctly--race conditions are hard to spot. Replacing switchMap with concatMap fixes all the examples above.
Reply Retweet Like
Ward Bell 14 Feb 18
Replying to @victorsavkin
But folks fail to connect what it _does_ with what they’re _trying to do_ 1) Canceling a fetch and starting a new one is safe (because idempotent) 2) Canceling an add or update is clearly not what you have in mind, rather you want to add or update another one. 2/3
Reply Retweet Like
Victor (Vitya) Savkin 14 Feb 18
Replying to @wardbell
even with a fetch, often you can only cancel it if you are fetching the same entity twice. Most NgRx effects don't work like that--they have a single effect fetching different entities of the same type.
Reply Retweet Like
Alex 🦅 Eagle 13 Feb 18
Replying to @victorsavkin
Can we catch these in Tsetse or Lint?
Reply Retweet Like
Victor (Vitya) Savkin 13 Feb 18
Replying to @Jakeherringbone
Certain scenarios can be detected. I tried to "solve" it by providing concrete operators that use switchMap/concatMap/mergeMap under the hood (e.g., optimisticUpdate, pessimisticUpdate), The right *Map is picked based on what the developer provided.
Reply Retweet Like