Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

justinwoo
Copy link
Contributor

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.)

@@ -0,0 +1 @@
export const isArrayLike = ((x: any): boolean => x && typeof x.length === 'number');
Copy link
Member

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)

Copy link
Member

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.

@benlesh
Copy link
Member

benlesh commented Jan 15, 2016

Couldn't this have been as easy as adding:

if (isArrayLike(ish)) {
  return new ArrayObservable(Array.from(ish.length), mapFn);
}

@justinwoo
Copy link
Contributor Author

Right, could be just as simple as return new ArrayObservable(Array.from(ish.length, mapFn, thisArg), scheduler);, but that does front-load transforming the values of the array-like into the transformed Observable, right?

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.

@justinwoo
Copy link
Contributor Author

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 from signature entirely.

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') {
Copy link
Contributor

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.

@luisgabriel
Copy link
Contributor

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);
}

@benlesh
Copy link
Member

benlesh commented Jan 16, 2016

from should have a result selector we can use. If it doesn't, we should add it.

@justinwoo
Copy link
Contributor Author

Result selector for every kind of input so that we use the same method signature as rxjs4 observable.from?

@benlesh
Copy link
Member

benlesh commented Jan 18, 2016

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 Array.from.

expect(x).toBe(expected[i++]);
}, null, done);
});

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added on L30 above.

@justinwoo
Copy link
Contributor Author

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');
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@justinwoo justinwoo force-pushed the from-arraylike branch 2 times, most recently from 90714da to e0f47b3 Compare January 22, 2016 02:24
@kwonoj
Copy link
Member

kwonoj commented Jan 22, 2016

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.
@kwonoj
Copy link
Member

kwonoj commented Jan 31, 2016

Merged with 7245005, thanks @justinwoo .

@kwonoj kwonoj closed this Jan 31, 2016
@lock
Copy link

lock bot commented Jun 7, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants