Skip to content

Commit 3ca435b

Browse files
docs(no-subject-unsubscribe): document add ban (#112)
Adds documentation about previously undocumented (but tested) behavior. Also adds rule details for this rule.
1 parent 636e6a5 commit 3ca435b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/rules/no-subject-unsubscribe.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@
99
This rule effects failures if the `unsubscribe` method is called on subjects.
1010
The method behaves differently to the `unsubscribe` method on subscriptions and is often an error.
1111

12+
This rule also effects failures if a subject is passed to a subscription's `add` method.
13+
Adding a subject to a subscription will cause the subject's `unsubscribe` method to get called
14+
when the subscription is unsubscribed.
15+
16+
## Rule details
17+
18+
Examples of **incorrect** code for this rule:
19+
20+
```ts
21+
import { Subject } from "rxjs";
22+
23+
const subject = new Subject<number>();
24+
subject.unsubscribe();
25+
```
26+
27+
```ts
28+
import { Subject, Subscription } from "rxjs";
29+
30+
const subject = new Subject<number>();
31+
const subscription = new Subscription();
32+
subscription.add(subject);
33+
```
34+
35+
Examples of **correct** code for this rule:
36+
37+
```ts
38+
import { Subject } from "rxjs";
39+
40+
const subject = new Subject<number>();
41+
subject.complete();
42+
```
43+
1244
## When Not To Use It
1345

1446
If you intentionally use `unsubscribe` to cause errors when subjects are `next`-ed after closing,
@@ -19,6 +51,7 @@ Type checked lint rules are more powerful than traditional lint rules, but also
1951
## Further reading
2052

2153
- [Closed Subjects](https://ncjamieson.com/closed-subjects/)
54+
- [Composing Subscription](https://ncjamieson.com/composing-subscriptions/)
2255

2356
## Related To
2457

0 commit comments

Comments
 (0)