Skip to content

Commit b96546d

Browse files
committed
mirror: reflect all internal api usages
+ add CSP to rIC + minor changes Signed-off-by: 🕷️ <[email protected]>
1 parent 7c83d39 commit b96546d

17 files changed

+116
-48
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: clean install dev valid test test-dev prod serve_mirror
1+
.PHONY: clean install dev valid test test-dev prod mirror-dev mirror-serve
22
.DEFAULT_GOAL := dev
33
DENO_DEV := NODE_ENV=development deno run --watch
44
DENO_PROD := NODE_ENV=production deno run
@@ -43,7 +43,12 @@ prod: test
4343

4444
tree -Dis $(BUILD_DIR) *.zip | grep -E "api|zip"
4545

46-
serve_mirror:
46+
mirror-dev:
47+
@echo "🎗 reminder to stop \"make dev\""
48+
rm -rf $(BUILD_DIR)
49+
$(DENO_DEV) $(DENO_OPTIONS) $(BUILD_SCRIPT) --mirror
50+
51+
mirror-serve:
4752
@echo "🎗 reminder to switch extension off"
4853
@echo "served at: http://localhost:5555/mirror.html"
4954
python3 -m http.server 5555 -d ./public/

build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import manifest from './manifest.json' with { type: 'json' };
55

