File size: 1,728 Bytes
9d2d895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface CountryIdentity {
  code: string;
  name: string;
}

export interface CountryHoverCache {
  code: string | null;
  name: string | null;
}

export type CancelableFrameTask = (() => void) & { cancel(): void };

export interface CountryHoverQueryController<TPoint> {
  queue(point: TPoint): void;
  cancel(): void;
  isPending(): boolean;
}

export function shouldRunInputSensitiveMapWork(isInputPending: () => boolean): boolean {
  return !isInputPending();
}

export function shouldRenderTradeAnimationFrame(frameCount: number, isInputPending: () => boolean): boolean {
  return frameCount % 2 === 0 && shouldRunInputSensitiveMapWork(isInputPending);
}

export function resolveCountryForPointerInteraction(
  hover: CountryHoverCache,
  hoverQueryPending: boolean,
  resolveFromCoordinate: () => CountryIdentity | null,
): CountryIdentity | null {
  if (!hoverQueryPending && hover.code && hover.name) {
    return { code: hover.code, name: hover.name };
  }
  return resolveFromCoordinate();
}

export function createCountryHoverQueryController<TPoint>(
  scheduleFrame: (run: () => void) => CancelableFrameTask,
  runQuery: (point: TPoint) => void,
): CountryHoverQueryController<TPoint> {
  let pendingPoint: TPoint | null = null;
  let pending = false;
  const frameTask = scheduleFrame(() => {
    const point = pendingPoint;
    pendingPoint = null;
    pending = false;
    if (point !== null) runQuery(point);
  });

  return {
    queue(point: TPoint): void {
      pendingPoint = point;
      pending = true;
      frameTask();
    },
    cancel(): void {
      pendingPoint = null;
      pending = false;
      frameTask.cancel();
    },
    isPending(): boolean {
      return pending;
    },
  };
}