Skip to content

Commit

Permalink
fix(from): Objects implementing Symbol.observable take precedence ove…
Browse files Browse the repository at this point in the history
…r other types
  • Loading branch information
taras authored and benlesh committed Jun 5, 2018
1 parent db2ac69 commit 80ceea0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions spec/observables/from-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ describe('from', () => {
})
});

const fakeArrayObservable = <T>(...values: T[]) => {
let arr = ['bad array!'];
arr[Symbol.observable] = () => {
return {
subscribe: (observer: Observer<T>) => {
for (const value of values) {
observer.next(value);
}
observer.complete();
}
};
};
return arr;
};

const fakerator = <T>(...values: T[]) => ({
[Symbol.iterator as symbol]: () => {
const clone = [...values];
Expand All @@ -75,6 +90,7 @@ describe('from', () => {
const sources: Array<{ name: string, value: any }> = [
{ name: 'observable', value: of('x') },
{ name: 'observable-like', value: fakervable('x') },
{ name: 'observable-like-array', value: fakeArrayObservable('x') },
{ name: 'array', value: ['x'] },
{ name: 'promise', value: Promise.resolve('x') },
{ name: 'iterator', value: fakerator('x') },
Expand Down
2 changes: 1 addition & 1 deletion src/internal/observable/from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): O
if (input instanceof Observable) {
return input;
}
return new Observable(subscribeTo(input));
return new Observable<T>(subscribeTo(input));
}

if (input != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/util/subscribeTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export const subscribeTo = <T>(result: ObservableInput<T>) => {
return result.subscribe(subscriber);
}
};
} else if (result && typeof result[Symbol_observable] === 'function') {
return subscribeToObservable(result as any);
} else if (isArrayLike(result)) {
return subscribeToArray(result);
} else if (isPromise(result)) {
return subscribeToPromise(result as Promise<any>);
} else if (result && typeof result[Symbol_iterator] === 'function') {
return subscribeToIterable(result as any);
} else if (result && typeof result[Symbol_observable] === 'function') {
return subscribeToObservable(result as any);
} else {
const value = isObject(result) ? 'an invalid object' : `'${result}'`;
const msg = `You provided ${value} where a stream was expected.`
Expand Down

0 comments on commit 80ceea0

Please sign in to comment.