66
const nodeEnv = Deno.env.get('NODE_ENV');
77
const isProd = nodeEnv === 'production';
8+
const isMirror = Deno.args.includes('--mirror');
89
const buildOptions: BuildOptions = {
910
plugins: [
1011
esbuildSvelte({
@@ -25,6 +26,7 @@ const buildOptions: BuildOptions = {
2526
__app_name__: `"browser-api-monitor@${manifest.version}"`,
2627
__app_version__: `"${manifest.version}"`,
2728
__home_page__: `"${manifest.homepage_url}"`,
29+
__mirror__: `${isMirror}`,
2830
},
2931
bundle: true,
3032
platform: 'browser',

src/api-monitor-cs-main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let originalMetrics: TTelemetry | null;
1616
let currentMetrics: TTelemetry | null;
1717
const eachSecond = new Timer(
1818
{ type: ETimer.TIMEOUT, delay: 1e3 },
19-
() => {
19+
function apiMonitorEachSecond() {
2020
onEachSecond();
2121
eachSecond.start();
2222
},
@@ -56,7 +56,8 @@ windowListen((o) => {
5656
if (EMsg.TELEMETRY_ACKNOWLEDGED === o.msg) {
5757
tick.delay = adjustTelemetryDelay(o.timeOfCollection);
5858
originalMetrics = currentMetrics;
59-
eachSecond.isPending() && tick.start();
59+
const shouldRun = eachSecond.isPending() && !tick.isPending();
60+
shouldRun && tick.start();
6061
} else if (EMsg.CONFIG === o.msg) {
6162
applyConfig(o.config);
6263
originalMetrics = currentMetrics = null;

src/api/communication.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { TSession } from './storage/storage.session.ts';
2121

2222
let port: chrome.runtime.Port | null = null;
2323
export function portPost(payload: TMsgOptions) {
24-
if (!chrome.runtime) {
24+
if (__mirror__) {
2525
windowPost(payload);
2626
return;
2727
}
@@ -72,7 +72,9 @@ export function runtimePost(payload: TMsgOptions) {
7272
}
7373

7474
export function runtimeListen(callback: (payload: TMsgOptions) => void) {
75-
if (chrome?.runtime) {
75+
if (__mirror__) {
76+
windowListen(callback);
77+
} else {
7678
chrome.runtime.onMessage.addListener(
7779
(payload, sender: chrome.runtime.MessageSender, sendResponse) => {
7880
if (
@@ -83,8 +85,6 @@ export function runtimeListen(callback: (payload: TMsgOptions) => void) {
8385
}
8486
},
8587
);
86-
} else {
87-
windowListen(callback);
8888
}
8989
}
9090

src/api/const.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const requestAnimationFrame = /*@__PURE__*/ globalThis
2525
.requestAnimationFrame.bind(globalThis);
2626
export const cancelAnimationFrame = /*@__PURE__*/ globalThis
2727
.cancelAnimationFrame.bind(globalThis);
28+
export const requestIdleCallback = /*@__PURE__*/ globalThis
29+
.requestIdleCallback.bind(globalThis);
30+
export const cancelIdleCallback = /*@__PURE__*/ globalThis
31+
.cancelIdleCallback.bind(globalThis);
2832
export const nativeYield = /*@__PURE__*/ globalThis.scheduler.yield.bind(
2933
globalThis.scheduler,
3034
);

src/api/time.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
cancelAnimationFrame,
3+
cancelIdleCallback,
34
clearTimeout,
45
nativePostTask,
56
NOOP,
67
requestAnimationFrame,
8+
requestIdleCallback,
79
setTimeout,
810
TELEMETRY_FREQUENCY_30PS,
911
TIME_60FPS_MS,
@@ -119,6 +121,39 @@ type TTimerOptions =
119121
| TTimerIdle
120122
| TTimerTask;
121123

124+
const timerApi = __mirror__
125+
? {
126+
get setTimeout() {
127+
return globalThis.setTimeout;
128+
},
129+
get clearTimeout() {
130+
return globalThis.clearTimeout;
131+
},
132+
get requestAnimationFrame() {
133+
return globalThis.requestAnimationFrame;
134+
},
135+
get cancelAnimationFrame() {
136+
return globalThis.cancelAnimationFrame;
137+
},
138+
get requestIdleCallback() {
139+
return globalThis.requestIdleCallback;
140+
},
141+
get cancelIdleCallback() {
142+
return globalThis.cancelIdleCallback;
143+
},
144+
get postTask() {
145+
return globalThis.scheduler.postTask;
146+
},
147+
}
148+
: {
149+
setTimeout,
150+
clearTimeout,
151+
requestAnimationFrame,
152+
cancelAnimationFrame,
153+
requestIdleCallback,
154+
cancelIdleCallback,
155+
postTask: nativePostTask,
156+
} as const;
122157
/**
123158
* A unification of ways to delay a callback execution
124159
* in javascript event-loop
@@ -158,27 +193,28 @@ export class Timer {
158193
if (
159194
this.#options.type === ETimer.TIMEOUT
160195
) {
161-
this.#handler = setTimeout(() => {
196+
this.#handler = timerApi.setTimeout(() => {
162197
this.#handler = 0;
163198
this.trigger(...args);
164199
}, this.delay);
165200
} else if (
166201
this.#options.type === ETimer.ANIMATION
167202
) {
168-
this.#handler = requestAnimationFrame((...argsAF) => {
203+
this.#handler = timerApi.requestAnimationFrame((...argsAF) => {
169204
this.#handler = 0;
170205
this.trigger(...[...args, ...argsAF]);
171206
});
172207
} else if (this.#options.type === ETimer.IDLE) {
173-
this.#handler = requestIdleCallback((...argsIC) => {
208+
this.#handler = timerApi.requestIdleCallback((...argsIC) => {
174209
this.#handler = 0;
175210
this.trigger(...[...args, ...argsIC]);
176211
}, { timeout: this.delay });
177212
} else if (this.#options.type === ETimer.TASK) {
178213
this.#abortController = new AbortController();
179-
nativePostTask(() => {
180-
this.#abortController = null;
214+
timerApi.postTask(() => {
181215
this.trigger(...args);
216+
// nullifying AFTER the trigger to allow use-case when aborting from the callback
217+
this.#abortController = null;
182218
}, {
183219
delay: this.delay,
184220
signal: this.#abortController.signal,
@@ -203,13 +239,13 @@ export class Timer {
203239

204240
stop() {
205241
if (this.#options.type === ETimer.TIMEOUT) {
206-
this.#handler && clearTimeout(this.#handler);
242+
this.#handler && timerApi.clearTimeout(this.#handler);
207243
this.#handler = 0;
208244
} else if (this.#options.type === ETimer.ANIMATION) {
209-
this.#handler && cancelAnimationFrame(this.#handler);
245+
this.#handler && timerApi.cancelAnimationFrame(this.#handler);
210246
this.#handler = 0;
211247
} else if (this.#options.type === ETimer.IDLE) {
212-
this.#handler && cancelIdleCallback(this.#handler);
248+
this.#handler && timerApi.cancelIdleCallback(this.#handler);
213249
this.#handler = 0;
214250
} else if (this.#options.type === ETimer.TASK) {
215251
this.#abortController && this.#abortController.abort();

src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ declare global {
55
let __app_name__: string;
66
let __app_version__: string;
77
let __home_page__: string;
8+
let __mirror__: boolean;
89
}

src/view/ConnectionAlert.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { ETimer, Timer } from '../api/time.ts';
44
import Alert from './shared/Alert.svelte';
55
import {
6+
CONTEXT_ERROR,
67
INJECTION_ALERT_TIMEOUT,
78
UPDATE_SENSOR_INTERVAL,
89
} from './shared/const.ts';
@@ -36,7 +37,7 @@
3637
});
3738
3839
onMount(() => {
39-
if (chrome.runtime) {
40+
if (!__mirror__) {
4041
pingContentScript();
4142
extensionUpdateSensor.start();
4243
}
@@ -60,7 +61,7 @@
6061
*/
6162
function whenUpdateDetected(callback: () => void) {
6263
loadLocalStorage().catch((e: Error) => {
63-
if (!e || e.message !== 'Extension context invalidated.') {
64+
if (!e || e.message !== CONTEXT_ERROR) {
6465
return;
6566
}
6667
try {

src/view/menu/SummaryBarItem.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
if (stopAnimate.isPending()) {
5555
stopAnimate.stop();
5656
}
57-
el.classList.add(AFTER_SCROLL_ANIMATION_CLASSNAME);
58-
stopAnimate.start(el);
57+
void requestAnimationFrame(() => {
58+
el.classList.add(AFTER_SCROLL_ANIMATION_CLASSNAME);
59+
stopAnimate.start(el);
60+
});
5961
}
6062
}
6163
</script>

src/view/panels/idle/IdleCallbackRequestHistory.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
eventChangeSorting={onChangeSort}
121121
><span class="icon -facts"></span></ColumnSortable>
122122
</th>
123+
<th class="ta-c" title="Calls per second">CPS</th>
123124
<th class="ta-c">
124125
<ColumnSortable
125126
field="calls"
@@ -142,7 +143,7 @@
142143
currentField={sortRequestIdleCallback.field}
143144
currentFieldOrder={sortRequestIdleCallback.order}
144145
eventChangeSorting={onChangeSort}
145-
>Delay</ColumnSortable>
146+
>Timeout</ColumnSortable>
146147
</th>
147148
<th class="ta-c">
148149
<ColumnSortable
@@ -171,6 +172,7 @@
171172
<td class="ta-c">
172173
<CellFacts facts={metric.facts} factsMap={RicFacts} />
173174
</td>
175+
<td class="ta-c">{metric.cps || undefined}</td>
174176
<td class="ta-c">
175177
<CellCancelable
176178
calls={metric.calls}

0 commit comments

Comments
 (0)