RxJS map function inside of merge function
up vote
1
down vote
favorite
I am trying to merge 3 different observables and identify them as source value. Also I am getting userDetails in withLatestfrom so that I can do operation depending on which observable got triggered (username - u, password - p, age - a). I am confident that there is something wrong in the fact that I am putting map functions inside of Observable.merge and that is why no matter which Observable was supposed to trigger my source is always 'u' in "switch case" statement.
@Effect
public test = Observable.merge(
this.onUserNameUpdate().pipe(
map(() => 'u')),
this.onPasswordUpdate().pipe(
map(() => 'p')),
this.onAgeUpdate().pipe(map(() => 'a')))
.withLatestFrom(this.store.select(getUserDetails),
(source, userDetails) => ({
source,
order
})
)
.map((params) => {
switch (params.source) {
case 'u':
// do smth with username
case 'p':
// do smth with password
case 'a':
// do smth with age
}
return {
// some object
}
})
Could anyone explain what am I doing wrong here?
javascript angular rxjs
|
show 4 more comments
up vote
1
down vote
favorite
I am trying to merge 3 different observables and identify them as source value. Also I am getting userDetails in withLatestfrom so that I can do operation depending on which observable got triggered (username - u, password - p, age - a). I am confident that there is something wrong in the fact that I am putting map functions inside of Observable.merge and that is why no matter which Observable was supposed to trigger my source is always 'u' in "switch case" statement.
@Effect
public test = Observable.merge(
this.onUserNameUpdate().pipe(
map(() => 'u')),
this.onPasswordUpdate().pipe(
map(() => 'p')),
this.onAgeUpdate().pipe(map(() => 'a')))
.withLatestFrom(this.store.select(getUserDetails),
(source, userDetails) => ({
source,
order
})
)
.map((params) => {
switch (params.source) {
case 'u':
// do smth with username
case 'p':
// do smth with password
case 'a':
// do smth with age
}
return {
// some object
}
})
Could anyone explain what am I doing wrong here?
javascript angular rxjs
1
Which version of Rxjs are you using? Are you usingrxjs-compat
as well? I'm asking that coz you're usingObservable.merge
as well as.pipe
which are syntaxes from two different versions of Rxjs.
– SiddAjmera
Nov 9 at 9:26
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
If you're using Rxjs5, then you should not be able to use.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.
– SiddAjmera
Nov 9 at 9:33
1
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57
|
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to merge 3 different observables and identify them as source value. Also I am getting userDetails in withLatestfrom so that I can do operation depending on which observable got triggered (username - u, password - p, age - a). I am confident that there is something wrong in the fact that I am putting map functions inside of Observable.merge and that is why no matter which Observable was supposed to trigger my source is always 'u' in "switch case" statement.
@Effect
public test = Observable.merge(
this.onUserNameUpdate().pipe(
map(() => 'u')),
this.onPasswordUpdate().pipe(
map(() => 'p')),
this.onAgeUpdate().pipe(map(() => 'a')))
.withLatestFrom(this.store.select(getUserDetails),
(source, userDetails) => ({
source,
order
})
)
.map((params) => {
switch (params.source) {
case 'u':
// do smth with username
case 'p':
// do smth with password
case 'a':
// do smth with age
}
return {
// some object
}
})
Could anyone explain what am I doing wrong here?
javascript angular rxjs
I am trying to merge 3 different observables and identify them as source value. Also I am getting userDetails in withLatestfrom so that I can do operation depending on which observable got triggered (username - u, password - p, age - a). I am confident that there is something wrong in the fact that I am putting map functions inside of Observable.merge and that is why no matter which Observable was supposed to trigger my source is always 'u' in "switch case" statement.
@Effect
public test = Observable.merge(
this.onUserNameUpdate().pipe(
map(() => 'u')),
this.onPasswordUpdate().pipe(
map(() => 'p')),
this.onAgeUpdate().pipe(map(() => 'a')))
.withLatestFrom(this.store.select(getUserDetails),
(source, userDetails) => ({
source,
order
})
)
.map((params) => {
switch (params.source) {
case 'u':
// do smth with username
case 'p':
// do smth with password
case 'a':
// do smth with age
}
return {
// some object
}
})
Could anyone explain what am I doing wrong here?
javascript angular rxjs
javascript angular rxjs
edited Nov 9 at 9:35
SiddAjmera
12k21137
12k21137
asked Nov 9 at 9:21
Edster
2610
2610
1
Which version of Rxjs are you using? Are you usingrxjs-compat
as well? I'm asking that coz you're usingObservable.merge
as well as.pipe
which are syntaxes from two different versions of Rxjs.
– SiddAjmera
Nov 9 at 9:26
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
If you're using Rxjs5, then you should not be able to use.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.
– SiddAjmera
Nov 9 at 9:33
1
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57
|
show 4 more comments
1
Which version of Rxjs are you using? Are you usingrxjs-compat
as well? I'm asking that coz you're usingObservable.merge
as well as.pipe
which are syntaxes from two different versions of Rxjs.
– SiddAjmera
Nov 9 at 9:26
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
If you're using Rxjs5, then you should not be able to use.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.
– SiddAjmera
Nov 9 at 9:33
1
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57
1
1
Which version of Rxjs are you using? Are you using
rxjs-compat
as well? I'm asking that coz you're using Observable.merge
as well as .pipe
which are syntaxes from two different versions of Rxjs.– SiddAjmera
Nov 9 at 9:26
Which version of Rxjs are you using? Are you using
rxjs-compat
as well? I'm asking that coz you're using Observable.merge
as well as .pipe
which are syntaxes from two different versions of Rxjs.– SiddAjmera
Nov 9 at 9:26
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
If you're using Rxjs5, then you should not be able to use
.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.– SiddAjmera
Nov 9 at 9:33
If you're using Rxjs5, then you should not be able to use
.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.– SiddAjmera
Nov 9 at 9:33
1
1
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Please try to replace first mixed (pipe
and observable operators) part with some pure RxJS 5 array of observables:
public test = Observable
.merge(
this.onUserNameUpdate()
.map(v => 'u'),
this.onPasswordUpdate()
.map(v => 'p'),
this.onAgeUpdate()
.map(v => 'a')
)
And see If your switch/case is moving accordingly.
As well I see you are getting additional user details depending on what source observable triggered action, I'd enclose this in concatMap
:
.concatMap((trigger) => {
return this.store.select(getUserDetails)
.map(user => {
let ret = user;
ret.trigger = trigger;;
})
})
In the end you got user info with metadata od what triggered change
EDIT
As a result of above code your stream shall be something like this:
Each onXXXUpdate
function that emits observable as a result is merged using merge
operator:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-merge
Because merge operator returns single observable that is capable of emitting every value type that it's "child" observables are emitting, in next operator concatMap
we map each value emitted (regardless of it's source) to new observable that is created as output from this.store.select(getUserDetails)
(I assumed this function returns observable, isn't it?). To keep track of what is the trigger we append this information as meta-data into user info response via map
. Afterwards, in final operator we shall have user info with trigger information that helps to distinguish, what actions shall be taken.
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in staticmerge
. I fixed my answer
– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip thiswithLatestFrom
operator? And yes,...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)
– Tomas
Nov 9 at 12:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Please try to replace first mixed (pipe
and observable operators) part with some pure RxJS 5 array of observables:
public test = Observable
.merge(
this.onUserNameUpdate()
.map(v => 'u'),
this.onPasswordUpdate()
.map(v => 'p'),
this.onAgeUpdate()
.map(v => 'a')
)
And see If your switch/case is moving accordingly.
As well I see you are getting additional user details depending on what source observable triggered action, I'd enclose this in concatMap
:
.concatMap((trigger) => {
return this.store.select(getUserDetails)
.map(user => {
let ret = user;
ret.trigger = trigger;;
})
})
In the end you got user info with metadata od what triggered change
EDIT
As a result of above code your stream shall be something like this:
Each onXXXUpdate
function that emits observable as a result is merged using merge
operator:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-merge
Because merge operator returns single observable that is capable of emitting every value type that it's "child" observables are emitting, in next operator concatMap
we map each value emitted (regardless of it's source) to new observable that is created as output from this.store.select(getUserDetails)
(I assumed this function returns observable, isn't it?). To keep track of what is the trigger we append this information as meta-data into user info response via map
. Afterwards, in final operator we shall have user info with trigger information that helps to distinguish, what actions shall be taken.
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in staticmerge
. I fixed my answer
– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip thiswithLatestFrom
operator? And yes,...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)
– Tomas
Nov 9 at 12:01
add a comment |
up vote
0
down vote
Please try to replace first mixed (pipe
and observable operators) part with some pure RxJS 5 array of observables:
public test = Observable
.merge(
this.onUserNameUpdate()
.map(v => 'u'),
this.onPasswordUpdate()
.map(v => 'p'),
this.onAgeUpdate()
.map(v => 'a')
)
And see If your switch/case is moving accordingly.
As well I see you are getting additional user details depending on what source observable triggered action, I'd enclose this in concatMap
:
.concatMap((trigger) => {
return this.store.select(getUserDetails)
.map(user => {
let ret = user;
ret.trigger = trigger;;
})
})
In the end you got user info with metadata od what triggered change
EDIT
As a result of above code your stream shall be something like this:
Each onXXXUpdate
function that emits observable as a result is merged using merge
operator:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-merge
Because merge operator returns single observable that is capable of emitting every value type that it's "child" observables are emitting, in next operator concatMap
we map each value emitted (regardless of it's source) to new observable that is created as output from this.store.select(getUserDetails)
(I assumed this function returns observable, isn't it?). To keep track of what is the trigger we append this information as meta-data into user info response via map
. Afterwards, in final operator we shall have user info with trigger information that helps to distinguish, what actions shall be taken.
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in staticmerge
. I fixed my answer
– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip thiswithLatestFrom
operator? And yes,...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)
– Tomas
Nov 9 at 12:01
add a comment |
up vote
0
down vote
up vote
0
down vote
Please try to replace first mixed (pipe
and observable operators) part with some pure RxJS 5 array of observables:
public test = Observable
.merge(
this.onUserNameUpdate()
.map(v => 'u'),
this.onPasswordUpdate()
.map(v => 'p'),
this.onAgeUpdate()
.map(v => 'a')
)
And see If your switch/case is moving accordingly.
As well I see you are getting additional user details depending on what source observable triggered action, I'd enclose this in concatMap
:
.concatMap((trigger) => {
return this.store.select(getUserDetails)
.map(user => {
let ret = user;
ret.trigger = trigger;;
})
})
In the end you got user info with metadata od what triggered change
EDIT
As a result of above code your stream shall be something like this:
Each onXXXUpdate
function that emits observable as a result is merged using merge
operator:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-merge
Because merge operator returns single observable that is capable of emitting every value type that it's "child" observables are emitting, in next operator concatMap
we map each value emitted (regardless of it's source) to new observable that is created as output from this.store.select(getUserDetails)
(I assumed this function returns observable, isn't it?). To keep track of what is the trigger we append this information as meta-data into user info response via map
. Afterwards, in final operator we shall have user info with trigger information that helps to distinguish, what actions shall be taken.
Please try to replace first mixed (pipe
and observable operators) part with some pure RxJS 5 array of observables:
public test = Observable
.merge(
this.onUserNameUpdate()
.map(v => 'u'),
this.onPasswordUpdate()
.map(v => 'p'),
this.onAgeUpdate()
.map(v => 'a')
)
And see If your switch/case is moving accordingly.
As well I see you are getting additional user details depending on what source observable triggered action, I'd enclose this in concatMap
:
.concatMap((trigger) => {
return this.store.select(getUserDetails)
.map(user => {
let ret = user;
ret.trigger = trigger;;
})
})
In the end you got user info with metadata od what triggered change
EDIT
As a result of above code your stream shall be something like this:
Each onXXXUpdate
function that emits observable as a result is merged using merge
operator:
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-merge
Because merge operator returns single observable that is capable of emitting every value type that it's "child" observables are emitting, in next operator concatMap
we map each value emitted (regardless of it's source) to new observable that is created as output from this.store.select(getUserDetails)
(I assumed this function returns observable, isn't it?). To keep track of what is the trigger we append this information as meta-data into user info response via map
. Afterwards, in final operator we shall have user info with trigger information that helps to distinguish, what actions shall be taken.
edited Nov 9 at 11:56
answered Nov 9 at 10:36
Tomas
913918
913918
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in staticmerge
. I fixed my answer
– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip thiswithLatestFrom
operator? And yes,...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)
– Tomas
Nov 9 at 12:01
add a comment |
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in staticmerge
. I fixed my answer
– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip thiswithLatestFrom
operator? And yes,...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)
– Tomas
Nov 9 at 12:01
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
Thank you for your effort. Unfortunately, when I used array of observables they are not being triggered at all and it never reaches the switch case statement.
– Edster
Nov 9 at 11:00
whoops, my bad, according to docs, this shall not be an array in static
merge
. I fixed my answer– Tomas
Nov 9 at 11:04
whoops, my bad, according to docs, this shall not be an array in static
merge
. I fixed my answer– Tomas
Nov 9 at 11:04
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
Actually I managed to make it work with array as well. Apparently there is a workaround using the ... operator. Something like that: Observable.merge(...[this.onUserNameUpdate().map(() => 'u'), this.onPasswordUpdate().map(() => 'p'), this.onAgeUpdate().map(() => 'a')]) Unfortunately, I still have the same bug as in OP.
– Edster
Nov 9 at 11:12
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
I tried your updated version even before I posted the question and it has the same problem as well. I am always getting the 'u' response.
– Edster
Nov 9 at 11:13
have you tried to skip this
withLatestFrom
operator? And yes, ...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)– Tomas
Nov 9 at 12:01
have you tried to skip this
withLatestFrom
operator? And yes, ...
operator will convert array to it's entities, so probably rather than calling it a hack it's a syntactic sugar :)– Tomas
Nov 9 at 12:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222899%2frxjs-map-function-inside-of-merge-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Which version of Rxjs are you using? Are you using
rxjs-compat
as well? I'm asking that coz you're usingObservable.merge
as well as.pipe
which are syntaxes from two different versions of Rxjs.– SiddAjmera
Nov 9 at 9:26
I am using rxjs 5. I guess I am supposed to not use Observable.merge then? I am not using rxjs-compat.
– Edster
Nov 9 at 9:31
If you're using Rxjs5, then you should not be able to use
.pipe
I guess. Your operators(map
) would directly chain to the Observable value itself.– SiddAjmera
Nov 9 at 9:33
1
Can you replicate this via a Minimal StackBlitz sample?
– SiddAjmera
Nov 9 at 9:39
Strange, I thought it is other way around... Correct me if I am wrong. blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
– Edster
Nov 9 at 9:57