-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtypes.ts
More file actions
90 lines (77 loc) · 3 KB
/
Copy pathtypes.ts
File metadata and controls
90 lines (77 loc) · 3 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
import { DOMAttributes } from "react";
import type { SchedulerConfig } from "../scheduler/types";
export interface ImpressionOptions {
/**
* A threshold indicating the percentage of the target's visibility needed to trigger the callback
* @default 0.2
*/
threshold?: number;
/**
* If true, freezes the intersection state once the element becomes visible.
* @default true
*/
freezeOnceVisible?: boolean;
/**
* The initial state of the intersection.
* @default false
*/
initialIsIntersecting?: boolean;
}
export interface PageViewOptions {
// TODO: add options
}
export type SetContext<C = unknown> = (context: C | ((prevContext: C) => C)) => void;
export type TaskReturnType<T = any> = void | EventResult<T> | Promise<void | EventResult<T>>;
export type Task<T = any> = (...args: any) => TaskReturnType<T>;
type InitFunction<C> = (initialContext: C, setContext: SetContext<C>) => void | Promise<void>;
type SendFunction<P, C> = (params: P, context: C, setContext: SetContext<C>) => TaskReturnType;
export type DOMEventNames = keyof Omit<DOMAttributes<HTMLDivElement>, "children" | "dangerouslySetInnerHTML">;
export type DOMEvents<P, C> = Partial<Record<DOMEventNames, DOMEventFunction<P, C>>>;
type DOMEventFunction<P, C> = (params: P, context: C, setContext: SetContext<C>) => TaskReturnType;
type ImpressionFunction<P, C> = (params: P, context: C, setContext: SetContext<C>) => TaskReturnType;
type PageViewFunction<P, C> = (params: P, context: C, setContext: SetContext<C>) => TaskReturnType;
export type EventResult<T = any> = Record<string, T>;
export type EventNames = "onImpression" | "onPageView" | DOMEventNames;
export interface TrackerConfig<Context, SendParams, DOMEventParams, ImpressionParams, PageViewParams> {
/**
* Initialize the tracker with the given context.
* @param initialContext - The initial context to use for the tracker.
* @returns void
*/
readonly init?: InitFunction<Context>;
/**
* The send function to send the event.
* @param params - The custom params to send.
* @param context - The context to send.
* @returns void
*/
readonly send?: SendFunction<SendParams, Context>;
/**
* The events to listen to.
*/
readonly DOMEvents?: DOMEvents<DOMEventParams, Context>;
/**
* The impression event to listen to.
*/
impression?: {
onImpression: ImpressionFunction<ImpressionParams, Context>;
options?: ImpressionOptions;
};
/**
* The page track event to listen to.
*/
pageView?: {
onPageView: PageViewFunction<PageViewParams, Context>;
/**
* TODO: add options
*/
// options?: PageViewOptions;
};
batch?: SchedulerConfig["batch"];
}
export interface TrackerContextProps<Context, SendParams, EventParams, ImpressionParams, PageTrackParams> {
tracker: TrackerConfig<Context, SendParams, EventParams, ImpressionParams, PageTrackParams>;
_setContext: (context: Context | ((prevContext: Context) => Context)) => void;
_getContext: () => Context;
_schedule: (task: Task) => Promise<void>;
}