Fireship · @fireship

Custom RxJS Operators by Example

Uploaded August 27, 2018 · Length 9:13

Custom RxJS Operators by Example

Summary

Learn how to create custom RxJS operators by example, including creating a custom pipeable operator from scratch and using existing RxJS operators to simplify the code.

  • rxjs
  • custom operators
  • rxjs operators
  • rxjs examples
  • rxjs tutorial

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    one of the most valuable things you can

  3. 0:08

    learn in rxjs is how to create custom

  4. 0:10

    operators they will help you reuse

  5. 0:13

    common logic throughout your application

  6. 0:14

    but more importantly you'll understand

  7. 0:16

    how observables work at a more

  8. 0:18

    fundamental level in this video we'll

  9. 0:20

    cover the basics of custom operators

  10. 0:22

    rxjs and if you're a pro member there's

  11. 0:24

    a follow up video about performing joins

  12. 0:26

    and firestore using custom operators if

  13. 0:29

    you're new to the channel I can

  14. 0:30

    subscribe and you can find the source

  15. 0:32

    code at angular firebase comm I like to

  16. 0:35

    think of an rxjs operator as middleware

  17. 0:37

    for an observable stream a chicken goes

  18. 0:40

    in the operator does something and

  19. 0:41

    nuggets come out there are tons of

  20. 0:43

    operators built into rxjs already but

  21. 0:46

    oftentimes you'll have complex business

  22. 0:47

    logic that doesn't fit one of the

  23. 0:49

    generic operators a common use case that

  24. 0:52

    I run into is joining collections and

  25. 0:54

    documents together from the firestore no

  26. 0:56

    sequel database the code to do this is

  27. 0:58

    relatively complex but I can wrap it all

  28. 1:00

    up in a custom operator and reuse it

  29. 1:02

    throughout my application as needed and

  30. 1:04

    I can even do things like state

  31. 1:05

    management air handling custom logging

  32. 1:08

    inside the operator itself now let's go

  33. 1:11

    ahead and jump into the code for this

  34. 1:12

    video I'll be using the blank typescript

  35. 1:14

    project on stack blitz I have installed

  36. 1:16

    rxjs and then I'm writing a function

  37. 1:18

    called log that will simply write the

  38. 1:20

    value that's emitted from the observable

  39. 1:22

    on the screen to the right so we'll use

  40. 1:24

    the of method to create an observable of

  41. 1:27

    the string hello and you can see that

  42. 1:29

    it's logged here in that browser I'm

  43. 1:31

    going to start by creating a more

  44. 1:33

    explicit complex operator and then we'll

  45. 1:35

    break this down to make it more simple

  46. 1:37

    for your more practical use cases an

  47. 1:39

    operator itself is just a pure function

  48. 1:42

    we'll go ahead and create one called to

  49. 1:44

    uppercase and it will just up case the

  50. 1:46

    characters in the string so this outer

  51. 1:48

    function is our operator and then it

  52. 1:51

    returns an inner function that returns

  53. 1:52

    an observable that feels a little

  54. 1:54

    confusing at first but after you do it a

  55. 1:56

    couple times it'll feel pretty natural

  56. 1:58

    the inner function gives you access to