|
1 | | -import { windowListen, windowPost } from './api/communication.ts'; |
| 1 | +import { EMsg, windowListen, windowPost } from './api/communication.ts'; |
2 | 2 | import { IS_DEV } from './api/env.ts'; |
3 | 3 | import { |
4 | 4 | TELEMETRY_FREQUENCY_1PS, |
5 | 5 | TELEMETRY_FREQUENCY_30PS, |
6 | 6 | } from './api/const.ts'; |
7 | 7 | import { Timer } from './api/time.ts'; |
8 | 8 | import { |
9 | | - collectMediaMetrics, |
10 | | - meetMedia, |
11 | | - doMediaCommand, |
12 | | - type TMediaTelemetry, |
13 | | -} from './api/mediaMonitor.ts'; |
14 | | -import { Wrapper, type TWrapperMetrics } from './wrapper/Wrapper.ts'; |
15 | | -import { panelsArray2Map, type TPanelMap } from './api/settings.ts'; |
| 9 | + wrapperOnEachSecond as onEachSecond, |
| 10 | + setSettings, |
| 11 | + cleanHistory, |
| 12 | + collectMetrics, |
| 13 | + runMediaCommand, |
| 14 | +} from './wrapper/Wrapper.ts'; |
16 | 15 | import { ETimerType } from './wrapper/TimerWrapper.ts'; |
17 | 16 |
|
18 | | -export interface TMetrics { |
19 | | - mediaMetrics: TMediaTelemetry; |
20 | | - wrapperMetrics: TWrapperMetrics; |
21 | | - collectingStartTime: number; |
22 | | -} |
23 | | - |
24 | | -let panels: TPanelMap; |
25 | | -let wrapper: Wrapper; |
26 | | -const eachSecond = new Timer({ delay: 1e3, repetitive: true }, () => { |
27 | | - meetMedia(document.querySelectorAll('video,audio')); |
28 | | - wrapper.eachSecond(); |
29 | | -}); |
| 17 | +const eachSecond = new Timer({ delay: 1e3, repetitive: true }, onEachSecond); |
30 | 18 | const tick = new Timer( |
31 | 19 | { delay: TELEMETRY_FREQUENCY_1PS, repetitive: true }, |
32 | | - function apiMonitorPostMetric() { |
| 20 | + function apiMonitorTelemetryTick() { |
33 | 21 | const now = Date.now(); |
34 | 22 |
|
35 | 23 | windowPost({ |
36 | | - msg: 'telemetry', |
37 | | - metrics: { |
38 | | - mediaMetrics: collectMediaMetrics(panels.media.visible), |
39 | | - wrapperMetrics: wrapper.collectMetrics(), |
40 | | - collectingStartTime: now, |
41 | | - }, |
| 24 | + msg: EMsg.TELEMETRY, |
| 25 | + collectingStartTime: now, |
| 26 | + telemetry: collectMetrics(), |
42 | 27 | }); |
43 | 28 | } |
44 | 29 | ); |
45 | 30 |
|
46 | 31 | windowListen((o) => { |
47 | | - if (o.msg === 'settings' && o.settings && typeof o.settings === 'object') { |
48 | | - if (!wrapper) { |
49 | | - wrapper = new Wrapper(panels); |
50 | | - } |
51 | | - panels = panelsArray2Map(o.settings.panels); |
52 | | - wrapper.setup(panels, o.settings); |
53 | | - } else if (o.msg === 'start-observe') { |
| 32 | + 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); |
| 37 | + } else if ( |
| 38 | + o.msg === EMsg.SETTINGS && |
| 39 | + o.settings && |
| 40 | + typeof o.settings === 'object' |
| 41 | + ) { |
| 42 | + setSettings(o.settings); |
| 43 | + } else if (o.msg === EMsg.START_OBSERVE) { |
54 | 44 | tick.start(); |
55 | 45 | eachSecond.start(); |
56 | | - } else if (o.msg === 'stop-observe') { |
| 46 | + } else if (o.msg === EMsg.STOP_OBSERVE) { |
57 | 47 | tick.stop(); |
58 | 48 | eachSecond.stop(); |
59 | | - } else if (o.msg === 'reset-wrapper-history') { |
60 | | - wrapper.cleanHistory(); |
| 49 | + } else if (o.msg === EMsg.RESET_WRAPPER_HISTORY) { |
| 50 | + cleanHistory(); |
61 | 51 | !tick.isPending && tick.trigger(); |
62 | | - } else if (o.msg === 'clear-timer-handler') { |
| 52 | + } else if (o.msg === EMsg.CLEAR_TIMER_HANDLER) { |
63 | 53 | if (o.type === ETimerType.TIMEOUT) { |
64 | 54 | window.clearTimeout(o.handler); |
65 | 55 | } else { |
66 | 56 | window.clearInterval(o.handler); |
67 | 57 | } |
68 | | - } else if (o.msg === 'media-command') { |
69 | | - doMediaCommand(o.mediaId, o.cmd, o.property); |
70 | | - } else if (o.msg === 'telemetry-acknowledged') { |
71 | | - // adaptive update-frequency |
72 | | - const ackTrafficDuration = Date.now() - o.timeSent; |
73 | | - const newDelay = (o.trafficDuration + ackTrafficDuration) * 3; |
74 | | - tick.delay = Math.max(TELEMETRY_FREQUENCY_30PS, newDelay); |
| 58 | + } else if (o.msg === EMsg.MEDIA_COMMAND) { |
| 59 | + runMediaCommand(o.mediaId, o.cmd, o.property); |
75 | 60 | } |
76 | 61 | }); |
77 | 62 |
|
|
0 commit comments