Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 21:14

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Circular dependencies Angular

Discussion on Circular dependencies Angular within the Web Development forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
Circular dependencies Angular

Hi, i don't know if someone can help me with this.. but i will try, i'm making a html 5 game with a friend with socket.io, p5 and angular as frontend, i know that angular is not the best for this but i know i will have to handle the same error in node.js.

Stage.ts
Code:
// Singleton have methods like addEntityToWorld, removeEntity, mainUpdate, 

   static get Instance() {
            if (Stage._instance === undefined) {
                Stage._instance = new Stage();
            }
            return Stage._instance;
        }

function mainRender(){
  this.mainPlayer.render();
  for(let e of this.entities){
  e.render();
 }
}
Player.ts
Code:
//have methods render(),update()
...
constructor(){
   this.weapon = new Weapon();
}
...
Weapon.ts
Code:
... 
function fire(){
...
Stage.Instance.addEntityToWorld(new Bullet(...))
...
}
...
So the circular dependency is: Stage.ts -> Player.ts -> Weapon.ts -> Stage.ts

I don't want to pass stage reference's to all the classes that use stage.js, it would destroy singleton pattern...
elmarcia is offline  
Old 01/31/2019, 09:50   #2
 
elite*gold: 4
Join Date: Dec 2010
Posts: 4,733
Received Thanks: 3,870
I hope I understood your problem correctly. Your problem could be solved, if you can refactor your code and create a new service, which covers both of your use cases or if you make your Stage.ts injectable.

Take a look here:
-

Good luck!
Wurzelhüpfer is offline  
Thanks
1 User
Old 01/31/2019, 11:51   #3
 
elite*gold: 0
Join Date: Nov 2018
Posts: 34
Received Thanks: 18
I can't follow your code properly since some important parts are missing, but i assume you are not following the modularization principle. As Wurzelhüpfer suggested you should create a service (conventionally named as any.service.ts) and use the auto-injection done by Angular via the constructor:

Code:
constructor(private anyService: AnyService){}
To make this service available you have 2 options:
The prefered option (which makes services lazy-loadable aswell:
In your stage.service.ts
Code:
 \@Injectable({
   providedIn: 'root'
})
or alternatively call your service in the app.module.ts and your service keeps being a singleton.

EDIT:As i read your question once more my answer is not what you are looking for, i guess.
Can you share a simple example online, so we can reproduce it?
JustinMind is offline  
Thanks
1 User
Old 01/31/2019, 18:44   #4
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
Thumbs down

Quote:
Originally Posted by JustinMind View Post
I can't follow your code properly since some important parts are missing, but i assume you are not following the modularization principle. As Wurzelhüpfer suggested you should create a service (conventionally named as any.service.ts) and use the auto-injection done by Angular via the constructor:

Code:
constructor(private anyService: AnyService){}
To make this service available you have 2 options:
The prefered option (which makes services lazy-loadable aswell:
In your stage.service.ts
Code:
 \@Injectable({
   providedIn: 'root'
})
or alternatively call your service in the app.module.ts and your service keeps being a singleton.

EDIT:As i read your question once more my answer is not what you are looking for, i guess.
Can you share a simple example online, so we can reproduce it?
The thing is that the game was made in full ES6 javascript, so you can do something like this:
Stage.js
Code:
class Stage{

constructor(){
this._entities = new Set();
}

initGame(){
   this.addEntity(new Player(...));
}

static get Instance() {
        if (this._instance === undefined) {
            this._instance = new Stage();
        }
        return this._instance;
    }

addEntity(e){
    this._entities.add(e);
}

destroy(e){
    this._entities.delete(e);
}
Code:
class Bullet{
   ...
}

Code:
class Player{
  fire(){
  Stage.Instance.addEntity(new Bullet(...));
 }
}
So the main.js file will look something like this:
Code:
Stage.Instance.initGame();
In plain javascript Stage class is loaded, then it creates a Player Entity, and that player instance uses Stage singleton to create another Entity bullet.

So what i did is create an angular component, and a bunch of classes for every javascript file, translate them to ts and try to use singleton pattern, but the way that angular works won't let me do that.

Injecting a service in every class that uses the singleton will be my approach, wish me luck and thx for your suggestions.
elmarcia is offline  
Old 02/01/2019, 16:18   #5



 
Serraniel's Avatar
 
elite*gold: 0
The Black Market: 205/1/0
Join Date: May 2010
Posts: 6,853
Received Thanks: 5,106
Arrow General Coding -> Web Development

#moved
Serraniel is offline  
Reply


Similar Threads Similar Threads
[S] C#, Typescript oder Angular Coder [B] BTC
03/21/2018 - Coders Trading - 2 Replies
^topic Am besten wär eine Person die sich am Projekt beteiligt (RAGE-MP Server bzw GTA 5 Server). Falls ihr auch nur eine der Sachen oben könnt wäre es nett wenn ihr euch mit einem Portfolio meldet, alles wäre natürlich besser. Kann pro abgeschlossene Arbeit zahlen, jedoch wäre Arbeitszeit angemessener.
[S] Angular Coder [B] BTC/ETH etc
02/23/2018 - Coders Trading - 4 Replies
Wie ihr dem Titel entnehmen könnt suche ich einen Angular Coder. Geht um ein GTA 5 Projekt, weitere Details gerne Privat. Discord:Sun Diego#1312
[JS] Hash Location in Angular 2 ermitteln
08/25/2016 - Web Development - 1 Replies
Hallo zusammen! Ich experimentiere gerade ein wenig mit Angular 2 herum (RC 4 statt RC 5, um kompatibel mit Angular2-Meteor zu sein) und bin auf ein kleines Problem gestoßen. Ich habe eine Seite, die verschlüsselte Nachrichten aus der Datenbank ausliest. Den Schlüssel will ich natürlich nicht an den Server schicken, sondern die Nachricht clientseitig entschlüsseln. In einem früheren Prototypen, der mit Angular 1 lief, hatte ich dazu den Schlüssel in den Hash-Teil der Url gepackt - also...
Designänderung einer bestehenden Webseite (Angular.js) | Webentwicklung
01/30/2016 - Coders Trading - 0 Replies
Hallo zusammen, ich suche einen fähigen Webentwickler um meiner bestehende Webseite ein neues Design zu verleihen. Details über die Webseite möchte ich nur im Einzelnen erklären. Es sind Kenntnisse in HTML, CSS, PHP, JS, AJAX vorteilhaft und optimal wären Erfahrungen mit angular.js. Alle Funktionen der Webseite laufen und müssen nur an ein neues Design angepasst werden. Das neue Design ist schon vorhanden. Es fehlt eben nur die Anpassung und die Feinarbeit. Zudem sollen einige Inhalte...
Ubuntu: Unmet dependencies
10/11/2015 - Web Development - 2 Replies
Hab ein kleines Problem mit meinem Ubuntu 14.04 vServer: user@server:~$ sudo apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these. The following packages have unmet dependencies: modsecurity-crs : Depends: libapache2-modsecurity (>= 2.6.0) but it is not installed E: Unmet dependencies. Try using -f.



All times are GMT +1. The time now is 21:14.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.