Skip to content

Commit fd6940b

Browse files
authored
Merge pull request #14 from maniator/cleanup
Cleanup some code in preparation for testing
2 parents 051d5cf + b0acc79 commit fd6940b

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

example/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ const countSubscription = countObservable$
2424
}
2525
});
2626

27-
// // test event binding
28-
// const inputObservable$ = Observable.fromEvent('input', document.getElementById('myInput'), (event) => event.currentTarget.value);
29-
// const divReverse = document.getElementById('myTextReverse');
30-
// const div = document.getElementById('myText');
31-
//
32-
// console.log(inputObservable$);
33-
//
34-
// inputObservable$
35-
// .map((text) => text.split('').reverse().join(''))
36-
// .do((text) => divReverse.textContent = text)
37-
// .subscribe();
38-
//
39-
// inputObservable$
40-
// .do((text) => div.textContent = text)
41-
// .subscribe();
27+
// test event binding
28+
const inputObservable$ = Observable.fromEvent('input', document.getElementById('myInput'), (event) => event.currentTarget.value);
29+
const divReverse = document.getElementById('myTextReverse');
30+
const div = document.getElementById('myText');
31+
32+
console.log(inputObservable$);
33+
34+
inputObservable$
35+
.map((text) => text.split('').reverse().join(''))
36+
.do((text) => divReverse.textContent = text)
37+
.subscribe();
38+
39+
inputObservable$
40+
.do((text) => div.textContent = text)
41+
.subscribe();

src/Observable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export class Observable {
66
this.observerCallback = observerCallback;
77
}
88

9+
static create (observerCallback) {
10+
return new Observable(observerCallback);
11+
}
12+
913
subscribe (next = noop, error = noop, complete = noop) {
1014
return new Subscription(this.observerCallback, { next, error, complete });
1115
}

src/Subject.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,64 @@
1-
import { Subscription } from './Subscription';
1+
import {Subscription} from './Subscription';
22

33
export class Subject {
44
constructor () {
55
this.next = this.next.bind(this);
66
this.error = this.error.bind(this);
77
this.complete = this.complete.bind(this);
88

9-
this.subscriptionList = [];
9+
this.observers = [];
1010
}
1111

1212
subscribe (observer) {
1313
const subscription = Subscription.createSimple(observer);
1414

15-
this.subscriptionList.push(subscription);
15+
this.observers.push(subscription.observer);
1616

1717
return subscription;
1818
}
1919

20+
unsubscribe () {
21+
this
22+
.cleanup((observer) => {
23+
observer.cleanup();
24+
25+
return false;
26+
});
27+
}
28+
2029
cleanup (callback) {
21-
this.subscriptionList = this.subscriptionList.filter((subscription) => {
22-
if (!subscription.isComplete) {
23-
callback(subscription);
30+
this.observers = this.observers.filter((observer) => {
31+
if (!observer.isComplete) {
32+
const ret = callback(observer);
33+
34+
if (typeof ret === 'boolean') {
35+
return ret;
36+
}
37+
2438
return true;
2539
}
2640

2741
return false;
2842
});
43+
44+
return this;
2945
}
3046

3147
next (...args) {
32-
this.cleanup((subscription) => {
33-
subscription.onNext(...args);
48+
this.cleanup((observer) => {
49+
observer.onNext(...args);
3450
});
3551
}
3652

3753
error (...errors) {
38-
this.cleanup((subscription) => {
39-
subscription.onError(...errors);
54+
this.cleanup((observer) => {
55+
observer.onError(...errors);
4056
});
4157
}
4258

4359
complete () {
44-
this.cleanup((subscription) => {
45-
subscription.onComplete();
60+
this.cleanup((observer) => {
61+
observer.onComplete();
4662
});
4763
}
4864
}

src/Subscription.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Observer } from './Observer';
22
import { noop } from './utilities';
33

44
export class Subscription {
5-
constructor (callback, observer) {
5+
constructor (callback = noop, observer = {}) {
66
this.observer = new Observer(observer);
77

88
this.observer.use(callback);

0 commit comments

Comments
 (0)