debugBus.ts
7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* 开发者调试视图的数据总线。
*
* 为什么要单独一层:调试视图的读者是"看机制的人",而它要读的东西散在三个地方 ——
* 出帧热路径(`@hm/map-overlay` 的画布出帧、各层 paint)、地图事件(MapStage 的手势 effect)、
* 取数结果(runAggregate)。把这些都挂进 zustand 就等于让"每帧一次的落笔计数"驱动 React 重渲染,
* 正是这套界面一直在躲的开销。所以这里和 gesture.ts 一样用模块级状态:
*
* - 写的人(层、MapStage)先问一次 `debugEnabled()`,关掉时一帧只多一个布尔判断;
* - 读的人(面板、覆盖画布)按自己的节律取,不参与地图出帧。
*
* ⚠️ 这里不许 import 任何图层模块:各层会 import 本模块,反向依赖会成环。
* 层的现状由层自己在建层时注册一个 `read()` 闭包进来(注册口在 `map/appOverlay.ts`)。
* 整块调试要剔除时,被引用的只有那一个注册口与本目录。
*/
import type { SurfaceReadout } from '@hm/map-overlay';
/** 一条调试记录的类型。它决定面板与时间轴上的颜色。 */
export type DebugEventKind = 'event' | 'gesture' | 'frame' | 'aggregate' | 'layer';
export interface DebugEvent {
/** 记下那一刻的 performance.now() */
at: number;
kind: DebugEventKind;
/** 正文。同名同类的事件在合并窗口内只累加 `n`,不新增行 */
text: string;
/** 附带读数,面板把它写在行尾(如 `桶 812 / 耗时 6ms`) */
detail?: string;
/** 合并计数:这一个窗口里同样的事发生了几次 */
n: number;
}
/**
* 同名事件合并的窗口(毫秒)。
*
* 拖动一次会发出上百个 drag/mapmove,逐条记录等于把真正要看的那几行(定档、收尾、
* 聚合发起与回包)冲出缓冲区。合并在同一个窗口内发生多次只写一行并累加 `n` ——
* "这一拍事件有多密"本身就是想看到的信息。
*/
const MERGE_WINDOW_MS = 240;
/** 时间轴保留的条数上限。再多也没法在一屏里读完。 */
const MAX_EVENTS = 120;
/**
* 一层的现状快照:画布那一半直接复用内核的 `SurfaceReadout`(同一批数,别再抄一份字段清单,
* 抄一遍就多一处「面板念的和层记的不是同一个数」的口子),外面这三项才是宿主才知道的事。
* `painted`/`total` 分开是因为快拖那一拍只画 0 笔但数据一条不少 —— 那正是本项目最容易被
* 误读成"数字没了=数据没了"的一格。
*/
export type LayerDebugStats = SurfaceReadout & {
/** CustomLayer 是否已创建(四层都是懒建,没建就等于这一层此刻不花钱) */
built: boolean;
visible: boolean;
/** 这一层自己的补充口径(如热力的归一化上限、聚焦层的形状种类) */
note?: string;
};
export interface LayerDebugRecord {
id: string;
/** 类名 + 中文职责,面板直接念这串 */
name: string;
zIndex: number;
kind: 'canvas' | 'webgl';
/** 调试视图描边用;热力层为 null */
canvas: HTMLCanvasElement | null;
read: () => LayerDebugStats;
}
export interface DebugBBox {
minLng: number;
maxLng: number;
minLat: number;
maxLat: number;
}
/** 最近一次聚合的口径。覆盖画布画网格、面板报数都读这一份。 */
export interface AggregateDebug {
zoom: number;
/** 请求用的外扩框 */
bbox: DebugBBox;
/** 真实可见范围(没外扩的) */
view: DebugBBox;
bucketCount: number;
matched: number;
used: number;
ms: number;
gridPx: number;
dLng: number;
dLat: number;
maxCount: number;
/** 桶中心(×1e6)与网格坐标:覆盖画布画格子与错位连线用它 */
lng: Int32Array | null;
lat: Int32Array | null;
gx: Int32Array | null;
gy: Int32Array | null;
count: Int32Array | null;
}
let enabled = false;
let events: DebugEvent[] = [];
let version = 0;
const listeners = new Set<() => void>();
const records = new Map<string, LayerDebugRecord>();
let aggregate: AggregateDebug | null = null;
/**
* 通知订阅者。
*
* 只在"确实有人要看"时才做:没人订阅时数组与版本号照旧维护,代价是几次赋值,
* 而写的人那一头始终只多一个布尔判断。
*/
function bump(): void {
version += 1;
for (const fn of listeners) fn();
}
/** 模块级开关。React 侧的 store 只负责记住用户的选择,真正的门在这里。 */
export function debugEnabled(): boolean {
return enabled;
}
export function setDebugEnabled(on: boolean): void {
if (enabled === on) return;
enabled = on;
if (!on) {
// 关掉时把计数清零:下次打开看到的应当是"从现在起"的现场,而不是一堆陈年帧数
for (const r of records.values()) r.canvas?.style.removeProperty('outline');
events = [];
aggregate = null;
}
bump();
}
/** 写一条记录。关掉调试时立刻返回,调用点不必各自加 if。 */
export function pushDebugEvent(kind: DebugEventKind, text: string, detail?: string): void {
if (!enabled) return;
const now = performance.now();
const last = events[events.length - 1];
if (last && last.kind === kind && last.text === text && now - last.at < MERGE_WINDOW_MS) {
last.n += 1;
last.detail = detail ?? last.detail;
// 合并也要让面板跟上:不然"×N"这个数字看着是停住的
bump();
return;
}
events.push({ at: now, kind, text, detail, n: 1 });
if (events.length > MAX_EVENTS) events.splice(0, events.length - MAX_EVENTS);
bump();
}
export function getDebugEvents(): DebugEvent[] {
return events;
}
export function clearDebugEvents(): void {
if (!enabled) return;
events = [];
bump();
}
export function getAggregateDebug(): AggregateDebug | null {
return aggregate;
}
export function setAggregateDebug(next: AggregateDebug): void {
if (!enabled) return;
aggregate = next;
bump();
}
/** 层在建层时注册;`canvas` 传进来只为描边高亮,读数走 `read()`。 */
export function registerDebugLayer(record: LayerDebugRecord): void {
records.set(record.id, record);
bump();
}
export function unregisterDebugLayer(id: string): void {
if (!records.delete(id)) return;
bump();
}
export function getDebugLayers(): LayerDebugRecord[] {
return [...records.values()].sort((a, b) => a.zIndex - b.zIndex);
}
/** 面板与覆盖画布的订阅口。返回注销函数。 */
export function subscribeDebug(fn: () => void): () => void {
listeners.add(fn);
return () => listeners.delete(fn);
}
/** 快照版本号。useSyncExternalStore 用它做变化判定,避免每次复制数组。 */
export function debugVersion(): number {
return version;
}
/** URL 明确要了调试视图:`?debug=1`。任何构建下都认这一条。 */
export function debugWantedByUrl(): boolean {
return new URLSearchParams(window.location.search).get('debug') === '1';
}
/**
* 调试视图可用吗:开发构建,或 URL 带了 `?debug=1`。
*
* 正式构建默认整块不进界面 —— 业务用户不会看到一个「调试」小牌。
*/
export function debugAvailable(): boolean {
return import.meta.env.DEV || debugWantedByUrl();
}
/** 打开时的初始态:URL 要就直接开,开发构建默认收着(免得每次起服务都得先关一下) */
export function debugInitialOn(): boolean {
return debugWantedByUrl();
}