Skip to content

fix(ktoaster): prevent duplicate containers via a shared pool#3041

Closed
johncowen wants to merge 1 commit intomainfrom
fix/singleton-tm-container
Closed

fix(ktoaster): prevent duplicate containers via a shared pool#3041
johncowen wants to merge 1 commit intomainfrom
fix/singleton-tm-container

Conversation

@johncowen
Copy link
Contributor

@johncowen johncowen commented Jan 6, 2026

#3017 has been up in draft for a little while which was already a replacement for #2980 which was reverted in #3015 and as we've been seeing this for a while over in kuma-land I've also seen mention of this elsewhere.

So as a potential alternative, I decided to put a draft up using our SharedPool which aims to enforce the DOM container as a shared singleton.

The changes to ToastManager itself are very minor and it all just relies on the SharedPool functionality instead, making it very easy to grok whats going on in the ToastManager without having to worry about the implementation. Just acquire things and release them when you are done, once the last release happens the "thing" gets destroyed.


I believe the constraints here were to not change the new ToastManager() API even though we essentially require a singleton. This PR keeps the multiple ToastManagers but implements the singleton inside, thus maintaining the API.

SharedPool itself was built many moons ago for our own application and I've just copy/pasta'ed it over here as is. I'd probably fiddle with it a bit more now if I had more chance, but it's easier to just drop in what we have over in kuma in here also.

Its basically a MultiMap with lifecycle hooks so you can tell it how to to create, acquire, release and finally destroy things when they are no longer in use. In our application we have many many singletons using this, but here we just need the one, keyed by the previous id of kongponents-toaster-container.

The DOM id itself I now randomise, via Date, so you its harder to fiddle with from the outside. I don't think this impacts SSR judging by the code here previous to the PR, but then again the "fiddle prevention" is probably not super necessary in the real-world.

I added something that you can play with in the sandbox: I add a bunch of ToastManager instances all at once, and then slowly remove all but the one we are using and you can see that having multiple managers doesn't produce multiple root containers. I then added a button to remove the ToastManager we are actually using. If you push this button either at the end or before others are removed, everything is cleaned up properly once all the ToastManagers have been destroyed (you'll notice the DOM element is removed)

I might have totally missed something here, so let me know if so. Also more than happy to close this if it's not the desired approach, just lemme know!

There's probably a few more things to say here, but if its not needed then its not worth me going over, but do let me know if anyone has any more questions.

@netlify
Copy link

netlify bot commented Jan 6, 2026

Deploy Preview for kongponents-sandbox ready!

Name Link
🔨 Latest commit 71c0449
🔍 Latest deploy log https://app.netlify.com/projects/kongponents-sandbox/deploys/695d61c446c1ad0008a9ee4d
😎 Deploy Preview https://deploy-preview-3041--kongponents-sandbox.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kongponents-bot
Copy link
Collaborator

🔴 PR audit failed. 🔴

🔥 No test coverage detected.

This PR does not include any test coverage changes, but it modifies source code. Please add appropriate tests to cover the changes made in this PR.

If you believe this is a false positive or if there are valid reasons for not including test coverage changes, please request an exemption by adding the test-coverage-exempt label to the PR and ensure it is approved by one of those managers jillztom, nateslo, erichsend, lahabana, hangrao, ryanmoore, elen4, DaniellaFreese, mfollett, ValeryG.

@netlify
Copy link

netlify bot commented Jan 6, 2026

Deploy Preview for kongponents ready!

Name Link
🔨 Latest commit 71c0449
🔍 Latest deploy log https://app.netlify.com/projects/kongponents/deploys/695d61c466b11000081dbacc
😎 Deploy Preview https://deploy-preview-3041--kongponents.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@johncowen
Copy link
Contributor Author

PS. forgot to say, the pool itself is a singleton only if everything is using the same version of kongponents, which I believe is the case

@kongponents-bot
Copy link
Collaborator

Preview package from this PR in consuming application

In consuming application project install preview version of kongponents generated by this PR:

@kong/kongponents@pr-3041

@johncowen
Copy link
Contributor Author

I made another alternative/option here #3045 also which personally I prefer, but happy with either.

@johncowen
Copy link
Contributor Author

Taking out of draft so its clear I'd be fine merging this - also good to close if not desired

@johncowen johncowen marked this pull request as ready for review January 7, 2026 13:05
@johncowen
Copy link
Contributor Author

Closing see #3045 (comment)

@johncowen johncowen closed this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants