1- export function AttachVueSettledEvents ( TargetEl : HTMLElement , Options : { QuietMs ?: number ; EventName ?: string ; ChangeEventName ?: string } = { } ) {
1+ export function AttachVueSettledEvents ( TargetEl : HTMLElement , Options : { QuietMs ?: number ; EventName ?: string ; ChangeEventName ?: string ; UrlChange ?: string } = { } ) {
22 const QuietMs = Options . QuietMs ?? 120
33 const EventName = Options . EventName ?? 'vue:settled'
44 const ChangeEventName = Options . ChangeEventName ?? 'vue:dom-changed'
5+ const UrlChangeEventName = Options . UrlChange ?? 'vue:url-changed'
56
67 if ( ! ( TargetEl instanceof HTMLElement ) ) {
78 throw new TypeError ( 'TargetEl must be an HTMLElement' )
@@ -11,6 +12,7 @@ export function AttachVueSettledEvents(TargetEl: HTMLElement, Options: { QuietMs
1112 let Seq = 0
1213 let Destroyed = false
1314 let LastMutationAt = performance . now ( )
15+ let URLHistory : URL = new URL ( location . href )
1416
1517 const EmitChange = ( Mutations : MutationRecord [ ] ) => {
1618 TargetEl . dispatchEvent (
@@ -47,6 +49,22 @@ export function AttachVueSettledEvents(TargetEl: HTMLElement, Options: { QuietMs
4749 } )
4850 }
4951
52+ const EmitUrlChange = ( ) => {
53+ const NewURL = new URL ( location . href )
54+ if ( NewURL . href !== URLHistory . href ) {
55+ URLHistory = NewURL
56+ TargetEl . dispatchEvent (
57+ new CustomEvent ( UrlChangeEventName , {
58+ detail : {
59+ Seq,
60+ At : performance . now ( ) ,
61+ URL : NewURL ,
62+ } ,
63+ } ) ,
64+ )
65+ }
66+ }
67+
5068 const ArmSettledTimer = ( ) => {
5169 clearTimeout ( Timer )
5270 Timer = setTimeout ( EmitSettled , QuietMs )
@@ -60,6 +78,7 @@ export function AttachVueSettledEvents(TargetEl: HTMLElement, Options: { QuietMs
6078 if ( Mutations . length >= 25 ) {
6179 ArmSettledTimer ( )
6280 }
81+ EmitUrlChange ( )
6382 } )
6483
6584 Observer . observe ( TargetEl , {
0 commit comments