OK, so I made use of AI and it did exactly what I would do - though it created in a page workflow without a trigger. So I can solve my problem by using this workflow on page_load and when ever the collection changes from within the page I can re-run the workflow. Unless one of you scream: Why don’t you use the smart “on collection loaded / changed” event (which I can’t find)
You can use a formula, formulas are reactive and will react to stuff like collection change with re-computing the whole thing. You pass in a collection, do your counting and that’s it, that’s the magic you’re probably looking for. The reactivity is very powerful concept, but it also makes it easy to shoot yourself in a foot.
TLDR: Just move your JavaScript function into a formula.