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
add a 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
yesterday
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
yesterday
@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
yesterday
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
yesterday
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
yesterday
add a 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 yesterday
asked yesterday
awmleer
4452515
4452515
1
What is the usecase for this? You can typically use justmerge
. Usingsubscribe
is the most Rx way I think.
– martin
yesterday
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
yesterday
@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
yesterday
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
yesterday
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
yesterday
add a comment |
1
What is the usecase for this? You can typically use justmerge
. Usingsubscribe
is the most Rx way I think.
– martin
yesterday
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
yesterday
@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
yesterday
@Picci Actually I want to write something likeb$.value
orb$.getValue()
.
– awmleer
yesterday
1
With subjects,value
andgetValue
are code smells and are best avoided.
– cartant
yesterday
1
1
What is the usecase for this? You can typically use just
merge
. Using subscribe
is the most Rx way I think.– martin
yesterday
What is the usecase for this? You can typically use just
merge
. Using subscribe
is the most Rx way I think.– martin
yesterday
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
yesterday
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
yesterday
@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
yesterday
@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
yesterday
@Picci Actually I want to write something like
b$.value
or b$.getValue()
.– awmleer
yesterday
@Picci Actually I want to write something like
b$.value
or b$.getValue()
.– awmleer
yesterday
1
1
With subjects,
value
and getValue
are code smells and are best avoided.– cartant
yesterday
With subjects,
value
and getValue
are code smells and are best avoided.– cartant
yesterday
add a 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$ = const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
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$ = const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
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$ = const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
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$ = 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$ = const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);
answered 21 hours ago
Luillyfe
1,67532331
1,67532331
add a comment |
add a comment |
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
yesterday
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
yesterday
@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
yesterday
@Picci Actually I want to write something like
b$.value
orb$.getValue()
.– awmleer
yesterday
1
With subjects,
value
andgetValue
are code smells and are best avoided.– cartant
yesterday