using Map in BehaviorSubject





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















           @Injectable()
export class GlobalService {

private variables: Map<number, Variable>;

private observableVariables: BehaviorSubject<Map<number,
Variable>>;

constructor() {
this.variables = new Map<number, Variable>();
this.observableVariables = new BehaviorSubject(new Map<number,
Variable>());
}

addVariable(index: number, variable:Variable) {
this.variables.set(index, variable);
}

public getVariable(index:number):Variable {
return this.variables.get(index);
}

public updateVariableType(index:number, type:VariableType){
let variable = this.variables.get(index);
variable.updateType(type);
}
public updateVariableName(index:number, name:String){
let variable = this.variables.get(index);
variable.updateName(type);
}
public updateAllComponents(){
return this.observableVariables.next(this.variables);
}
}


I am using a Global service to which the components subscribe. I am using a map data structure to save the variables with the respective index. When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one? The senario in my case is the new component should get all the data from the Map not just the last added one. And how to broadcast only the variables that has both name and type populated through updateVariableType and updateVariableName?










share|improve this question

























  • I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

    – fransyozef
    Jan 4 at 16:18











  • "When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

    – fransyozef
    Jan 4 at 16:19


















0















           @Injectable()
export class GlobalService {

private variables: Map<number, Variable>;

private observableVariables: BehaviorSubject<Map<number,
Variable>>;

constructor() {
this.variables = new Map<number, Variable>();
this.observableVariables = new BehaviorSubject(new Map<number,
Variable>());
}

addVariable(index: number, variable:Variable) {
this.variables.set(index, variable);
}

public getVariable(index:number):Variable {
return this.variables.get(index);
}

public updateVariableType(index:number, type:VariableType){
let variable = this.variables.get(index);
variable.updateType(type);
}
public updateVariableName(index:number, name:String){
let variable = this.variables.get(index);
variable.updateName(type);
}
public updateAllComponents(){
return this.observableVariables.next(this.variables);
}
}


I am using a Global service to which the components subscribe. I am using a map data structure to save the variables with the respective index. When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one? The senario in my case is the new component should get all the data from the Map not just the last added one. And how to broadcast only the variables that has both name and type populated through updateVariableType and updateVariableName?










share|improve this question

























  • I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

    – fransyozef
    Jan 4 at 16:18











  • "When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

    – fransyozef
    Jan 4 at 16:19














0












0








0








           @Injectable()
export class GlobalService {

private variables: Map<number, Variable>;

private observableVariables: BehaviorSubject<Map<number,
Variable>>;

constructor() {
this.variables = new Map<number, Variable>();
this.observableVariables = new BehaviorSubject(new Map<number,
Variable>());
}

addVariable(index: number, variable:Variable) {
this.variables.set(index, variable);
}

public getVariable(index:number):Variable {
return this.variables.get(index);
}

public updateVariableType(index:number, type:VariableType){
let variable = this.variables.get(index);
variable.updateType(type);
}
public updateVariableName(index:number, name:String){
let variable = this.variables.get(index);
variable.updateName(type);
}
public updateAllComponents(){
return this.observableVariables.next(this.variables);
}
}


I am using a Global service to which the components subscribe. I am using a map data structure to save the variables with the respective index. When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one? The senario in my case is the new component should get all the data from the Map not just the last added one. And how to broadcast only the variables that has both name and type populated through updateVariableType and updateVariableName?










share|improve this question
















           @Injectable()
export class GlobalService {

private variables: Map<number, Variable>;

private observableVariables: BehaviorSubject<Map<number,
Variable>>;

constructor() {
this.variables = new Map<number, Variable>();
this.observableVariables = new BehaviorSubject(new Map<number,
Variable>());
}

addVariable(index: number, variable:Variable) {
this.variables.set(index, variable);
}

public getVariable(index:number):Variable {
return this.variables.get(index);
}

public updateVariableType(index:number, type:VariableType){
let variable = this.variables.get(index);
variable.updateType(type);
}
public updateVariableName(index:number, name:String){
let variable = this.variables.get(index);
variable.updateName(type);
}
public updateAllComponents(){
return this.observableVariables.next(this.variables);
}
}


I am using a Global service to which the components subscribe. I am using a map data structure to save the variables with the respective index. When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one? The senario in my case is the new component should get all the data from the Map not just the last added one. And how to broadcast only the variables that has both name and type populated through updateVariableType and updateVariableName?







angular angular5 angular6 angular2-services angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 14:31







Teja

















asked Jan 4 at 13:20









TejaTeja

104




104













  • I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

    – fransyozef
    Jan 4 at 16:18











  • "When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

    – fransyozef
    Jan 4 at 16:19



















  • I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

    – fransyozef
    Jan 4 at 16:18











  • "When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

    – fransyozef
    Jan 4 at 16:19

















I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

– fransyozef
Jan 4 at 16:18





I'm not familiar with the Map structure, or is that just your custom struct? What you also could do is use the BehaviousSubject as a store (so also for add/get/update) and then notify all subscribers.

– fransyozef
Jan 4 at 16:18













"When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

– fransyozef
Jan 4 at 16:19





"When a new component subscribes to this service will the new component gets all the data in the Map or only the last added one?" --> have you tried it? Maybe you can setup a plunkr code with a minimal example

– fransyozef
Jan 4 at 16:19












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039734%2fusing-map-in-behaviorsubject%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039734%2fusing-map-in-behaviorsubject%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS