Skip to content

Commit 6d581dd

Browse files
committed
review communication loop
1 parent 624954b commit 6d581dd

File tree

9 files changed

+48
-36
lines changed

9 files changed

+48
-36
lines changed

src/api-monitor-cs-main.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { EMsg, windowListen, windowPost } from './api/communication.ts';
22
import { IS_DEV } from './api/env.ts';
3+
import { TELEMETRY_FREQUENCY_1PS } from './api/const.ts';
4+
import { adjustTelemetryDelay, Timer } from './api/time.ts';
35
import {
4-
TELEMETRY_FREQUENCY_1PS,
5-
TELEMETRY_FREQUENCY_30PS,
6-
} from './api/const.ts';
7-
import { Timer } from './api/time.ts';
8-
import {
9-
wrapperOnEachSecond as onEachSecond,
6+
onEachSecond,
107
setSettings,
118
cleanHistory,
129
collectMetrics,
1310
runMediaCommand,
11+
runTimerCommand,
1412
} from './wrapper/Wrapper.ts';
15-
import { ETimerType } from './wrapper/TimerWrapper.ts';
1613

1714
const eachSecond = new Timer({ delay: 1e3, repetitive: true }, onEachSecond);
1815
const tick = new Timer(
@@ -22,18 +19,15 @@ const tick = new Timer(
2219

2320
windowPost({
2421
msg: EMsg.TELEMETRY,
25-
collectingStartTime: now,
22+
timeOfCollection: now,
2623
telemetry: collectMetrics(),
2724
});
2825
}
2926
);
3027

