When the section list in the sidebar TOC is updated (thanks to T307251), a hook should fire so that gadgets and user scripts that modify the TOC could do their job.
A current workaround would be to await mw.loader.using('mediawiki.template.mustache'); (mediawiki.template.mustache module is requested when the wikipage.tableOfContents hook is fired), but that is unstable and unclear to developers.