Fireship · @fireship
Custom RxJS Operators by Example
Uploaded August 27, 2018 · Length 9:13
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.
- 0:00
[Music]
- 0:06
one of the most valuable things you can
- 0:08
learn in rxjs is how to create custom
- 0:10
operators they will help you reuse
- 0:13
common logic throughout your application
- 0:14
but more importantly you'll understand
- 0:16
how observables work at a more
- 0:18
fundamental level in this video we'll
- 0:20
cover the basics of custom operators
- 0:22
rxjs and if you're a pro member there's
- 0:24
a follow up video about performing joins
- 0:26
and firestore using custom operators if
- 0:29
you're new to the channel I can
- 0:30
subscribe and you can find the source
- 0:32
code at angular firebase comm I like to
- 0:35
think of an rxjs operator as middleware
- 0:37
for an observable stream a chicken goes
- 0:40
in the operator does something and
- 0:41
nuggets come out there are tons of
- 0:43
operators built into rxjs already but
- 0:46
oftentimes you'll have complex business
- 0:47
logic that doesn't fit one of the
- 0:49
generic operators a common use case that
- 0:52
I run into is joining collections and
- 0:54
documents together from the firestore no
- 0:56
sequel database the code to do this is
- 0:58
relatively complex but I can wrap it all
- 1:00
up in a custom operator and reuse it
- 1:02
throughout my application as needed and
- 1:04
I can even do things like state
- 1:05
management air handling custom logging
- 1:08
inside the operator itself now let's go
- 1:11
ahead and jump into the code for this
- 1:12
video I'll be using the blank typescript
- 1:14
project on stack blitz I have installed
- 1:16
rxjs and then I'm writing a function
- 1:18
called log that will simply write the
- 1:20
value that's emitted from the observable
- 1:22
on the screen to the right so we'll use
- 1:24
the of method to create an observable of
- 1:27
the string hello and you can see that
- 1:29
it's logged here in that browser I'm
- 1:31
going to start by creating a more
- 1:33
explicit complex operator and then we'll
- 1:35
break this down to make it more simple
- 1:37
for your more practical use cases an
- 1:39
operator itself is just a pure function
- 1:42
we'll go ahead and create one called to
- 1:44
uppercase and it will just up case the
- 1:46
characters in the string so this outer
- 1:48
function is our operator and then it
- 1:51
returns an inner function that returns
- 1:52
an observable that feels a little
- 1:54
confusing at first but after you do it a
- 1:56
couple times it'll feel pretty natural
- 1:58
the inner function gives you access to


