Skip to content

Commit

Permalink
perf(Stream): remove this.num in Stream to improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Medeiros committed Feb 27, 2016
1 parent 13bd699 commit 53bcaad
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import {LastProducer} from './operator/LastProducer';

export class Stream<T> implements Observer<T> {
public observers: Array<Observer<T>>;
public num: number; // Number of non-operator subscribers

constructor(public machine: Producer<T>) {
this.observers = [];
this.num = 0;
}

next(x: T): void {
Expand Down Expand Up @@ -52,14 +50,14 @@ export class Stream<T> implements Observer<T> {

subscribe(observer: Observer<T>): void {
this.observers.push(observer);
if (++this.num === 1) this.machine.start(this);
if (this.observers.length === 1) this.machine.start(this);
}

unsubscribe(observer: Observer<T>): void {
const i = this.observers.indexOf(observer);
if (i > -1) {
this.observers.splice(i, 1);
if (--this.num <= 0) this.machine.stop();
if (this.observers.length <= 0) this.machine.stop();
}
}

Expand Down

0 comments on commit 53bcaad

Please sign in to comment.