How to convert an Observable into a BehaviorSubject?
up vote
0
down vote
favorite
I'm trying to convert an Observable into a BehaviorSubject. Like this:
a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// 🔴
I have also tried:
a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// 🔴
And:
a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// 🔴
And:
a$ = new Observable()
b$ = a$.pipe(
toBehaviorSubject(123)
)
// 🔴
But none of these works. For now I have to implement like this:
a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// 🔵
This would be a little bit ugly in a class:
class Foo() {
a$ = new Observable() // Actually, a$ is more complicated than this.
b$ = new BehaviorSubject(123)
constructor() {
this.a$.subscribe(this.b$)
}
}
So, is there a simpler way to convert a Observable to a BehaviorSubject without using class constructor?
This is my real case:
export class Foo {
autoCompleteItems$ = new BehaviorSubject<string>(null)
autoCompleteSelected$ = new BehaviorSubject<number>(-1)
autoCompleteSelectedChange$ = new Subject<'up'|'down'>()
constructor() {
this.autoCompleteItems$.pipe(
switchMap((items) => {
if (!items) return EMPTY
return this.autoCompleteSelectedChange$.pipe(
startWith('down'),
scan<any, number>((acc, value) => {
if (value === 'up') {
if (acc <= 0) {
return items.length - 1
} else {
return acc - 1
}
} else {
if (acc >= items.length - 1) {
return 0
} else {
return acc + 1
}
}
}, -1)
)
})
).subscribe(this.autoCompleteSelected$)
}
doAutoComplete = () => {
const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
// do something with `item`
}
}
javascript typescript rxjs observable behaviorsubject
|
show 1 more comment
up vote
0
down vote
favorite
I'm trying to convert an Observable into a BehaviorSubject. Like this:
a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// 🔴
I have also tried:
a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// 🔴
And:
a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// 🔴
And:
a$ = new Observable()
b$ = a$.pipe(
toBehaviorSubject(123)
)
// 🔴
But none of these works. For now I have to implement like this:
a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// 🔵
This would be a little bit ugly in a class:
class Foo() {
a$ = new Observable() // Actually, a$ is more complicated than this.
b$ = new BehaviorSubject(123)
constructor() {
this.a$.subscribe(this.b$)
}
}
So, is there a simpler way to convert a Observable to a BehaviorSubject without using class constructor?
This is my real case:
export class Foo {
autoCompleteItems$ = new BehaviorSubject<string>(null)
autoCompleteSelected$ = new BehaviorSubject<number>(-1)
autoCompleteSelectedChange$ = new Subject<'up'|'down'>()
constructor() {
this.autoCompleteItems$.pipe(
switchMap((items) => {
if (!items) return EMPTY
return this.autoCompleteSelectedChange$.pipe(
startWith('down'),
scan<any, number>((acc, value) => {
if (value === 'up') {
if (acc <= 0) {
return items.length - 1
} else {
return acc - 1
}
} else {
if (acc >= items.length - 1) {
return 0
} else {
return acc + 1
}
}
}, -1)
)
})
).subscribe(this.autoCompleteSelected$)
}
doAutoComplete = () => {
const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
// do something with `item`
}
}
javascript typescript rxjs observable behaviorsubject
1
What is the usecase for this? You can typically use justmerge
. Usingsubscribe
is the most Rx way I think.
– martin
Nov 19 at 10:00
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look atshareReply
or a sequence ofpublishReplay
andrefCount
– Picci
Nov 19 at 10:08
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
Nov 19 at 10:16
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
Nov 19 at 13:01
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to convert an Observable into a BehaviorSubject. Like this:
a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// 🔴
I have also tried:
a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// 🔴
And:
a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// 🔴
And:
a$ = new Observable()
b$ = a$.pipe(
toBehaviorSubject(123)
)
// 🔴
But none of these works. For now I have to implement like this:
a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// 🔵
This would be a little bit ugly in a class:
class Foo() {
a$ = new Observable() // Actually, a$ is more complicated than this.
b$ = new BehaviorSubject(123)
constructor() {
this.a$.subscribe(this.b$)
}
}
So, is there a simpler way to convert a Observable to a BehaviorSubject without using class constructor?
This is my real case:
export class Foo {
autoCompleteItems$ = new BehaviorSubject<string>(null)
autoCompleteSelected$ = new BehaviorSubject<number>(-1)
autoCompleteSelectedChange$ = new Subject<'up'|'down'>()
constructor() {
this.autoCompleteItems$.pipe(
switchMap((items) => {
if (!items) return EMPTY
return this.autoCompleteSelectedChange$.pipe(
startWith('down'),
scan<any, number>((acc, value) => {
if (value === 'up') {
if (acc <= 0) {
return items.length - 1
} else {
return acc - 1
}
} else {
if (acc >= items.length - 1) {
return 0
} else {
return acc + 1
}
}
}, -1)
)
})
).subscribe(this.autoCompleteSelected$)
}
doAutoComplete = () => {
const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
// do something with `item`
}
}
javascript typescript rxjs observable behaviorsubject
I'm trying to convert an Observable into a BehaviorSubject. Like this:
a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// 🔴
I have also tried:
a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// 🔴
And:
a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// 🔴
And:
a$ = new Observable()
b$ = a$.pipe(
toBehaviorSubject(123)
)
// 🔴
But none of these works. For now I have to implement like this:
a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// 🔵
This would be a little bit ugly in a class:
class Foo() {
a$ = new Observable() // Actually, a$ is more complicated than this.
b$ = new BehaviorSubject(123)
constructor() {
this.a$.subscribe(this.b$)
}
}
So, is there a simpler way to convert a Observable to a BehaviorSubject without using class constructor?
This is my real case:
export class Foo {
autoCompleteItems$ = new BehaviorSubject<string>(null)
autoCompleteSelected$ = new BehaviorSubject<number>(-1)
autoCompleteSelectedChange$ = new Subject<'up'|'down'>()
constructor() {
this.autoCompleteItems$.pipe(
switchMap((items) => {
if (!items) return EMPTY
return this.autoCompleteSelectedChange$.pipe(
startWith('down'),
scan<any, number>((acc, value) => {
if (value === 'up') {
if (acc <= 0) {
return items.length - 1
} else {
return acc - 1
}
} else {
if (acc >= items.length - 1) {
return 0
} else {
return acc + 1
}
}
}, -1)
)
})
).subscribe(this.autoCompleteSelected$)
}
doAutoComplete = () => {
const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
// do something with `item`
}
}
javascript typescript rxjs observable behaviorsubject
javascript typescript rxjs observable behaviorsubject
edited Nov 19 at 10:16
asked Nov 19 at 9:57
awmleer
4472515
4472515
1
What is the usecase for this? You can typically use justmerge
. Usingsubscribe
is the most Rx way I think.
– martin
Nov 19 at 10:00
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look atshareReply
or a sequence ofpublishReplay
andrefCount
– Picci
Nov 19 at 10:08
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
Nov 19 at 10:16
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
Nov 19 at 13:01
|
show 1 more comment
1
What is the usecase for this? You can typically use justmerge
. Usingsubscribe
is the most Rx way I think.
– martin
Nov 19 at 10:00
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look atshareReply
or a sequence ofpublishReplay
andrefCount
– Picci
Nov 19 at 10:08
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
Nov 19 at 10:16
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
Nov 19 at 13:01
1
1
What is the usecase for this? You can typically use just
merge
. Using subscribe
is the most Rx way I think.– martin
Nov 19 at 10:00
What is the usecase for this? You can typically use just
merge
. Using subscribe
is the most Rx way I think.– martin
Nov 19 at 10:00
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look at
shareReply
or a sequence of publishReplay
and refCount
– Picci
Nov 19 at 10:08
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look at
shareReply
or a sequence of publishReplay
and refCount
– Picci
Nov 19 at 10:08
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Actually I want to write something like
b$.value
or b$.getValue()
.– awmleer
Nov 19 at 10:16
@Picci Actually I want to write something like
b$.value
or b$.getValue()
.– awmleer
Nov 19 at 10:16
1
1
With subjects,
value
and getValue
are code smells and are best avoided.– cartant
Nov 19 at 13:01
With subjects,
value
and getValue
are code smells and are best avoided.– cartant
Nov 19 at 13:01
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
1
Thanks for answering! Butbs
is still an observable andbs.value
is undefined.😥
– awmleer
Nov 22 at 12:14
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
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
1
Thanks for answering! Butbs
is still an observable andbs.value
is undefined.😥
– awmleer
Nov 22 at 12:14
add a comment |
up vote
0
down vote
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
1
Thanks for answering! Butbs
is still an observable andbs.value
is undefined.😥
– awmleer
Nov 22 at 12:14
add a comment |
up vote
0
down vote
up vote
0
down vote
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
edited Nov 22 at 12:39
awmleer
4472515
4472515
answered Nov 20 at 5:07
Luillyfe
1,72532331
1,72532331
1
Thanks for answering! Butbs
is still an observable andbs.value
is undefined.😥
– awmleer
Nov 22 at 12:14
add a comment |
1
Thanks for answering! Butbs
is still an observable andbs.value
is undefined.😥
– awmleer
Nov 22 at 12:14
1
1
Thanks for answering! But
bs
is still an observable and bs.value
is undefined.😥– awmleer
Nov 22 at 12:14
Thanks for answering! But
bs
is still an observable and bs.value
is undefined.😥– awmleer
Nov 22 at 12:14
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%2f53372138%2fhow-to-convert-an-observable-into-a-behaviorsubject%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
What is the usecase for this? You can typically use just
merge
. Usingsubscribe
is the most Rx way I think.– martin
Nov 19 at 10:00
Which is the reason you want to convert an Observable to a BehaviourSubject? Is it because you want to have access to the last value? If this is the case you can look at
shareReply
or a sequence ofpublishReplay
andrefCount
– Picci
Nov 19 at 10:08
@Picci Yes. I want to have access to the latest value. Thanks for advise! I'm going to have a look on these APIs.
– awmleer
Nov 19 at 10:11
@Picci Actually I want to write something like
b$.value
orb$.getValue()
.– awmleer
Nov 19 at 10:16
1
With subjects,
value
andgetValue
are code smells and are best avoided.– cartant
Nov 19 at 13:01