Skip to content

Commit

Permalink
refactor(core): revert to TypeScript v2.0, from v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Dec 22, 2016
1 parent 7ee823d commit d408290
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"ts-node": "1.7.2",
"tsify": "2.0.3",
"tslint": "4.0.2",
"typescript": "2.1.4",
"typescript": "2.0.x",
"typings": "2.0.0",
"uglify-js": "^2.6.2",
"validate-commit-msg": "^2.4.0"
Expand Down
57 changes: 32 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface InternalListener<T> {
_c: () => void;
}

export interface PartialListener<T> {
next?: (x: T) => void;
error?: (err: any) => void;
complete?: () => void;
}

const NO_IL: InternalListener<any> = {
_n: noop,
_e: noop,
Expand Down Expand Up @@ -71,14 +77,15 @@ export interface Observable<T> {
};

// mutates the input
function internalizeProducer<T>(producer: Producer<T> & Partial<InternalProducer<T>>) {
producer._start = function _start(il: InternalListener<T> & Partial<Listener<T>>) {
il.next = il._n;
il.error = il._e;
il.complete = il._c;
this.start(il);
};
producer._stop = producer.stop;
function internalizeProducer<T>(producer: Producer<T>) {
(producer as InternalProducer<T> & Producer<T>)._start =
function _start(il: InternalListener<T>) {
(il as InternalListener<T> & Listener<T>).next = il._n;
(il as InternalListener<T> & Listener<T>).error = il._e;
(il as InternalListener<T> & Listener<T>).complete = il._c;
this.start(il as InternalListener<T> & Listener<T>);
};
(producer as InternalProducer<T> & Producer<T>)._stop = producer.stop;
}

class StreamSub<T> implements Subscription {
Expand Down Expand Up @@ -1336,22 +1343,22 @@ export class Stream<T> implements InternalListener<T> {
/**
* Adds a Listener to the Stream.
*
* @param {Listener<T>} listener
* @param {Listener} listener
*/
addListener(listener: Partial<Listener<T> & InternalListener<T>>): void {
listener._n = listener.next || noop;
listener._e = listener.error || noop;
listener._c = listener.complete || noop;
this._add(listener as InternalListener<T>);
addListener(listener: PartialListener<T>): void {
(listener as InternalListener<T> & Listener<T>)._n = listener.next || noop;
(listener as InternalListener<T> & Listener<T>)._e = listener.error || noop;
(listener as InternalListener<T> & Listener<T>)._c = listener.complete || noop;
this._add(listener as InternalListener<T> & Listener<T>);
}

/**
* Removes a Listener from the Stream, assuming the Listener was added to it.
*
* @param {Listener<T>} listener
*/
removeListener(listener: Partial<Listener<T> & InternalListener<T>>): void {
this._remove(listener as InternalListener<T>);
removeListener(listener: Listener<T>): void {
this._remove(listener as InternalListener<T> & Listener<T>);
}

/**
Expand Down Expand Up @@ -1384,15 +1391,15 @@ export class Stream<T> implements InternalListener<T> {
* start, generate events, and stop the Stream.
* @return {Stream}
*/
static create<T>(producer?: Producer<T> & Partial<InternalProducer<T>>): Stream<T> {
static create<T>(producer?: Producer<T>): Stream<T> {
if (producer) {
if (typeof producer.start !== 'function'
|| typeof producer.stop !== 'function') {
throw new Error('producer requires both start and stop functions');
}
internalizeProducer(producer); // mutates the input
}
return new Stream(producer as InternalProducer<T>);
return new Stream(producer as InternalProducer<T> & Producer<T>);
}

/**
Expand All @@ -1403,11 +1410,11 @@ export class Stream<T> implements InternalListener<T> {
* start, generate events, and stop the Stream.
* @return {MemoryStream}
*/
static createWithMemory<T>(producer?: Producer<T> & Partial<InternalProducer<T>>): MemoryStream<T> {
static createWithMemory<T>(producer?: Producer<T>): MemoryStream<T> {
if (producer) {
internalizeProducer(producer); // mutates the input
}
return new MemoryStream<T>(producer as InternalProducer<T>);
return new MemoryStream<T>(producer as InternalProducer<T> & Producer<T>);
}

/**
Expand Down Expand Up @@ -2128,16 +2135,16 @@ export class Stream<T> implements InternalListener<T> {
*
* @param {Listener<T>} listener
*/
setDebugListener(listener: Partial<Listener<T> & InternalListener<T>> | null | undefined) {
setDebugListener(listener: Listener<T> | null | undefined) {
if (!listener) {
this._d = false;
this._dl = NO as InternalListener<T>;
} else {
this._d = true;
listener._n = listener.next || noop;
listener._e = listener.error || noop;
listener._c = listener.complete || noop;
this._dl = listener as InternalListener<T>;
(listener as InternalListener<T> & Listener<T>)._n = listener.next || noop;
(listener as InternalListener<T> & Listener<T>)._e = listener.error || noop;
(listener as InternalListener<T> & Listener<T>)._c = listener.complete || noop;
this._dl = listener as InternalListener<T> & Listener<T>;
}
}
}
Expand Down

0 comments on commit d408290

Please sign in to comment.