-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtypes.ts
More file actions
135 lines (117 loc) · 4.37 KB
/
Copy pathtypes.ts
File metadata and controls
135 lines (117 loc) · 4.37 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
import type { DOMAttributes } from "react";
import type { BatchConfig } from "../scheduler/types";
import { type z } from "zod";
export type AnyContext = Record<string, any>;
export type AnyEventParams = Record<string, any>;
export type AnySchemas = Record<string, z.ZodObject<any>>;
export type SchemaParams<Schemas extends AnySchemas = AnySchemas, T extends keyof Schemas = keyof Schemas> = z.infer<
Schemas[T]
>;
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>;
export type DOMEventNames = keyof Omit<DOMAttributes<HTMLDivElement>, "children" | "dangerouslySetInnerHTML">;
export type DOMEvents<P, C> = Partial<Record<DOMEventNames, EventFunction<P, C>>>;
type EventFunction<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 type EventParamsWithContext<EventParams, Context> = EventParams | ((context: Context) => EventParams);
export type EventParamsWithSchema<
Schemas extends AnySchemas = AnySchemas,
T extends keyof Schemas = keyof Schemas,
Context extends AnyContext = AnyContext,
> = {
schema: T;
params: ((context: Context) => SchemaParams<Schemas, T>) | SchemaParams<Schemas, T>;
};
export type SchemaConfig<Schemas extends AnySchemas> = {
schemas: Schemas;
onSchemaError?: (error: z.ZodError) => void;
abortOnError?: boolean;
};
export type UnionPropsWithAndWithoutSchema<
T extends keyof Schemas,
Schemas extends AnySchemas = AnySchemas,
EventParams extends AnyEventParams = AnyEventParams,
Context extends AnyContext = AnyContext,
> = PropsWithSchema<T, Schemas, Context> | PropsWithoutSchema<EventParams, Context>;
export type PropsWithSchema<
T extends keyof Schemas,
Schemas extends AnySchemas = AnySchemas,
Context extends AnyContext = AnyContext,
> = {
schema: T;
params: ((context: Context) => SchemaParams<Schemas, T>) | SchemaParams<Schemas, T>;
};
export type PropsWithoutSchema<
EventParams extends AnyEventParams = AnyEventParams,
Context extends AnyContext = AnyContext,
> = {
schema?: undefined;
params: EventParamsWithContext<EventParams, Context>;
};
export interface TrackerConfig<Context, EventParams, Schemas extends AnySchemas> {
/**
* 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?: EventFunction<EventParams | SchemaParams<Schemas, keyof Schemas>, Context>;
/**
* The events to listen to.
*/
readonly DOMEvents?: DOMEvents<EventParams | SchemaParams<Schemas, keyof Schemas>, Context>;
/**
* The impression event to listen to.
*/
impression?: {
onImpression: EventFunction<EventParams | SchemaParams<Schemas, keyof Schemas>, Context>;
options?: ImpressionOptions;
};
/**
* The page track event to listen to.
*/
pageView?: {
onPageView: EventFunction<EventParams | SchemaParams<Schemas, keyof Schemas>, Context>;
/**
* TODO: add options
*/
// options?: PageViewOptions;
};
batch?: BatchConfig;
schema?: SchemaConfig<Schemas>;
}
export interface TrackerContextProps<Context, EventParams, Schemas extends AnySchemas> {
tracker: TrackerConfig<Context, EventParams, Schemas>;
_setContext: (context: Context | ((prevContext: Context) => Context)) => void;
_getContext: () => Context;
_schedule: (task: Task) => Promise<void>;
}