HeatmapSession.ts 5.85 KB
import type { RefObject } from 'react';
import { Frame } from '@hm/map-overlay';
import type {
  AggregateResult,
  Filter,
  HotspotRow,
  MetaFile,
} from '../aggregate/types';
import { GRID } from '../aggregate/grid';
import { ClusterLayer } from '../map/ClusterLayer';
import { BucketHoverLayer } from '../map/BucketHoverLayer';
import { CasePinLayer } from '../map/CasePinLayer';
import { HotspotLayer } from '../map/HotspotLayer';
import { ScopeLayer } from '../map/ScopeLayer';
import { SpotlightLayer } from '../map/SpotlightLayer';
import type { PerfSnapshot } from '@/store/useHeatmapStore';
import type { Basemap, HeatLayerLike } from '../map/provider/registry';
import type { MapScope } from '@/store/useHeatmapStore';
import type { HeatKey } from '../map/heatFeed';
import { dispatchIntent, type HeatmapIntentKind } from '@/store/intentBus';

/**
 * 一张地图会话持有的全部可变状态。
 *
 * 阶段 2 从 MapStage 的 34 个散落 ref 收敛而来。收进来的判据只有一条:
 * **被两个以上的异步流程或 effect 共享**。只服务于单个 effect 的「上一次值」
 * (prevFilter / prevDimFit / prevRailTab 等)不进来 —— 那不是会话状态,
 * 是那个 effect 自己的局部账,塞进来只会让容器变成另一个上帝对象。
 *
 * 这里没有 React state:mapReady / spotCellM / emptyDismissed / viewportTick 决定的是
 * JSX 长什么样,留在组件里;本类只放「决定画什么、往哪儿画」的那批引用与序号。
 *
 * ⚠️ 字段的更新时机必须与搬移前逐字一致:
 *   - filter / mapFilter / mapScope / meta 每次 render 由 MapStage 同步写入(原 xxxRef.current);
 *   - pendingFit / fitScope / userMoved 的写入顺序在 aggregateFlow 里有依赖,不可重排;
 *   - token 只在各自流程的入口 ++,作废判断只认 token 本身。
 */
export class HeatmapSession {
  /* ---------------- DOM 句柄 ----------------
   * 直接持有 React ref 对象而不是快照值:JSX 里的 ref={containerRef} 不必改,
   * 而 flow 读到的永远是当下那一个 DOM(地图初始化前为 null,之后由 React 写入)。
   */
  constructor(
    readonly containerRef: RefObject<HTMLDivElement | null>,
    readonly wrapRef: RefObject<HTMLDivElement | null>,
  ) {}

  get container(): HTMLDivElement | null {
    return this.containerRef.current;
  }
  get wrap(): HTMLDivElement | null {
    return this.wrapRef.current;
  }

  /* ---------------- SDK 与图层实例 ---------------- */
  map: Basemap | null = null;
  heat: HeatLayerLike | null = null;
  cluster: ClusterLayer | null = null;
  /** 数字桶悬浮反馈层。只在 hover 到桶时建画布,平时不占一张全屏 backing store */
  bucketHover: BucketHoverLayer | null = null;
  hotspot: HotspotLayer | null = null;
  pin: CasePinLayer | null = null;
  scope: ScopeLayer | null = null;
  spot: SpotlightLayer | null = null;

  /* ---------------- 数据快照与指纹 ---------------- */
  agg: AggregateResult | null = null;
  /** 热力层最近一次的输入指纹与它算出的读数。两者一起才是「这次不必重喂」的凭据 */
  heatKey: HeatKey | null = null;
  heatReadout: Partial<PerfSnapshot> = {
    heatPoints: 0,
    backgroundBuckets: 0,
    heatGridPx: GRID.gridPx,
  };
  /** 只服务抽屉避让的投影基准(每帧摆一次,不重画就不必再摆) */
  projector: Frame | null = null;

  /* ---------------- 防抖计时器 ---------------- */
  refreshTimer: number | null = null;
  analysisTimer: number | null = null;
  statsTimer: number | null = null;

  /* ---------------- 请求序号(last-wins) ----------------
   * Worker 是排队处理的,先发的请求可能后回来。回来时若已不是最新一次,
   * 结果整块丢弃 —— 每个流程只认自己的那一个 token。
   */
  aggToken = 0;
  statsToken = 0;
  analysisToken = 0;
  spotToken = 0;

  /* ---------------- 取景状态机 ----------------
   * 这四组字段决定「数据回包时相机该不该动」。写入顺序有依赖,见 aggregateFlow。
   */
  /** 上一次取景的那一片黑点(对象本身,不是序号:筛选一变,同一序号可能是另一片) */
  lastFocusRow: HotspotRow | null = null;
  /** 上一次已经取过景的那一刀收窄 */
  fitScope: MapScope | null = null;
  /** 「这一刀还没取过景」的待办;只有它等于当前那一刀时回包才允许搬相机 */
  pendingFit: MapScope | null = null;
  /** 用户是否亲自动过镜头;数据驱动的取景必须尊重它 */
  userMoved = false;

  /* ---------------- store 快照(每次 render 由 MapStage 同步) ----------------
   * 事件回调里要读最新筛选条件,但不能因为它们变化而重绑地图事件。
   */
  filter!: Filter;
  mapFilter!: Filter;
  mapScope: MapScope | null = null;
  meta: MetaFile | null = null;

  /**
   * 发一次地图意图。阶段 3 从「面板改 store 里的 seq」换成显式命令:
   * store action 与会话内的 flow 共用这一条通道,MapStage 按意图类型订阅版本。
   *
   * 和原先的 seq 一样「每次调用都算一次请求」——重复点同一行也要重新取景。
   * 与 SDK 状态不同,意图不需要 dispose:它是事件,不是资源。
   */
  dispatch(intent: HeatmapIntentKind): void {
    dispatchIntent(intent);
  }

  /** 销毁所有图层与地图。卸载顺序与搬移前一致:先图层、后底图 */
  dispose(): void {
    this.heat?.destroy();
    this.cluster?.destroy();
    this.bucketHover?.destroy();
    this.bucketHover = null;
    this.hotspot?.destroy();
    this.pin?.destroy();
    this.pin = null;
    this.scope?.destroy();
    this.scope = null;
    this.spot?.destroy();
    this.spot = null;
    this.map?.destroy();
    this.map = null;
    this.heat = null;
    this.cluster = null;
    this.bucketHover = null;
    this.hotspot = null;
  }
}