valor-software / ng2-dragula

Simple drag and drop with dragula
http://valor-software.com/ng2-dragula/
MIT License
1.91k stars 430 forks source link

[bug report] Dragula service is provided multiple times instead of as a singleton #1082

Open RGunning opened 1 year ago

RGunning commented 1 year ago

REQUIRED: Before filing a bug report

Change each [ ] to [x] when you have done it.

Describe the bug (required)

With v3 of this library, the DragulaService is provided multiple times instead of just when importing DragulaModule.forRoot().

  1. It is now providedIn: root https://github.com/valor-software/ng2-dragula/pull/1044/files#diff-845f2a393c0ade2f75831cff246267c8b1c96691d81d2d710c47f20eb14ef6d9R31
  2. provided on import of the DragulaModule imports: [DragulaModule] https://github.com/valor-software/ng2-dragula/pull/1044/files#diff-77b8912947381f081ed7f9efbc418716a1dcdfac9c5ebc2d5e6539cad507878fR8
  3. provided on calling DragulaModule.forRoot() imports: [DragulaModule.forRoot()]

As such it is easy to get inject the wrong instance of the DragulaService leading to incorrect behaviour. In my application I have a service (providedIn: Root) that injects the DragulaService. However, because it is providedIn root, since the v3 update it is now getting a different instance of the DragulaService to that injected into the DragulaDirective. As such calls of createGroup made in my root service are being ignored.

*** Expected: importing DragulaModule does not provide an instance of the DragulaService and instead the DragulaService is either provided by providedIn: root or DragulaModule.forRoot(). My preferred solution would be to remove the forRoot and rely solely on providedIn: root, but this would be a breaking change.

Versions (required)

Please state which versions of the following packages you have installed:

dino-laktasic commented 4 months ago

I can confirm that I run into the same issue and it's definitely BUG. You should either provide DragulaService in root and remove it from providers list in DragulaModule and remove forRoot function or keep forRoot function and remove it from providers list in DragulaModule and remove providedIn from DragulaService service decorator. I tried fixes locally and they were working as expected.

kunajs commented 3 months ago

Hi, I prepared an example which is probably related to the described problem: https://stackblitz.com/edit/angular-18-ng2-dragula-511

I am dynamically creating the inner components which should use the same dragulaService, but it seems that it is different instance. Check the console where is logged for each dynamic component and for root component:

As you can see, each dynamic component has only containers inside themselves, so it is not connected to each other and to root container.

Compare it with old version where I do the same dynamic loading (just in a bit different way because of angular 9) and in console all the containers are connected. And it is because there is only 1 instance od dragulaService accross the whole project. https://stackblitz.com/edit/angular-9-ng2-dragula-211

Please, check it. I think the solutions suggested above should resolve this issue.

kunajs commented 2 months ago

Are you going to repair it soon?