1414 * limitations under the License.
1515 */
1616
17- import { debounce } from "lodash-es" ;
18- import type { SharedKvStoreContext } from "#src/kvstore/frontend.js" ;
1917import { StatusMessage } from "#src/status.js" ;
2018import { WatchableValue } from "#src/trackable_value.js" ;
19+ import { dynamicDebounce } from "#src/util/debounce.js" ;
2120import { RefCounted } from "#src/util/disposable.js" ;
2221import {
2322 bigintToStringJsonReplacer ,
2423 urlSafeParse ,
2524 verifyObject ,
2625} from "#src/util/json.js" ;
27- import type { Trackable } from "#src/util/trackable.js" ;
2826import { getCachedJson } from "#src/util/trackable.js" ;
27+ import type { Viewer } from "#src/viewer.js" ;
2928
3029/**
3130 * @file Implements a binding between a Trackable value and the URL hash state.
@@ -68,23 +67,38 @@ export class UrlHashBinding extends RefCounted {
6867
6968 private defaultFragment : string ;
7069
70+ get root ( ) {
71+ return this . viewer . state ;
72+ }
73+
74+ get sharedKvStoreContext ( ) {
75+ return this . viewer . dataSourceProvider . sharedKvStoreContext ;
76+ }
77+
7178 constructor (
72- public root : Trackable ,
73- public sharedKvStoreContext : SharedKvStoreContext ,
79+ private viewer : Viewer ,
7480 options : UrlHashBindingOptions = { } ,
7581 ) {
7682 super ( ) ;
77- const { updateDelayMilliseconds = 200 , defaultFragment = "{}" } = options ;
83+ const { defaultFragment = "{}" } = options ;
84+ const { root } = this ;
7885 this . registerEventListener ( window , "hashchange" , ( ) =>
7986 this . updateFromUrlHash ( ) ,
8087 ) ;
81- const throttledSetUrlHash = debounce (
88+ const throttledSetUrlHash = this . registerDisposer ( dynamicDebounce (
8289 ( ) => this . setUrlHash ( ) ,
83- updateDelayMilliseconds ,
84- { maxWait : updateDelayMilliseconds * 2 } ,
85- ) ;
90+ viewer . urlHashRateLimit ,
91+ ( wait ) => ( { maxWait : wait * 2 } ) ,
92+ ) ) ;
8693 this . registerDisposer ( root . changed . add ( throttledSetUrlHash ) ) ;
87- this . registerDisposer ( ( ) => throttledSetUrlHash . cancel ( ) ) ;
94+ window . addEventListener ( "blur" , ( ) => {
95+ throttledSetUrlHash . flush ( ) ;
96+ } ) ;
97+ // mouseleave works better than blur
98+ // both events don't seem to work with shortcut to copy url (ctrl+l/cmd+l)
99+ document . addEventListener ( 'mouseleave' , ( ) => {
100+ throttledSetUrlHash . flush ( ) ;
101+ } ) ;
88102 this . defaultFragment = defaultFragment ;
89103 }
90104
@@ -101,6 +115,7 @@ export class UrlHashBinding extends RefCounted {
101115 ) ;
102116 if ( stateString !== this . prevStateString ) {
103117 this . prevStateString = stateString ;
118+ console . log ( "we will replace the state" , stateString ) ;
104119 if ( decodeURIComponent ( stateString ) === "{}" ) {
105120 history . replaceState ( null , "" , "#" ) ;
106121 } else {
0 commit comments