-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(from): allow Observable.from to handle array-like objects #1195
Conversation
@@ -0,0 +1 @@ | |||
export const isArrayLike = ((x: any): boolean => x && typeof x.length === 'number'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make this a type guard, so TS can better infer types inside the if block;
export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number');
(edit: @Blesh ... updated code block format to TypeScript)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra parens around both of these examples confused me.
Also, protip: in Github if you start with TypeScript it will format the code block as TypeScript.
js for JavaScript, etc. ```ts might work.
Couldn't this have been as easy as adding: if (isArrayLike(ish)) {
return new ArrayObservable(Array.from(ish.length), mapFn);
} |
Right, could be just as simple as If there's nothing about the scheduler semantics that needs to be worked out, then that would be fine with me, but I was delaying the transformation of the values until they are called for either on subscription or scheduler dispatch. |
0320925
to
8e9dc97
Compare
Updated based on feedback, but would love to hear if I can just get rid of most of this code or just merge this into the normal ArrayObservable and change the |
static create<T>(ish: any, mapFnOrScheduler: Scheduler | ((x: number, y: any) => T), thisArg?: any, lastScheduler?: Scheduler): Observable<T> { | ||
let scheduler: Scheduler; | ||
let mapFn: (x: number, y: any) => T; | ||
if (typeof mapFnOrScheduler === 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use isFunction() here.
What about this: //import {map} from '../operator/map'
if (isArrayLike(ish)) {
const source = new ArrayObservable(Array.from(ish.length), scheduler);
return map.call(source, mapFn, thisArg);
} |
8e9dc97
to
85cc8af
Compare
|
Result selector for every kind of input so that we use the same method signature as rxjs4 observable.from? |
Actually, I've thought it over. This is the proper implementation. Disregard my silly idea above, you don't want to allocate another array with |
expect(x).toBe(expected[i++]); | ||
}, null, done); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test for arguments
? I suspect this will be a primary use case for this, since it's the most common ArrayLike.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added on L30 above.
85cc8af
to
2c265f2
Compare
Updated and all based on feedback. Are we okay with the fromArray/fromArrayLike split? |
@@ -0,0 +1 @@ | |||
export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have common usage's of this function over codebase except from
? if not, what about defer to export it once there's usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not crowd from
with barely related defs and end up having split history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it contains multiple lines I'd definitely agree, in this case would like to follow suggestion in #1211 (comment) , similar case to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you insist. Pushed it up now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't trying to insist, apologizes for my tone makes you felt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine, I'm just biased towards separating it out.
90714da
to
e0f47b3
Compare
Change looks good to me, let me leave PR opened bit more to see if any other suggestions around. |
this brings the signature and functionality closer to `Observable.from` that is in RxJS4.
e0f47b3
to
8bde821
Compare
Merged with 7245005, thanks @justinwoo . |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
per #1166, this brings the signature and functionality closer to
Observable.from
that is in RxJS4.
Cover your eyes, folks, this ain't pretty. Would appreciate feedback on how this should be changed. (e.g. should the default signature just be the 4-arg one with the mapFn? should fromArray just do the mapFn stuff by default and merge definitions with this? etc.)