Akita: query.selectActive should return only Observable<SomeClass[]>

Created on 20 Mar 2019  路  3Comments  路  Source: datorama/akita

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[x] Other... Please describe:

Current behavior

The return of this.query.selectActive(); is typed as Observable<SomeModel[]> | Observable<SomeModel>

Expected behavior

I believe it should be typed as Observable<SomeModel[]> only, or at least Observable<SomeModel[] | SomeModel>.

Currently it is Observable<SomeModel[]> | Observable<SomeModel>

What is the motivation / use case for changing the behavior?

1 - Typescript don't allow to use the .pipe(map(x => ...))

Have to 'cast' to Observable<any> if want to use the map

(this.query.selectActive() as Observable<any>).pipe(map(item => item instanceof Array ? item : [item]));

2 - It's easier to get the first element of array (if only one is allowed)

3 - I believe the Observable is for sure the return type. The doubt here is whether it is an array or not. So, Observable<SomeModel[] | SomeModel> is better (if not Observable<SomeModel[]>).

Environment


Angular version: 7.3.0


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Others:

help wanted question

Most helpful comment

You need to implement the right interface - ActiveState for single and MultiActiveState for multiple. Please, read the docs. https://netbasal.gitbook.io/akita/entity-store/entity-store/active-state

All 3 comments

You need to implement the right interface - ActiveState for single and MultiActiveState for multiple. Please, read the docs. https://netbasal.gitbook.io/akita/entity-store/entity-store/active-state

Advanced comment: Only relevant if generically extending QueryEntity as a helper subclass.

I ran into a related issue when creating my own QueryEntityExtended class that I inherit from. I add various helper functions in here

for example: QueryEntityExtended<...> extends QueryEntity<...>

You can actually cast Observable<E[]> | Observable<E> to Observable<E[] | E> and then run the Array.isArray(...) check within your pipe(...)

So I ended up with:

(this.selectActive() as Observable<E | E[]>).pipe(switchMap(...))

Like I said this is only relevant AFAIK if you're extending the generic class - in which case it won't know what ActiveState you're using. So for most people finding this you probably just need to read the ActiveState docs.

@simeyla wow, I faced the exact problem as yours, and I named my helper class as ExtendedQueryEntity lol. so lucky to find your answer here, after several hours in the problem...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amr-alamir picture amr-alamir  路  6Comments

mikejr83 picture mikejr83  路  6Comments

johanrin picture johanrin  路  7Comments

stherrienaspnet picture stherrienaspnet  路  3Comments

twittwer picture twittwer  路  7Comments