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,45 @@ 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 (
82- ( ) => this . setUrlHash ( ) ,
83- updateDelayMilliseconds ,
84- { maxWait : updateDelayMilliseconds * 2 } ,
88+ const throttledSetUrlHash = this . registerDisposer (
89+ dynamicDebounce (
90+ ( ) => this . setUrlHash ( ) ,
91+ viewer . urlHashRateLimit ,
92+ ( wait ) => ( { maxWait : wait * 2 } ) ,
93+ ) ,
8594 ) ;
8695 this . registerDisposer ( root . changed . add ( throttledSetUrlHash ) ) ;
87- this . registerDisposer ( ( ) => throttledSetUrlHash . cancel ( ) ) ;
96+ window . addEventListener ( "blur" , ( ) => {
97+ throttledSetUrlHash . flush ( ) ;
98+ } ) ;
99+ // mouseleave works better than blur
100+ document . addEventListener ( "mouseleave" , ( ) => {
101+ throttledSetUrlHash . flush ( ) ;
102+ } ) ;
103+ // update shortcut to copy url (ctrl+l/cmd+l)
104+ window . addEventListener ( "keydown" , ( event ) => {
105+ if ( event . key === "l" ) {
106+ throttledSetUrlHash . flush ( ) ;
107+ }
108+ } ) ;
88109 this . defaultFragment = defaultFragment ;
89110 }
90111
@@ -101,6 +122,7 @@ export class UrlHashBinding extends RefCounted {
101122 ) ;
102123 if ( stateString !== this . prevStateString ) {
103124 this . prevStateString = stateString ;
125+ console . log ( "we will replace the state" , stateString ) ;
104126 if ( decodeURIComponent ( stateString ) === "{}" ) {
105127 history . replaceState ( null , "" , "#" ) ;
106128 } else {
0 commit comments