-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(from): allow Observable.from to handle array-like objects
this brings the signature and functionality closer to `Observable.from` that is in RxJS4.
- Loading branch information
Showing
4 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {Scheduler} from '../Scheduler'; | ||
import {Observable} from '../Observable'; | ||
import {Subscriber} from '../Subscriber'; | ||
import {Subscription} from '../Subscription'; | ||
|
||
export class ArrayLikeObservable<T> extends Observable<T> { | ||
|
||
private mapFn: (x: number, y: any) => T; | ||
|
||
static create<T>(arrayLike: ArrayLike<T>, mapFn: (x: number, y: any) => T, thisArg: any, scheduler?: Scheduler) { | ||
return new ArrayLikeObservable(arrayLike, mapFn, thisArg, scheduler); | ||
} | ||
|
||
static dispatch(state: any) { | ||
|
||
const { arrayLike, index, count, mapFn, subscriber } = state; | ||
|
||
if (index >= count) { | ||
subscriber.complete(); | ||
return; | ||
} | ||
|
||
const result = mapFn ? mapFn(index, arrayLike[index]) : arrayLike[index]; | ||
subscriber.next(result); | ||
|
||
if (subscriber.isUnsubscribed) { | ||
return; | ||
} | ||
|
||
state.index = index + 1; | ||
|
||
(<any> this).schedule(state); | ||
} | ||
|
||
// value used if Array has one value and _isScalar | ||
value: any; | ||
|
||
constructor(public arrayLike: ArrayLike<T>, mapFn: (x: number, y: any) => T, thisArg: any, public scheduler?: Scheduler) { | ||
super(); | ||
if (!scheduler && arrayLike.length === 1) { | ||
this._isScalar = true; | ||
this.value = arrayLike[0]; | ||
} | ||
if (mapFn) { | ||
this.mapFn = mapFn.bind(thisArg); | ||
} | ||
} | ||
|
||
_subscribe(subscriber: Subscriber<T>): Subscription | Function | void { | ||
let index = 0; | ||
const { arrayLike, mapFn, scheduler } = this; | ||
const count = arrayLike.length; | ||
|
||
if (scheduler) { | ||
return scheduler.schedule(ArrayLikeObservable.dispatch, 0, { | ||
arrayLike, index, count, mapFn, subscriber | ||
}); | ||
} else { | ||
for (let i = 0; i < count && !subscriber.isUnsubscribed; i++) { | ||
let result = mapFn ? mapFn(i, arrayLike[i]) : arrayLike[i]; | ||
subscriber.next(result); | ||
} | ||
subscriber.complete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number'); |