|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { debounce } from "lodash-es"; |
18 | | -import type { SharedKvStoreContext } from "#src/kvstore/frontend.js"; |
19 | 17 | import { StatusMessage } from "#src/status.js"; |
20 | 18 | import { WatchableValue } from "#src/trackable_value.js"; |
| 19 | +import { dynamicDebounce } from "#src/util/debounce.js"; |
21 | 20 | import { RefCounted } from "#src/util/disposable.js"; |
22 | 21 | import { |
23 | 22 | bigintToStringJsonReplacer, |
24 | 23 | urlSafeParse, |
25 | 24 | verifyObject, |
26 | 25 | } from "#src/util/json.js"; |
27 | | -import type { Trackable } from "#src/util/trackable.js"; |
28 | 26 | import { getCachedJson } from "#src/util/trackable.js"; |
| 27 | +import type { Viewer } from "#src/viewer.js"; |
29 | 28 |
|
30 | 29 | /** |
31 | 30 | * @file Implements a binding between a Trackable value and the URL hash state. |
@@ -68,23 +67,47 @@ export class UrlHashBinding extends RefCounted { |
68 | 67 |
|
69 | 68 | private defaultFragment: string; |
70 | 69 |
|
| 70 | + get root() { |
| 71 | + return this.viewer.state; |
| 72 | + } |
| 73 | + |
| 74 | + get sharedKvStoreContext() { |
| 75 | + return this.viewer.dataSourceProvider.sharedKvStoreContext; |
| 76 | + } |
| 77 | + |
71 | 78 | constructor( |
72 | | - public root: Trackable, |
73 | | - public sharedKvStoreContext: SharedKvStoreContext, |
| 79 | + private viewer: Viewer, |
74 | 80 | options: UrlHashBindingOptions = {}, |
75 | 81 | ) { |
76 | 82 | super(); |
77 | | - const { updateDelayMilliseconds = 200, defaultFragment = "{}" } = options; |
| 83 | + const { defaultFragment = "{}" } = options; |
| 84 | + const { root } = this; |
78 | 85 | this.registerEventListener(window, "hashchange", () => |
79 | 86 | this.updateFromUrlHash(), |
80 | 87 | ); |
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 | + ), |
85 | 94 | ); |
86 | 95 | this.registerDisposer(root.changed.add(throttledSetUrlHash)); |
87 | | - this.registerDisposer(() => throttledSetUrlHash.cancel()); |
| 96 | + // try to update the url before the user might attempt to be copying it |
| 97 | + window.addEventListener("blur", () => { |
| 98 | + throttledSetUrlHash.flush(); |
| 99 | + }); |
| 100 | + // mouseleave works better (occurs earlier) than blur |
| 101 | + document.addEventListener("mouseleave", () => { |
| 102 | + throttledSetUrlHash.flush(); |
| 103 | + }); |
| 104 | + // update url for the select url shortcut (ctrl+l/cmd+l) |
| 105 | + // select url triggers the blur event, but for chrome, it occurs too late for the url to be updated |
| 106 | + window.addEventListener("keydown", (event) => { |
| 107 | + if (event.key === "l") { |
| 108 | + throttledSetUrlHash.flush(); |
| 109 | + } |
| 110 | + }); |
88 | 111 | this.defaultFragment = defaultFragment; |
89 | 112 | } |
90 | 113 |
|
|
0 commit comments