Skip to content

Commit 6361301

Browse files
committed
update debug immediately
1 parent 9dea0ca commit 6361301

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/ui/url_hash_binding.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export class UrlHashBinding extends RefCounted {
7575
return this.viewer.dataSourceProvider.sharedKvStoreContext;
7676
}
7777

78-
7978
urlHashRateLimit = new WatchableValue(200);
8079

8180
constructor(
@@ -113,6 +112,31 @@ export class UrlHashBinding extends RefCounted {
113112
}
114113
});
115114
this.defaultFragment = defaultFragment;
115+
this.viewer.urlHashRateLimit.changed.add(this.updateRateLimit);
116+
this.urlHashRateLimit.changed.add(this.updateRateLimit);
117+
}
118+
119+
updateRateLimit = () => {
120+
const debugEl =
121+
document.body.querySelector("#debug-url-rate") ??
122+
document.body.appendChild(document.createElement("div"));
123+
debugEl.id = "debug-url-rate";
124+
125+
const userFunc = this.viewer.urlHashRateLimit.value;
126+
const safeFunc = /^[xelogmaxin ,*/\+\-\d\(\)]*$/;
127+
if (safeFunc.test(userFunc)) {
128+
const translatedFunc = userFunc
129+
.replaceAll("log", "Math.log")
130+
.replaceAll("min", "Math.min")
131+
.replaceAll("max", "Math.max")
132+
.replaceAll("e", "Math.E");
133+
this.urlHashRateLimit.value = eval(
134+
`'use strict';(x) => ${translatedFunc}`,
135+
)(this.prevStateString?.length ?? 0);
136+
debugEl.textContent = `URL rate: ${(this.urlHashRateLimit.value / 1000).toFixed(1)} s (state size (x)=${this.prevStateString?.length ?? 0}, f=${this.viewer.urlHashRateLimit.value})`;
137+
} else {
138+
debugEl.textContent = `URL rate: INVALID FUNCTION (${userFunc})`;
139+
}
116140
}
117141

118142
/**
@@ -128,26 +152,7 @@ export class UrlHashBinding extends RefCounted {
128152
);
129153
if (stateString !== this.prevStateString) {
130154
this.prevStateString = stateString;
131-
const userFunc = this.viewer.urlHashRateLimit.value;
132-
const safeFunc = /^[xelogmaxin ,*/\+\-\d\(\)]*$/;
133-
if (safeFunc.test(userFunc)) {
134-
const translatedFunc = userFunc
135-
.replaceAll("log", "Math.log")
136-
.replaceAll("min", "Math.min")
137-
.replaceAll("max", "Math.max")
138-
.replaceAll("e", "Math.E");
139-
this.urlHashRateLimit.value = eval(
140-
`'use strict';(x) => ${translatedFunc}`,
141-
)(stateString.length);
142-
console.log(
143-
"new rate",
144-
stateString.length,
145-
this.urlHashRateLimit.value,
146-
);
147-
const debugEl = document.body.querySelector("#debug-url-rate") ?? document.body.appendChild(document.createElement("div"));
148-
debugEl.id = "debug-url-rate";
149-
debugEl.textContent = `URL rate: ${(this.urlHashRateLimit.value / 1000).toFixed(1)} s (state size (x)=${stateString.length}, f=${userFunc})`;
150-
}
155+
this.updateRateLimit();
151156
if (decodeURIComponent(stateString) === "{}") {
152157
history.replaceState(null, "", "#");
153158
} else {

0 commit comments

Comments
 (0)