3128
windowListen((o) => {
3229
if (o.msg === EMsg.TELEMETRY_ACKNOWLEDGED) {
33-
// adaptive update-frequency
34-
const ackTrafficDuration = Date.now() - o.timeSent;
35-
const newDelay = (o.trafficDuration + ackTrafficDuration) * 3;
36-
tick.delay = Math.max(TELEMETRY_FREQUENCY_30PS, newDelay);
30+
tick.delay = adjustTelemetryDelay(o.timeOfCollection);
3731
} else if (
3832
o.msg === EMsg.SETTINGS &&
3933
o.settings &&
@@ -49,12 +43,8 @@ windowListen((o) => {
4943
} else if (o.msg === EMsg.RESET_WRAPPER_HISTORY) {
5044
cleanHistory();
5145
!tick.isPending && tick.trigger();
52-
} else if (o.msg === EMsg.CLEAR_TIMER_HANDLER) {
53-
if (o.type === ETimerType.TIMEOUT) {
54-
window.clearTimeout(o.handler);
55-
} else {
56-
window.clearInterval(o.handler);
57-
}
46+
} else if (o.msg === EMsg.TIMER_COMMAND) {
47+
runTimerCommand(o.type, o.handler);
5848
} else if (o.msg === EMsg.MEDIA_COMMAND) {
5949
runMediaCommand(o.mediaId, o.cmd, o.property);
6050
}

src/api/communication.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export enum EMsg {
105105
TELEMETRY_ACKNOWLEDGED,
106106
MEDIA_COMMAND,
107107
RESET_WRAPPER_HISTORY,
108-
CLEAR_TIMER_HANDLER,
108+
TIMER_COMMAND,
109109
}
110110

111111
export interface TMsgStartObserve {
@@ -117,8 +117,8 @@ export interface TMsgStopObserve {
117117
export interface TMsgResetHistory {
118118
msg: EMsg.RESET_WRAPPER_HISTORY;
119119
}
120-
export interface TMsgClearHandler {
121-
msg: EMsg.CLEAR_TIMER_HANDLER;
120+
export interface TMsgTimerCommand {
121+
msg: EMsg.TIMER_COMMAND;
122122
type: ETimerType;
123123
handler: number;
124124
}
@@ -127,13 +127,12 @@ export interface TMsgLoaded {
127127
}
128128
export interface TMsgTelemetry {
129129
msg: EMsg.TELEMETRY;
130-
collectingStartTime: number;
130+
timeOfCollection: number;
131131
telemetry: TTelemetry;
132132
}
133133
export interface TMsgTelemetryAcknowledged {
134134
msg: EMsg.TELEMETRY_ACKNOWLEDGED;
135-
trafficDuration: number;
136-
timeSent: number;
135+
timeOfCollection: number;
137136
}
138137
export interface TMsgSettings {
139138
msg: EMsg.SETTINGS;
@@ -152,6 +151,6 @@ export type TMsgOptions =
152151
| TMsgStopObserve
153152
| TMsgLoaded
154153
| TMsgResetHistory
155-
| TMsgClearHandler
154+
| TMsgTimerCommand
156155
| TMsgSettings
157156
| TMsgMediaCommand;

src/api/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const ERRORS_IGNORED = [
44
];
55
export const TELEMETRY_FREQUENCY_30PS = 33.3333333333; // ms
66
export const TELEMETRY_FREQUENCY_1PS = 1000; // ms
7-
export const MAX_TRAFFIC_DURATION_BEFORE_AUTOPAUSE = 2e3; // ms
7+
export const MAX_SENDING_TIME_LAG = 2e3; // ms
88
export const FRAME_1of60 = 0.0166666666667; // ms
99
export const VARIABLE_ANIMATION_THROTTLE = 3500; // eye blinking average frequency
1010
export const CALLED_ABORTED_TOOLTIP = '<called>-<aborted>/<abort-locations>';

src/api/time.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
clearTimeout,
44
requestAnimationFrame,
55
cancelAnimationFrame,
6+
TELEMETRY_FREQUENCY_30PS,
67
} from './const.ts';
78

89
export function callingOnce<T extends (...args: any[]) => any>(
@@ -223,3 +224,12 @@ export function trim2microsecond(ms: any) {
223224
export function msToHms(delay: number | unknown): string | undefined {
224225
return delay && Number(delay) > 10e3 ? Stopper.toString(delay) : undefined;
225226
}
227+
228+
const TICK_TIME_LAG_SCALAR = 3;
229+
230+
export function adjustTelemetryDelay(timeOfCollection: number) {
231+
const timeLag = Date.now() - timeOfCollection;
232+
const newDelay = timeLag * TICK_TIME_LAG_SCALAR;
233+
234+
return Math.max(TELEMETRY_FREQUENCY_30PS, newDelay);
235+
}

src/view/App.svelte

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { runtimeListen, portPost, EMsg } from '../api/communication.ts';
33
import { IS_DEV } from '../api/env.ts';
4-
import { MAX_TRAFFIC_DURATION_BEFORE_AUTOPAUSE } from '../api/const.ts';
4+
import { MAX_SENDING_TIME_LAG } from '../api/const.ts';
55
import { getSettings, setSettings } from '../api/settings.ts';
66
import type { TTelemetry } from '../wrapper/Wrapper.ts';
77
import { onMount } from 'svelte';
@@ -35,19 +35,18 @@
3535
} else if (o.msg === EMsg.TELEMETRY) {
3636
telemetry = o.telemetry;
3737
38-
const now = Date.now();
39-
const trafficDuration = now - o.collectingStartTime;
38+
const shouldPause =
39+
Date.now() - o.timeOfCollection > MAX_SENDING_TIME_LAG;
4040
41-
if (trafficDuration > MAX_TRAFFIC_DURATION_BEFORE_AUTOPAUSE) {
41+
if (shouldPause) {
4242
if (!paused) {
4343
onTogglePause();
4444
autopauseAlertEl?.show();
4545
}
4646
} else {
4747
portPost({
4848
msg: EMsg.TELEMETRY_ACKNOWLEDGED,
49-
trafficDuration,
50-
timeSent: now,
49+
timeOfCollection: o.timeOfCollection,
5150
});
5251
}
5352

src/view/components/ActiveTimers.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
function onRemoveHandler(metric: TOnlineTimerMetrics) {
1515
portPost({
16-
msg: EMsg.CLEAR_TIMER_HANDLER,
16+
msg: EMsg.TIMER_COMMAND,
1717
type: metric.type,
1818
handler: metric.handler,
1919
});

src/wrapper/TimerWrapper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ export class TimerWrapper {
466466
};
467467
}
468468

469+
runCommand(type: ETimerType, handler: number) {
470+
if (type === ETimerType.TIMEOUT) {
471+
window.clearTimeout(handler);
472+
} else {
473+
window.clearInterval(handler);
474+
}
475+
}
476+
469477
cleanHistory() {
470478
this.setTimeoutHistory.clear();
471479
this.clearTimeoutHistory.clear();

src/wrapper/TraceUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class TraceUtil {
105105
const stack = stackString.split(REGEX_STACKTRACE_SPLIT) || [];
106106
const rv: TTrace[] = [];
107107

108-
// loop from the end, excluding error name at [0] and self trace at [1|x]
108+
// loop from the end, excluding error name at [0] and self trace at [1|n]
109109
for (let n = stack.length - 1; n > 1; n--) {
110110
const parsed = this.#parseTraceRow(stack[n]);
111111

@@ -134,7 +134,7 @@ export class TraceUtil {
134134
#getShortTrace(stackString: string): TTrace | null {
135135
const stack = stackString.split(REGEX_STACKTRACE_SPLIT) || [];
136136

137-
// loop from the start, excluding error name at [0] and self trace at [1|x]
137+
// loop from the start, excluding error name at [0] and self trace at [1|n]
138138
for (let n = 2, N = stack.length; n < N; n++) {
139139
const parsed = this.#parseTraceRow(stack[n]);
140140

src/wrapper/Wrapper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function setSettings(settings: TSettings) {
8383
wrapApis();
8484
}
8585

86-
export function wrapperOnEachSecond() {
86+
export function onEachSecond() {
8787
apiMedia.meetMedia();
8888
if (
8989
panels.requestAnimationFrame.wrap &&
@@ -128,6 +128,12 @@ export function runMediaCommand(
128128
apiMedia.runCommand(...args);
129129
}
130130

131+
export function runTimerCommand(
132+
...args: Parameters<TimerWrapper['runCommand']>
133+
) {
134+
apiTimer.runCommand(...args);
135+
}
136+
131137
export function cleanHistory() {
132138
apiEval.cleanHistory();
133139
apiTimer.cleanHistory();

0 commit comments

Comments
 (0)