RxJS in-depth: Github repo ‘utils’ directory review (part 1)
Tags: angular, rxjs, webdev
Published: 2020-01-23 17:01:32

---
title: RxJS in-depth: Github repo ‘utils’ directory review (part 1)
published: true
date: 2020-01-22 09:37:00 UTC
tags: angular,rxjs,web-development
canonical_url: https://medium.com/angular-in-depth/rxjs-in-depth-github-repo-utils-directory-review-part-1-d0aa15adaeb9
cover_image: https://thepracticaldev.s3.amazonaws.com/i/fu3b3dbaht3y1cyt4kws.jpeg
---

_Curiosity leads to discoveries._


_RxJS is quite a popular library in Angular world so for me it is always interesting to find out something new about its internals. Today I want to dig into_ [**_utils_**](https://github.com/ReactiveX/rxjs/tree/6.5.1/src/internal/util) _folder of its GitHub_ [_repo_](https://github.com/ReactiveX/rxjs)_. This article is written not for some practical usage but rather for reverse engineering enthusiasts and my curiosity._

Here is a link to RxJS 6.5 /[src](https://github.com/ReactiveX/rxjs/tree/6.5.1/src)/[internal](https://github.com/ReactiveX/rxjs/tree/6.5.1/src/internal)/[util](https://github.com/ReactiveX/rxjs/tree/6.5.1/src/internal/util)/ dir. This folder is full of files. Let’s review them one by one. Some of them can bring something interesting 🔍.

#### #1 [ArgumentOutOfRangeError.ts](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/util/ArgumentOutOfRangeError.ts)

The main code is next:

```ts
function **ArgumentOutOfRangeErrorImpl** (this: any) {  
 Error.call(this);  
 this.message = 'argument out of range';  
 this.name = 'ArgumentOutOfRangeError';  
 return this;
}

_ArgumentOutOfRangeErrorImpl_.prototype = Object._create_(Error.prototype);

_export const_ ArgumentOutOfRangeError : ArgumentOutOfRangeErrorCtor = _ArgumentOutOfRangeErrorImpl as any_;
```

**What is interesting here?**

a) We inherit from built-in Error class here. So we can _throw_ calls instance with the message we need.

b) superclass constructor is called in an explicit way: Error.call(this);

**Where it is used in RxJS source code?**

Let's take a look at [**_takeLast_**](https://rxjs-dev.firebaseapp.com/api/operators/takeLast)**_(count)_** operator — it should emit last _count_ values after the source observable is complete. Of cause it cannot accept negative count — [so](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/operators/takeLast.ts#L40):

```
 When using takeLast(i), it delivers an ArgumentOutOrRangeError to the Observer's error callback if i < 0
```

at [line 63](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/operators/takeLast.ts#L63):

```ts
if (this.total < 0) { throw new ArgumentOutOfRangeError; }
```

**Why should I know this?**

Reading source code can help you to understand how the library works in edge cases. And sometimes it is the only way to understand why it works how it works.

#### #2 [EmptyError.ts](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/util/EmptyError.ts)

It looks very similar to ArgumentOutOfRangeError

```ts
function EmptyErrorImpl(this: any) {
  Error.call(this);
  this.message = 'no elements in sequence';
  this.name = 'EmptyError';
  return this;
}

EmptyErrorImpl.prototype = Object.create(Error.prototype);

export const EmptyError: EmptyErrorCtor = EmptyErrorImpl as any;
```

But the application scope is different.

**Where it is used in code?**

Let's review [**_first_**](https://rxjs-dev.firebaseapp.com/api/operators/first) operator — it should emit only the first value (or the first value that meets some condition) emitted by the source Observable.

But what is sequence is empty? We can find an answer at [line 69](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/operators/first.ts#L69) of [rxjs](https://github.com/ReactiveX/rxjs/tree/6.5.1)/[src](https://github.com/ReactiveX/rxjs/tree/6.5.1/src)/[internal](https://github.com/ReactiveX/rxjs/tree/6.5.1/src/internal)/[operators](https://github.com/ReactiveX/rxjs/tree/6.5.1/src/internal/operators)/[first.ts](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/operators/first.ts)

```
 Delivers an EmptyError to the Observer's `error` callback if the Observable completes before any `next` notification was sent.  
 
```

And this happens in lines 86–90:

```ts
return (source: Observable<T>) => source.pipe(
    predicate ? filter((v, i) => predicate(v, i, source)) : identity,
    take(1),
    hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new **EmptyError** ()),
  );
```

**\*Interesting remark:** you can use **take(1)** instead of **first()** with almost the same result:

```ts
source$.pipe(take(1))
vs
source$.pipe(first())
```

The only difference that **_take_** operator will not emit **EmptyError** if source$ completes before producing value.

**Why should I know this?**  
Since **first** operator uses **EmptyError**  — a final code bundle should contain [**EmptyError.ts**](https://github.com/ReactiveX/rxjs/blob/6.5.1/src/internal/util/EmptyError.ts) file too. So when you use **first** operator — a final bundle size will be a bit bigger. You can read more about it [here](https://swalsh.org/blog/rxjs-first-vs-take1/).


[Continue reading...](https://medium.com/angular-in-depth/rxjs-in-depth-github-repo-utils-directory-review-part-1-d0aa15adaeb9)


*** 

I am preparing my future video-course with advanced techniques of mastering Angular/RxJS. Want to get a notification when it is done? Leave your email here (and get free video-course): http://eepurl.com/gHF0av

Like this article? Follow me on [Twitter](https://twitter.com/El_Extremal)!