Skip to content

Commit 9a9d330

Browse files
authored
Merge pull request #32 from tusharmath/composite-subscription-linkedlist
Composite subscription linkedlist
2 parents d1649c4 + f089a43 commit 9a9d330

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/lib/CompositeSubscription.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
import {ISubscription} from '../types/core/ISubscription'
21
/**
32
* Created by tushar.mathur on 12/10/16.
43
*/
54

5+
import {ISubscription} from '../types/core/ISubscription'
6+
import {LinkedList, Node} from './LinkedList'
67

7-
const propClosed = (x: ISubscription) => x.closed
8+
const unsubscribe = (x: ISubscription) => x.unsubscribe()
89

910
export class CompositeSubscription implements ISubscription {
10-
constructor (private subscriptions: ISubscription[] = []) {
11+
public closed = false
12+
private subscriptions: LinkedList<ISubscription>
13+
14+
constructor () {
15+
this.subscriptions = new LinkedList<ISubscription>()
1116
}
1217

1318
add (d: ISubscription) {
14-
this.subscriptions.push(d)
19+
return this.subscriptions.add(d)
1520
}
1621

17-
unsubscribe (): void {
18-
for (var i = 0; i < this.subscriptions.length; i++) {
19-
this.subscriptions[i].unsubscribe()
20-
}
22+
remove (d: Node<ISubscription>) {
23+
d.value.unsubscribe()
24+
return this.subscriptions.remove(d)
2125
}
2226

23-
get closed (): boolean {
24-
return this.subscriptions.map(propClosed).every(Boolean)
27+
unsubscribe (): void {
28+
this.subscriptions.forEach(unsubscribe)
29+
this.closed = true
2530
}
2631
}

src/lib/LinkedList.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55

66
export class Node<T> {
7-
public left: Node<T> | null
8-
public right: Node<T> | null
7+
public left: Node<T> | undefined
8+
public right: Node<T> | undefined
99

1010
constructor (public value: T) {
11-
this.right = null
12-
this.left = null
11+
this.right = undefined
12+
this.left = undefined
1313
}
1414

1515
static of <T> (val: T) {
@@ -19,11 +19,11 @@ export class Node<T> {
1919

2020
export class LinkedList<T> {
2121
public length: number
22-
private __head: Node<T> | null
22+
private __head: Node<T> | undefined
2323

2424
constructor () {
2525
this.length = 0
26-
this.__head = null
26+
this.__head = undefined
2727
}
2828

2929
element () {
@@ -58,12 +58,12 @@ export class LinkedList<T> {
5858
}
5959
else if (n.left) {
6060
this.__head = n.left
61-
n.left.right = null
61+
n.left.right = undefined
6262
}
6363
else if (n.right) {
64-
n.right.left = null
64+
n.right.left = undefined
6565
} else {
66-
this.__head = null
66+
this.__head = undefined
6767
}
6868
this.length--
6969
}

0 commit comments

Comments
 (0)